MediaChild.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "MediaChild.h"
  6. #include "MediaParent.h"
  7. #include "nsGlobalWindow.h"
  8. #include "mozilla/MediaManager.h"
  9. #include "mozilla/Logging.h"
  10. #include "nsQueryObject.h"
  11. #undef LOG
  12. mozilla::LazyLogModule gMediaChildLog("MediaChild");
  13. #define LOG(args) MOZ_LOG(gMediaChildLog, mozilla::LogLevel::Debug, args)
  14. namespace mozilla {
  15. namespace media {
  16. already_AddRefed<Pledge<nsCString>>
  17. GetOriginKey(const nsCString& aOrigin, bool aPrivateBrowsing, bool aPersist)
  18. {
  19. RefPtr<MediaManager> mgr = MediaManager::GetInstance();
  20. MOZ_ASSERT(mgr);
  21. RefPtr<Pledge<nsCString>> p = new Pledge<nsCString>();
  22. uint32_t id = mgr->mGetOriginKeyPledges.Append(*p);
  23. if (XRE_GetProcessType() == GeckoProcessType_Default) {
  24. mgr->GetNonE10sParent()->RecvGetOriginKey(id, aOrigin, aPrivateBrowsing,
  25. aPersist);
  26. } else {
  27. Child::Get()->SendGetOriginKey(id, aOrigin, aPrivateBrowsing, aPersist);
  28. }
  29. return p.forget();
  30. }
  31. void
  32. SanitizeOriginKeys(const uint64_t& aSinceWhen, bool aOnlyPrivateBrowsing)
  33. {
  34. LOG(("SanitizeOriginKeys since %llu %s", aSinceWhen,
  35. (aOnlyPrivateBrowsing? "in Private Browsing." : ".")));
  36. if (XRE_GetProcessType() == GeckoProcessType_Default) {
  37. // Avoid opening MediaManager in this case, since this is called by
  38. // sanitize.js when cookies are cleared, which can happen on startup.
  39. RefPtr<Parent<NonE10s>> tmpParent = new Parent<NonE10s>();
  40. tmpParent->RecvSanitizeOriginKeys(aSinceWhen, aOnlyPrivateBrowsing);
  41. } else {
  42. Child::Get()->SendSanitizeOriginKeys(aSinceWhen, aOnlyPrivateBrowsing);
  43. }
  44. }
  45. static Child* sChild;
  46. Child* Child::Get()
  47. {
  48. MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Content);
  49. MOZ_ASSERT(NS_IsMainThread());
  50. if (!sChild) {
  51. sChild = static_cast<Child*>(dom::ContentChild::GetSingleton()->SendPMediaConstructor());
  52. }
  53. return sChild;
  54. }
  55. Child::Child()
  56. : mActorDestroyed(false)
  57. {
  58. LOG(("media::Child: %p", this));
  59. MOZ_COUNT_CTOR(Child);
  60. }
  61. Child::~Child()
  62. {
  63. LOG(("~media::Child: %p", this));
  64. sChild = nullptr;
  65. MOZ_COUNT_DTOR(Child);
  66. }
  67. void Child::ActorDestroy(ActorDestroyReason aWhy)
  68. {
  69. mActorDestroyed = true;
  70. }
  71. bool
  72. Child::RecvGetOriginKeyResponse(const uint32_t& aRequestId, const nsCString& aKey)
  73. {
  74. RefPtr<MediaManager> mgr = MediaManager::GetInstance();
  75. if (!mgr) {
  76. return false;
  77. }
  78. RefPtr<Pledge<nsCString>> pledge = mgr->mGetOriginKeyPledges.Remove(aRequestId);
  79. if (pledge) {
  80. pledge->Resolve(aKey);
  81. }
  82. return true;
  83. }
  84. PMediaChild*
  85. AllocPMediaChild()
  86. {
  87. return new Child();
  88. }
  89. bool
  90. DeallocPMediaChild(media::PMediaChild *aActor)
  91. {
  92. delete static_cast<Child*>(aActor);
  93. return true;
  94. }
  95. } // namespace media
  96. } // namespace mozilla