xpcom/base/nsMessageLoop.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsMessageLoop.h"
michael@0 7 #include "mozilla/WeakPtr.h"
michael@0 8 #include "base/message_loop.h"
michael@0 9 #include "base/task.h"
michael@0 10 #include "nsIRunnable.h"
michael@0 11 #include "nsITimer.h"
michael@0 12 #include "nsCOMPtr.h"
michael@0 13 #include "nsAutoPtr.h"
michael@0 14 #include "nsComponentManagerUtils.h"
michael@0 15 #include "nsThreadUtils.h"
michael@0 16
michael@0 17 using namespace mozilla;
michael@0 18
michael@0 19 namespace {
michael@0 20
michael@0 21 /**
michael@0 22 * This Task runs its nsIRunnable when Run() is called, or after
michael@0 23 * aEnsureRunsAfterMS milliseconds have elapsed since the object was
michael@0 24 * constructed.
michael@0 25 *
michael@0 26 * Note that the MessageLoop owns this object and will delete it after it calls
michael@0 27 * Run(). Tread lightly.
michael@0 28 */
michael@0 29 class MessageLoopIdleTask
michael@0 30 : public Task
michael@0 31 , public SupportsWeakPtr<MessageLoopIdleTask>
michael@0 32 {
michael@0 33 public:
michael@0 34 MOZ_DECLARE_REFCOUNTED_TYPENAME(MessageLoopIdleTask)
michael@0 35 MessageLoopIdleTask(nsIRunnable* aTask, uint32_t aEnsureRunsAfterMS);
michael@0 36 virtual ~MessageLoopIdleTask() {}
michael@0 37 virtual void Run();
michael@0 38
michael@0 39 private:
michael@0 40 nsresult Init(uint32_t aEnsureRunsAfterMS);
michael@0 41
michael@0 42 nsCOMPtr<nsIRunnable> mTask;
michael@0 43 nsCOMPtr<nsITimer> mTimer;
michael@0 44 };
michael@0 45
michael@0 46 /**
michael@0 47 * This timer callback calls MessageLoopIdleTask::Run() when its timer fires.
michael@0 48 * (The timer can't call back into MessageLoopIdleTask directly since that's
michael@0 49 * not a refcounted object; it's owned by the MessageLoop.)
michael@0 50 *
michael@0 51 * We keep a weak reference to the MessageLoopIdleTask, although a raw pointer
michael@0 52 * should in theory suffice: When the MessageLoopIdleTask runs (right before
michael@0 53 * the MessageLoop deletes it), it cancels its timer. But the weak pointer
michael@0 54 * saves us from worrying about an edge case somehow messing us up here.
michael@0 55 */
michael@0 56 class MessageLoopTimerCallback
michael@0 57 : public nsITimerCallback
michael@0 58 {
michael@0 59 public:
michael@0 60 MessageLoopTimerCallback(MessageLoopIdleTask* aTask);
michael@0 61 virtual ~MessageLoopTimerCallback() {};
michael@0 62
michael@0 63 NS_DECL_ISUPPORTS
michael@0 64 NS_DECL_NSITIMERCALLBACK
michael@0 65
michael@0 66 private:
michael@0 67 WeakPtr<MessageLoopIdleTask> mTask;
michael@0 68 };
michael@0 69
michael@0 70 MessageLoopIdleTask::MessageLoopIdleTask(nsIRunnable* aTask,
michael@0 71 uint32_t aEnsureRunsAfterMS)
michael@0 72 : mTask(aTask)
michael@0 73 {
michael@0 74 // Init() really shouldn't fail, but if it does, we schedule our runnable
michael@0 75 // immediately, because it's more important to guarantee that we run the task
michael@0 76 // eventually than it is to run the task when we're idle.
michael@0 77 nsresult rv = Init(aEnsureRunsAfterMS);
michael@0 78 if (NS_FAILED(rv)) {
michael@0 79 NS_WARNING("Running idle task early because we couldn't initialize our timer.");
michael@0 80 NS_DispatchToCurrentThread(mTask);
michael@0 81
michael@0 82 mTask = nullptr;
michael@0 83 mTimer = nullptr;
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 nsresult
michael@0 88 MessageLoopIdleTask::Init(uint32_t aEnsureRunsAfterMS)
michael@0 89 {
michael@0 90 mTimer = do_CreateInstance("@mozilla.org/timer;1");
michael@0 91 if (NS_WARN_IF(!mTimer))
michael@0 92 return NS_ERROR_UNEXPECTED;
michael@0 93
michael@0 94 nsRefPtr<MessageLoopTimerCallback> callback =
michael@0 95 new MessageLoopTimerCallback(this);
michael@0 96
michael@0 97 return mTimer->InitWithCallback(callback, aEnsureRunsAfterMS,
michael@0 98 nsITimer::TYPE_ONE_SHOT);
michael@0 99 }
michael@0 100
michael@0 101 /* virtual */ void
michael@0 102 MessageLoopIdleTask::Run()
michael@0 103 {
michael@0 104 // Null out our pointers because if Run() was called by the timer, this
michael@0 105 // object will be kept alive by the MessageLoop until the MessageLoop calls
michael@0 106 // Run().
michael@0 107
michael@0 108 if (mTimer) {
michael@0 109 mTimer->Cancel();
michael@0 110 mTimer = nullptr;
michael@0 111 }
michael@0 112
michael@0 113 if (mTask) {
michael@0 114 mTask->Run();
michael@0 115 mTask = nullptr;
michael@0 116 }
michael@0 117 }
michael@0 118
michael@0 119 MessageLoopTimerCallback::MessageLoopTimerCallback(MessageLoopIdleTask* aTask)
michael@0 120 : mTask(aTask->asWeakPtr())
michael@0 121 {}
michael@0 122
michael@0 123 NS_IMETHODIMP
michael@0 124 MessageLoopTimerCallback::Notify(nsITimer* aTimer)
michael@0 125 {
michael@0 126 // We don't expect to hit the case when the timer fires but mTask has been
michael@0 127 // deleted, because mTask should cancel the timer before the mTask is
michael@0 128 // deleted. But you never know...
michael@0 129 NS_WARN_IF_FALSE(mTask, "This timer shouldn't have fired.");
michael@0 130
michael@0 131 if (mTask) {
michael@0 132 mTask->Run();
michael@0 133 }
michael@0 134 return NS_OK;
michael@0 135 }
michael@0 136
michael@0 137 NS_IMPL_ISUPPORTS(MessageLoopTimerCallback, nsITimerCallback)
michael@0 138
michael@0 139 } // anonymous namespace
michael@0 140
michael@0 141 NS_IMPL_ISUPPORTS(nsMessageLoop, nsIMessageLoop)
michael@0 142
michael@0 143 NS_IMETHODIMP
michael@0 144 nsMessageLoop::PostIdleTask(nsIRunnable* aTask, uint32_t aEnsureRunsAfterMS)
michael@0 145 {
michael@0 146 // The message loop owns MessageLoopIdleTask and deletes it after calling
michael@0 147 // Run(). Be careful...
michael@0 148 MessageLoop::current()->PostIdleTask(FROM_HERE,
michael@0 149 new MessageLoopIdleTask(aTask, aEnsureRunsAfterMS));
michael@0 150 return NS_OK;
michael@0 151 }
michael@0 152
michael@0 153 nsresult
michael@0 154 nsMessageLoopConstructor(nsISupports* aOuter,
michael@0 155 const nsIID& aIID,
michael@0 156 void** aInstancePtr)
michael@0 157 {
michael@0 158 if (NS_WARN_IF(aOuter))
michael@0 159 return NS_ERROR_NO_AGGREGATION;
michael@0 160 nsISupports* messageLoop = new nsMessageLoop();
michael@0 161 return messageLoop->QueryInterface(aIID, aInstancePtr);
michael@0 162 }

mercurial