Bug 1330350 - Add holding mechanism to fix faulty device counter. r=jib, a=lizzard
authorMunro Mengjue Chiang <mchiang@mozilla.com>
Fri, 27 Jan 2017 00:08:06 +0800
changeset 376398 f668fe84b2b584d7cfaec1503277ece5caf91583
parent 376397 a09f50f513b876153ccab49775478daba3cc6863
child 376399 0e612af21502fcee49f1d490058d8109f7c4e1b9
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib, lizzard
bugs1330350
milestone53.0a2
Bug 1330350 - Add holding mechanism to fix faulty device counter. r=jib, a=lizzard MozReview-Commit-ID: CIUucjaglK1
dom/media/MediaDevices.cpp
dom/media/MediaDevices.h
--- a/dom/media/MediaDevices.cpp
+++ b/dom/media/MediaDevices.cpp
@@ -10,19 +10,42 @@
 #include "mozilla/MediaManager.h"
 #include "MediaTrackConstraints.h"
 #include "nsIEventTarget.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIPermissionManager.h"
 #include "nsPIDOMWindow.h"
 #include "nsQueryObject.h"
 
+#define DEVICECHANGE_HOLD_TIME_IN_MS 1000
+
 namespace mozilla {
 namespace dom {
 
+class FuzzTimerCallBack final : public nsITimerCallback
+{
+  ~FuzzTimerCallBack() {}
+
+public:
+  explicit FuzzTimerCallBack(MediaDevices* aMediaDevices) : mMediaDevices(aMediaDevices) {}
+
+  NS_DECL_ISUPPORTS
+
+  NS_IMETHOD Notify(nsITimer* aTimer) final
+  {
+    mMediaDevices->DispatchTrustedEvent(NS_LITERAL_STRING("devicechange"));
+    return NS_OK;
+  }
+
+private:
+  nsCOMPtr<MediaDevices> mMediaDevices;
+};
+
+NS_IMPL_ISUPPORTS(FuzzTimerCallBack, nsITimerCallback)
+
 class MediaDevices::GumResolver : public nsIDOMGetUserMediaSuccessCallback
 {
 public:
   NS_DECL_ISUPPORTS
 
   explicit GumResolver(Promise* aPromise) : mPromise(aPromise) {}
 
   NS_IMETHOD
@@ -187,25 +210,39 @@ NS_INTERFACE_MAP_BEGIN(MediaDevices)
   NS_INTERFACE_MAP_ENTRY(MediaDevices)
 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
 
 void
 MediaDevices::OnDeviceChange()
 {
   MOZ_ASSERT(NS_IsMainThread());
   nsresult rv = CheckInnerWindowCorrectness();
-  if (NS_FAILED(rv))
+  if (NS_FAILED(rv)) {
+    MOZ_ASSERT(false);
     return;
+  }
 
   if (!(MediaManager::Get()->IsActivelyCapturingOrHasAPermission(GetOwner()->WindowID()) ||
     Preferences::GetBool("media.navigator.permission.disabled", false))) {
     return;
   }
 
-  DispatchTrustedEvent(NS_LITERAL_STRING("devicechange"));
+  if (!mFuzzTimer)
+  {
+    mFuzzTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
+  }
+
+  if (!mFuzzTimer) {
+    MOZ_ASSERT(false);
+    return;
+  }
+
+  mFuzzTimer->Cancel();
+  RefPtr<FuzzTimerCallBack> cb = new FuzzTimerCallBack(this);
+  mFuzzTimer->InitWithCallback(cb, DEVICECHANGE_HOLD_TIME_IN_MS, nsITimer::TYPE_ONE_SHOT);
 }
 
 mozilla::dom::EventHandlerNonNull*
 MediaDevices::GetOndevicechange()
 {
   if (NS_IsMainThread()) {
     return GetEventHandler(nsGkAtoms::ondevicechange, EmptyString());
   }
--- a/dom/media/MediaDevices.h
+++ b/dom/media/MediaDevices.h
@@ -62,16 +62,17 @@ public:
                                 ErrorResult& aRv) override;
 
 private:
   class GumResolver;
   class EnumDevResolver;
   class GumRejecter;
 
   virtual ~MediaDevices();
+  nsCOMPtr<nsITimer> mFuzzTimer;
 };
 
 NS_DEFINE_STATIC_IID_ACCESSOR(MediaDevices,
                               MOZILLA_DOM_MEDIADEVICES_IMPLEMENTATION_IID)
 
 } // namespace dom
 } // namespace mozilla