security/sandbox/chromium/base/run_loop.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/run_loop.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef BASE_RUN_LOOP_H_
     1.9 +#define BASE_RUN_LOOP_H_
    1.10 +
    1.11 +#include "base/base_export.h"
    1.12 +#include "base/callback.h"
    1.13 +#include "base/memory/weak_ptr.h"
    1.14 +#include "base/message_loop/message_loop.h"
    1.15 +
    1.16 +namespace base {
    1.17 +#if defined(OS_ANDROID)
    1.18 +class MessagePumpForUI;
    1.19 +#endif
    1.20 +
    1.21 +#if defined(OS_IOS)
    1.22 +class MessagePumpUIApplication;
    1.23 +#endif
    1.24 +
    1.25 +// Helper class to Run a nested MessageLoop. Please do not use nested
    1.26 +// MessageLoops in production code! If you must, use this class instead of
    1.27 +// calling MessageLoop::Run/Quit directly. RunLoop::Run can only be called once
    1.28 +// per RunLoop lifetime. Create a RunLoop on the stack and call Run/Quit to run
    1.29 +// a nested MessageLoop.
    1.30 +class BASE_EXPORT RunLoop {
    1.31 + public:
    1.32 +  RunLoop();
    1.33 +#if !defined(OS_MACOSX) && !defined(OS_ANDROID) && \
    1.34 +    !defined(USE_GTK_MESSAGE_PUMP)
    1.35 +  explicit RunLoop(MessageLoop::Dispatcher* dispatcher);
    1.36 +#endif
    1.37 +  ~RunLoop();
    1.38 +
    1.39 +#if !defined(OS_MACOSX) && !defined(OS_ANDROID) && \
    1.40 +    !defined(USE_GTK_MESSAGE_PUMP)
    1.41 +  void set_dispatcher(MessageLoop::Dispatcher* dispatcher) {
    1.42 +    dispatcher_ = dispatcher;
    1.43 +  }
    1.44 +#endif
    1.45 +
    1.46 +  // Run the current MessageLoop. This blocks until Quit is called. Before
    1.47 +  // calling Run, be sure to grab an AsWeakPtr or the QuitClosure in order to
    1.48 +  // stop the MessageLoop asynchronously. MessageLoop::Quit and QuitNow will
    1.49 +  // also trigger a return from Run, but those are deprecated.
    1.50 +  void Run();
    1.51 +
    1.52 +  // Run the current MessageLoop until it doesn't find any tasks or messages in
    1.53 +  // the queue (it goes idle). WARNING: This may never return! Only use this
    1.54 +  // when repeating tasks such as animated web pages have been shut down.
    1.55 +  void RunUntilIdle();
    1.56 +
    1.57 +  bool running() const { return running_; }
    1.58 +
    1.59 +  // Quit an earlier call to Run(). There can be other nested RunLoops servicing
    1.60 +  // the same task queue (MessageLoop); Quitting one RunLoop has no bearing on
    1.61 +  // the others. Quit can be called before, during or after Run. If called
    1.62 +  // before Run, Run will return immediately when called. Calling Quit after the
    1.63 +  // RunLoop has already finished running has no effect.
    1.64 +  //
    1.65 +  // WARNING: You must NEVER assume that a call to Quit will terminate the
    1.66 +  // targetted message loop. If a nested message loop continues running, the
    1.67 +  // target may NEVER terminate. It is very easy to livelock (run forever) in
    1.68 +  // such a case.
    1.69 +  void Quit();
    1.70 +
    1.71 +  // Convenience method to get a closure that safely calls Quit (has no effect
    1.72 +  // if the RunLoop instance is gone).
    1.73 +  //
    1.74 +  // Example:
    1.75 +  //   RunLoop run_loop;
    1.76 +  //   PostTask(run_loop.QuitClosure());
    1.77 +  //   run_loop.Run();
    1.78 +  base::Closure QuitClosure();
    1.79 +
    1.80 + private:
    1.81 +  friend class MessageLoop;
    1.82 +#if defined(OS_ANDROID)
    1.83 +  // Android doesn't support the blocking MessageLoop::Run, so it calls
    1.84 +  // BeforeRun and AfterRun directly.
    1.85 +  friend class base::MessagePumpForUI;
    1.86 +#endif
    1.87 +
    1.88 +#if defined(OS_IOS)
    1.89 +  // iOS doesn't support the blocking MessageLoop::Run, so it calls
    1.90 +  // BeforeRun directly.
    1.91 +  friend class base::MessagePumpUIApplication;
    1.92 +#endif
    1.93 +
    1.94 +  // Return false to abort the Run.
    1.95 +  bool BeforeRun();
    1.96 +  void AfterRun();
    1.97 +
    1.98 +  MessageLoop* loop_;
    1.99 +
   1.100 +  // WeakPtrFactory for QuitClosure safety.
   1.101 +  base::WeakPtrFactory<RunLoop> weak_factory_;
   1.102 +
   1.103 +  // Parent RunLoop or NULL if this is the top-most RunLoop.
   1.104 +  RunLoop* previous_run_loop_;
   1.105 +
   1.106 +#if !defined(OS_MACOSX) && !defined(OS_ANDROID) && \
   1.107 +    !defined(USE_GTK_MESSAGE_PUMP)
   1.108 +  MessageLoop::Dispatcher* dispatcher_;
   1.109 +#endif
   1.110 +
   1.111 +  // Used to count how many nested Run() invocations are on the stack.
   1.112 +  int run_depth_;
   1.113 +
   1.114 +  bool run_called_;
   1.115 +  bool quit_called_;
   1.116 +  bool running_;
   1.117 +
   1.118 +  // Used to record that QuitWhenIdle() was called on the MessageLoop, meaning
   1.119 +  // that we should quit Run once it becomes idle.
   1.120 +  bool quit_when_idle_received_;
   1.121 +
   1.122 +  DISALLOW_COPY_AND_ASSIGN(RunLoop);
   1.123 +};
   1.124 +
   1.125 +}  // namespace base
   1.126 +
   1.127 +#endif  // BASE_RUN_LOOP_H_

mercurial