Bug 1265408 - Add LogToDeveloperConsole to WebAudioUtils; r?padenot draft
authorDan Minor <dminor@mozilla.com>
Fri, 03 Jun 2016 14:23:11 -0400
changeset 375233 e943d454220dcdf02fbe976299ffc43f6c2a1fe9
parent 375224 07e566f903b8747f97ba15d84662f14e430f546b
child 375234 a9699c1dbdae043fbecdb36f6ab276edbb67f50d
push id20196
push userdminor@mozilla.com
push dateFri, 03 Jun 2016 18:26:34 +0000
reviewerspadenot
bugs1265408
milestone49.0a1
Bug 1265408 - Add LogToDeveloperConsole to WebAudioUtils; r?padenot MozReview-Commit-ID: 2Zf59JjQX9u
dom/media/webaudio/WebAudioUtils.cpp
dom/media/webaudio/WebAudioUtils.h
--- a/dom/media/webaudio/WebAudioUtils.cpp
+++ b/dom/media/webaudio/WebAudioUtils.cpp
@@ -3,16 +3,20 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "WebAudioUtils.h"
 #include "AudioNodeStream.h"
 #include "blink/HRTFDatabaseLoader.h"
 
+#include "nsContentUtils.h"
+#include "nsIConsoleService.h"
+#include "nsIScriptError.h"
+
 namespace mozilla {
 
 LazyLogModule gWebAudioAPILog("WebAudioAPI");
 
 namespace dom {
 
 void WebAudioUtils::ConvertAudioTimelineEventToTicks(AudioTimelineEvent& aEvent,
                                                      AudioNodeStream* aDest)
@@ -84,10 +88,64 @@ WebAudioUtils::SpeexResamplerProcess(Spe
   tmp2.SetLength(*aOutLen);
   ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen);
   int result = speex_resampler_process_float(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen);
   ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen);
   return result;
 #endif
 }
 
+void
+WebAudioUtils::LogToDeveloperConsole(uint64_t aWindowID, const char* aKey)
+{
+  // This implementation is derived from dom/media/VideoUtils.cpp, but we
+  // use a windowID so that the message is delivered to the developer console.
+  // It is similar to ContentUtils::ReportToConsole, but also works off main
+  // thread.
+  if (!NS_IsMainThread()) {
+    nsCOMPtr<nsIRunnable> task =
+      NS_NewRunnableFunction([aWindowID, aKey]() { LogToDeveloperConsole(aWindowID, aKey); });
+    NS_DispatchToMainThread(task.forget(), NS_DISPATCH_NORMAL);
+    return;
+  }
+
+  nsCOMPtr<nsIConsoleService> console(
+    do_GetService("@mozilla.org/consoleservice;1"));
+  if (!console) {
+    NS_WARNING("Failed to log message to console.");
+    return;
+  }
+
+  nsAutoCString spec;
+  uint32_t aLineNumber, aColumnNumber;
+  JSContext *cx = nsContentUtils::GetCurrentJSContext();
+  if (cx) {
+    nsJSUtils::GetCallingLocation(cx, spec, &aLineNumber, &aColumnNumber);
+  }
+
+  nsresult rv;
+  nsCOMPtr<nsIScriptError> errorObject =
+      do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
+  if (!errorObject) {
+    NS_WARNING("Failed to log message to console.");
+    return;
+  }
+
+  nsXPIDLString result;
+  rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
+                                          aKey, result);
+
+  if (NS_FAILED(rv)) {
+    NS_WARNING("Failed to log message to console.");
+    return;
+  }
+
+  errorObject->InitWithWindowID(result,
+                                NS_ConvertUTF8toUTF16(spec),
+                                EmptyString(),
+                                aLineNumber, aColumnNumber,
+                                nsIScriptError::warningFlag, "Web Audio",
+                                aWindowID);
+  console->LogMessage(errorObject);
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/media/webaudio/WebAudioUtils.h
+++ b/dom/media/webaudio/WebAudioUtils.h
@@ -220,15 +220,19 @@ namespace WebAudioUtils {
                         const int16_t* aIn, uint32_t* aInLen,
                         float* aOut, uint32_t* aOutLen);
 
   int
   SpeexResamplerProcess(SpeexResamplerState* aResampler,
                         uint32_t aChannel,
                         const int16_t* aIn, uint32_t* aInLen,
                         int16_t* aOut, uint32_t* aOutLen);
+
+  void
+  LogToDeveloperConsole(uint64_t aWindowID, const char* aKey);
+
   } // namespace WebAudioUtils
 
 } // namespace dom
 } // namespace mozilla
 
 #endif