1 // Copyright (c) 2016 Marshall A. Greenblatt. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google Inc. nor the name Chromium Embedded 14 // Framework nor the names of its contributors may be used to endorse 15 // or promote products derived from this software without specific prior 16 // written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // --------------------------------------------------------------------------- 31 // 32 // The contents of this file must follow a specific format in order to 33 // support the CEF translator tool. See the translator.README.txt file in the 34 // tools directory for more information. 35 // 36 37 #ifndef CEF_INCLUDE_CEF_THREAD_H_ 38 #define CEF_INCLUDE_CEF_THREAD_H_ 39 #pragma once 40 41 #include "include/cef_task.h" 42 #include "include/internal/cef_thread_internal.h" 43 44 /// 45 // A simple thread abstraction that establishes a message loop on a new thread. 46 // The consumer uses CefTaskRunner to execute code on the thread's message loop. 47 // The thread is terminated when the CefThread object is destroyed or Stop() is 48 // called. All pending tasks queued on the thread's message loop will run to 49 // completion before the thread is terminated. CreateThread() can be called on 50 // any valid CEF thread in either the browser or render process. This class 51 // should only be used for tasks that require a dedicated thread. In most cases 52 // you can post tasks to an existing CEF thread instead of creating a new one; 53 // see cef_task.h for details. 54 /// 55 /*--cef(source=library)--*/ 56 class CefThread : public CefBaseRefCounted { 57 public: 58 /// 59 // Create and start a new thread. This method does not block waiting for the 60 // thread to run initialization. |display_name| is the name that will be used 61 // to identify the thread. |priority| is the thread execution priority. 62 // |message_loop_type| indicates the set of asynchronous events that the 63 // thread can process. If |stoppable| is true the thread will stopped and 64 // joined on destruction or when Stop() is called; otherwise, the thread 65 // cannot be stopped and will be leaked on shutdown. On Windows the 66 // |com_init_mode| value specifies how COM will be initialized for the thread. 67 // If |com_init_mode| is set to COM_INIT_MODE_STA then |message_loop_type| 68 // must be set to ML_TYPE_UI. 69 /// 70 /*--cef(optional_param=display_name)--*/ 71 static CefRefPtr<CefThread> CreateThread( 72 const CefString& display_name, 73 cef_thread_priority_t priority, 74 cef_message_loop_type_t message_loop_type, 75 bool stoppable, 76 cef_com_init_mode_t com_init_mode); 77 78 /// 79 // Create and start a new thread with default/recommended values. 80 // |display_name| is the name that will be used to identify the thread. 81 /// CreateThread(const CefString & display_name)82 static CefRefPtr<CefThread> CreateThread(const CefString& display_name) { 83 return CreateThread(display_name, TP_NORMAL, ML_TYPE_DEFAULT, true, 84 COM_INIT_MODE_NONE); 85 } 86 87 /// 88 // Returns the CefTaskRunner that will execute code on this thread's message 89 // loop. This method is safe to call from any thread. 90 /// 91 /*--cef()--*/ 92 virtual CefRefPtr<CefTaskRunner> GetTaskRunner() = 0; 93 94 /// 95 // Returns the platform thread ID. It will return the same value after Stop() 96 // is called. This method is safe to call from any thread. 97 /// 98 /*--cef(default_retval=kInvalidPlatformThreadId)--*/ 99 virtual cef_platform_thread_id_t GetPlatformThreadId() = 0; 100 101 /// 102 // Stop and join the thread. This method must be called from the same thread 103 // that called CreateThread(). Do not call this method if CreateThread() was 104 // called with a |stoppable| value of false. 105 /// 106 /*--cef()--*/ 107 virtual void Stop() = 0; 108 109 /// 110 // Returns true if the thread is currently running. This method must be called 111 // from the same thread that called CreateThread(). 112 /// 113 /*--cef()--*/ 114 virtual bool IsRunning() = 0; 115 }; 116 117 #endif // CEF_INCLUDE_CEF_THREAD_H_ 118