tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MessagePump_mac.mm (3270B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "MessagePump.h"
      8 
      9 #include <Foundation/Foundation.h>
     10 
     11 #include "base/scoped_nsautorelease_pool.h"
     12 #include "mozilla/ProfilerMarkers.h"
     13 #include "nsISupportsImpl.h"
     14 
     15 using namespace mozilla::ipc;
     16 
     17 static void NoOp(void* info) {}
     18 
     19 NS_IMPL_ADDREF_INHERITED(MessagePumpForNonMainUIThreads, MessagePump)
     20 NS_IMPL_RELEASE_INHERITED(MessagePumpForNonMainUIThreads, MessagePump)
     21 NS_IMPL_QUERY_INTERFACE(MessagePumpForNonMainUIThreads, nsIThreadObserver)
     22 
     23 MessagePumpForNonMainUIThreads::MessagePumpForNonMainUIThreads(
     24    nsISerialEventTarget* aEventTarget)
     25    : mEventTarget(aEventTarget), keep_running_(true) {
     26  MOZ_ASSERT(mEventTarget);
     27  CFRunLoopSourceContext source_context = CFRunLoopSourceContext();
     28  source_context.perform = NoOp;
     29  quit_source_ = CFRunLoopSourceCreate(nullptr,  // allocator
     30                                       0,        // priority
     31                                       &source_context);
     32  CFRunLoopAddSource(run_loop(), quit_source_, kCFRunLoopCommonModes);
     33 }
     34 
     35 MessagePumpForNonMainUIThreads::~MessagePumpForNonMainUIThreads() {
     36  CFRunLoopRemoveSource(run_loop(), quit_source_, kCFRunLoopCommonModes);
     37  CFRelease(quit_source_);
     38 }
     39 
     40 void MessagePumpForNonMainUIThreads::DoRun(
     41    base::MessagePump::Delegate* aDelegate) {
     42  // If this is a chromium thread and no nsThread is associated with it, this
     43  // call will create a new nsThread.
     44  nsIThread* thread = NS_GetCurrentThread();
     45  MOZ_ASSERT(thread);
     46 
     47  // Set the main thread observer so we can wake up when xpcom events need to
     48  // get processed.
     49  nsCOMPtr<nsIThreadInternal> ti(do_QueryInterface(thread));
     50  MOZ_ASSERT(ti);
     51  ti->SetObserver(this);
     52 
     53  base::ScopedNSAutoreleasePool autoReleasePool;
     54 
     55  while (keep_running_) {
     56    // Drain the xpcom event loop first.
     57    if (NS_ProcessNextEvent(nullptr, false)) {
     58      continue;
     59    }
     60 
     61    autoReleasePool.Recycle();
     62 
     63    if (!keep_running_) {
     64      break;
     65    }
     66 
     67    // Now process the CFRunLoop. It exits after running once.
     68    // NSRunLoop manages autorelease pools itself.
     69    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
     70                             beforeDate:[NSDate distantFuture]];
     71  }
     72 
     73  ti->SetObserver(nullptr);
     74  keep_running_ = true;
     75 }
     76 
     77 void MessagePumpForNonMainUIThreads::Quit() {
     78  keep_running_ = false;
     79  CFRunLoopSourceSignal(quit_source_);
     80  CFRunLoopWakeUp(run_loop());
     81 }
     82 
     83 NS_IMETHODIMP MessagePumpForNonMainUIThreads::OnDispatchedEvent() {
     84  // ScheduleWork will signal an input source to the run loop, making it exit so
     85  // it can process the xpcom event.
     86  ScheduleWork();
     87  return NS_OK;
     88 }
     89 
     90 NS_IMETHODIMP
     91 MessagePumpForNonMainUIThreads::OnProcessNextEvent(nsIThreadInternal* thread,
     92                                                   bool mayWait) {
     93  return NS_OK;
     94 }
     95 
     96 NS_IMETHODIMP
     97 MessagePumpForNonMainUIThreads::AfterProcessNextEvent(nsIThreadInternal* thread,
     98                                                      bool eventWasProcessed) {
     99  return NS_OK;
    100 }