author Andreas Pehrson <apehrson@mozilla.com>
Tue, 12 Nov 2019 10:30:16 +0000
changeset 501560 9e3f44e87a1a2be927c7fab135653c6b86b982c9
parent 448947 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1594466 - Don't rewrite mimeType if fully defined. r=bryce Prior to this patch the mimetype was rewritten per the printf format "%s; codecs=%s" also when codecs were defined in the constrained mime type. The latter '%s' would be the codecs string from the mime type parser, which would have dropped any quotation marks surrounding the string. Hence 'codecs="vp8, opus"' would be considered supported (quotation marks included), but when selecting mime type in start(), it would be rewritten with quotation marks dropped. Thus looking like 'codecs=vp8,' which is not supported. This patch removes the rewrite step when the mime type is fully defined with codecs, so that the quotation marks are left in place as given to the constructor. Differential Revision: https://phabricator.services.mozilla.com/D52520

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "nsMessageLoop.h"
#include "mozilla/WeakPtr.h"
#include "base/message_loop.h"
#include "base/task.h"
#include "nsINamed.h"
#include "nsIRunnable.h"
#include "nsITimer.h"
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsThreadUtils.h"

using namespace mozilla;

namespace {

 * This Task runs its nsIRunnable when Run() is called, or after
 * aEnsureRunsAfterMS milliseconds have elapsed since the object was
 * constructed.
 * Note that the MessageLoop owns this object and will delete it after it calls
 * Run().  Tread lightly.
class MessageLoopIdleTask : public Runnable,
                            public SupportsWeakPtr<MessageLoopIdleTask> {
  MessageLoopIdleTask(nsIRunnable* aTask, uint32_t aEnsureRunsAfterMS);
  NS_IMETHOD Run() override;

  nsresult Init(uint32_t aEnsureRunsAfterMS);

  nsCOMPtr<nsIRunnable> mTask;
  nsCOMPtr<nsITimer> mTimer;

  virtual ~MessageLoopIdleTask() {}

 * This timer callback calls MessageLoopIdleTask::Run() when its timer fires.
 * (The timer can't call back into MessageLoopIdleTask directly since that's
 * not a refcounted object; it's owned by the MessageLoop.)
 * We keep a weak reference to the MessageLoopIdleTask, although a raw pointer
 * should in theory suffice: When the MessageLoopIdleTask runs (right before
 * the MessageLoop deletes it), it cancels its timer.  But the weak pointer
 * saves us from worrying about an edge case somehow messing us up here.
class MessageLoopTimerCallback : public nsITimerCallback, public nsINamed {
  explicit MessageLoopTimerCallback(MessageLoopIdleTask* aTask);


  NS_IMETHOD GetName(nsACString& aName) override {
    return NS_OK;

  WeakPtr<MessageLoopIdleTask> mTask;

  virtual ~MessageLoopTimerCallback() {}

MessageLoopIdleTask::MessageLoopIdleTask(nsIRunnable* aTask,
                                         uint32_t aEnsureRunsAfterMS)
    : mozilla::Runnable("MessageLoopIdleTask"), mTask(aTask) {
  // Init() really shouldn't fail, but if it does, we schedule our runnable
  // immediately, because it's more important to guarantee that we run the task
  // eventually than it is to run the task when we're idle.
  nsresult rv = Init(aEnsureRunsAfterMS);
  if (NS_FAILED(rv)) {
        "Running idle task early because we couldn't initialize our timer.");

    mTask = nullptr;
    mTimer = nullptr;

nsresult MessageLoopIdleTask::Init(uint32_t aEnsureRunsAfterMS) {
  RefPtr<MessageLoopTimerCallback> callback =
      new MessageLoopTimerCallback(this);
  return NS_NewTimerWithCallback(getter_AddRefs(mTimer), callback,
                                 aEnsureRunsAfterMS, nsITimer::TYPE_ONE_SHOT);

MessageLoopIdleTask::Run() {
  // Null out our pointers because if Run() was called by the timer, this
  // object will be kept alive by the MessageLoop until the MessageLoop calls
  // Run().

  if (mTimer) {
    mTimer = nullptr;

  if (mTask) {
    mTask = nullptr;

  return NS_OK;

MessageLoopTimerCallback::MessageLoopTimerCallback(MessageLoopIdleTask* aTask)
    : mTask(aTask) {}

MessageLoopTimerCallback::Notify(nsITimer* aTimer) {
  // We don't expect to hit the case when the timer fires but mTask has been
  // deleted, because mTask should cancel the timer before the mTask is
  // deleted.  But you never know...
  NS_WARNING_ASSERTION(mTask, "This timer shouldn't have fired.");

  if (mTask) {
  return NS_OK;

NS_IMPL_ISUPPORTS(MessageLoopTimerCallback, nsITimerCallback, nsINamed)

}  // namespace

NS_IMPL_ISUPPORTS(nsMessageLoop, nsIMessageLoop)

nsMessageLoop::PostIdleTask(nsIRunnable* aTask, uint32_t aEnsureRunsAfterMS) {
  // The message loop owns MessageLoopIdleTask and deletes it after calling
  // Run().  Be careful...
  RefPtr<MessageLoopIdleTask> idle =
      new MessageLoopIdleTask(aTask, aEnsureRunsAfterMS);

  return NS_OK;

nsresult nsMessageLoopConstructor(nsISupports* aOuter, const nsIID& aIID,
                                  void** aInstancePtr) {
  if (NS_WARN_IF(aOuter)) {
  nsISupports* messageLoop = new nsMessageLoop();
  return messageLoop->QueryInterface(aIID, aInstancePtr);