|
| 1 | +// Copied from upstream CEF with minor modifications for PHP Desktop |
| 2 | +// purposes. |
| 3 | + |
| 4 | +// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights |
| 5 | +// reserved. Use of this source code is governed by a BSD-style license that |
| 6 | +// can be found in the LICENSE file. |
| 7 | + |
| 8 | +#ifndef CEF_TESTS_SHARED_BROWSER_MAIN_MESSAGE_LOOP_H_ |
| 9 | +#define CEF_TESTS_SHARED_BROWSER_MAIN_MESSAGE_LOOP_H_ |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include "include/base/cef_bind.h" |
| 13 | +#include "include/base/cef_scoped_ptr.h" |
| 14 | +#include "include/cef_task.h" |
| 15 | + |
| 16 | +#if defined(OS_WIN) |
| 17 | +#include <windows.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +// Represents the message loop running on the main application thread in the |
| 21 | +// browser process. This will be the same as the CEF UI thread on Linux, OS X |
| 22 | +// and Windows when not using multi-threaded message loop mode. The methods of |
| 23 | +// this class are thread-safe unless otherwise indicated. |
| 24 | +class MainMessageLoop { |
| 25 | + public: |
| 26 | + // Returns the singleton instance of this object. |
| 27 | + static MainMessageLoop* Get(); |
| 28 | + |
| 29 | + // Run the message loop. The thread that this method is called on will be |
| 30 | + // considered the main thread. This blocks until Quit() is called. |
| 31 | + virtual int Run() = 0; |
| 32 | + |
| 33 | + // Quit the message loop. |
| 34 | + virtual void Quit() = 0; |
| 35 | + |
| 36 | + // Post a task for execution on the main message loop. |
| 37 | + virtual void PostTask(CefRefPtr<CefTask> task) = 0; |
| 38 | + |
| 39 | + // Returns true if this message loop runs tasks on the current thread. |
| 40 | + virtual bool RunsTasksOnCurrentThread() const = 0; |
| 41 | + |
| 42 | +#if defined(OS_WIN) |
| 43 | + // Set the current modeless dialog on Windows for proper delivery of dialog |
| 44 | + // messages when using multi-threaded message loop mode. This method must be |
| 45 | + // called from the main thread. See http://support.microsoft.com/kb/71450 for |
| 46 | + // background. |
| 47 | + virtual void SetCurrentModelessDialog(HWND hWndDialog) = 0; |
| 48 | +#endif |
| 49 | + |
| 50 | + // Post a closure for execution on the main message loop. |
| 51 | + void PostClosure(const base::Closure& closure); |
| 52 | + |
| 53 | + protected: |
| 54 | + // Only allow deletion via scoped_ptr. |
| 55 | + friend struct base::DefaultDeleter<MainMessageLoop>; |
| 56 | + |
| 57 | + MainMessageLoop(); |
| 58 | + virtual ~MainMessageLoop(); |
| 59 | + |
| 60 | + private: |
| 61 | + DISALLOW_COPY_AND_ASSIGN(MainMessageLoop); |
| 62 | +}; |
| 63 | + |
| 64 | +#define CURRENTLY_ON_MAIN_THREAD() \ |
| 65 | + MainMessageLoop::Get()->RunsTasksOnCurrentThread() |
| 66 | + |
| 67 | +#define REQUIRE_MAIN_THREAD() DCHECK(CURRENTLY_ON_MAIN_THREAD()) |
| 68 | + |
| 69 | +#define MAIN_POST_TASK(task) MainMessageLoop::Get()->PostTask(task) |
| 70 | + |
| 71 | +#define MAIN_POST_CLOSURE(closure) \ |
| 72 | + MainMessageLoop::Get()->PostClosure(closure) |
| 73 | + |
| 74 | +// Use this struct in conjuction with RefCountedThreadSafe to ensure that an |
| 75 | +// object is deleted on the main thread. For example: |
| 76 | +// |
| 77 | +// class Foo : public base::RefCountedThreadSafe<Foo, DeleteOnMainThread> { |
| 78 | +// public: |
| 79 | +// Foo(); |
| 80 | +// void DoSomething(); |
| 81 | +// |
| 82 | +// private: |
| 83 | +// // Allow deletion via scoped_refptr only. |
| 84 | +// friend struct DeleteOnMainThread; |
| 85 | +// friend class base::RefCountedThreadSafe<Foo, DeleteOnMainThread>; |
| 86 | +// |
| 87 | +// virtual ~Foo() {} |
| 88 | +// }; |
| 89 | +// |
| 90 | +// base::scoped_refptr<Foo> foo = new Foo(); |
| 91 | +// foo->DoSomething(); |
| 92 | +// foo = NULL; // Deletion of |foo| will occur on the main thread. |
| 93 | +// |
| 94 | +struct DeleteOnMainThread { |
| 95 | + template <typename T> |
| 96 | + static void Destruct(const T* x) { |
| 97 | + if (CURRENTLY_ON_MAIN_THREAD()) { |
| 98 | + delete x; |
| 99 | + } else { |
| 100 | + MainMessageLoop::Get()->PostClosure( |
| 101 | + base::Bind(&DeleteOnMainThread::Destruct<T>, x)); |
| 102 | + } |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +#endif // CEF_TESTS_SHARED_BROWSER_MAIN_MESSAGE_LOOP_H_ |
0 commit comments