1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that can 3 // be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_CONTEXT_H_ 6 #define CEF_LIBCEF_BROWSER_CONTEXT_H_ 7 #pragma once 8 9 #include <list> 10 #include <map> 11 #include <string> 12 13 #include "include/cef_app.h" 14 #include "libcef/browser/main_runner.h" 15 16 #include "base/observer_list.h" 17 #include "base/threading/platform_thread.h" 18 #include "third_party/skia/include/core/SkColor.h" 19 20 class CefBrowserInfoManager; 21 class CefTraceSubscriber; 22 23 class CefContext { 24 public: 25 // Interface to implement for observers that wish to be informed of changes 26 // to the context. All methods will be called on the UI thread. 27 class Observer { 28 public: 29 // Called before the context is destroyed. 30 virtual void OnContextDestroyed() = 0; 31 32 protected: ~Observer()33 virtual ~Observer() {} 34 }; 35 36 CefContext(); 37 ~CefContext(); 38 39 // Returns the singleton CefContext instance. 40 static CefContext* Get(); 41 42 // These methods will be called on the main application thread. 43 bool Initialize(const CefMainArgs& args, 44 const CefSettings& settings, 45 CefRefPtr<CefApp> application, 46 void* windows_sandbox_info); 47 void RunMessageLoop(); 48 void QuitMessageLoop(); 49 void Shutdown(); 50 51 // Returns true if the current thread is the initialization thread. 52 bool OnInitThread(); 53 54 // Returns true if the context is initialized. initialized()55 bool initialized() { return initialized_; } 56 57 // Returns true if the context is shutting down. shutting_down()58 bool shutting_down() { return shutting_down_; } 59 settings()60 const CefSettings& settings() const { return settings_; } 61 62 // Returns the background color for the browser. If |browser_settings| is 63 // nullptr or does not specify a color then the global settings will be used. 64 // The alpha component will be either SK_AlphaTRANSPARENT or SK_AlphaOPAQUE 65 // (e.g. fully transparent or fully opaque). If |is_windowless| is 66 // STATE_DISABLED then SK_AlphaTRANSPARENT will always be returned. If 67 // |is_windowless| is STATE_ENABLED then SK_ColorTRANSPARENT may be returned 68 // to enable transparency for windowless browsers. See additional comments on 69 // CefSettings.background_color and CefBrowserSettings.background_color. 70 SkColor GetBackgroundColor(const CefBrowserSettings* browser_settings, 71 cef_state_t windowless_state) const; 72 73 CefTraceSubscriber* GetTraceSubscriber(); 74 75 // Populate request context settings for the global system context based on 76 // CefSettings and command-line flags. 77 void PopulateGlobalRequestContextSettings( 78 CefRequestContextSettings* settings); 79 80 // Normalize and validate request context settings for user-created contexts. 81 void NormalizeRequestContextSettings(CefRequestContextSettings* settings); 82 83 // Manage observer objects. The observer must either outlive this object or 84 // remove itself before destruction. These methods can only be called on the 85 // UI thread. 86 void AddObserver(Observer* observer); 87 void RemoveObserver(Observer* observer); 88 bool HasObserver(Observer* observer) const; 89 90 private: 91 void OnContextInitialized(); 92 93 // Performs shutdown actions that need to occur on the UI thread before any 94 // threads are destroyed. 95 void ShutdownOnUIThread(); 96 97 // Destroys the main runner and related objects. 98 void FinalizeShutdown(); 99 100 // Track context state. 101 bool initialized_; 102 bool shutting_down_; 103 104 // The thread on which the context was initialized. 105 base::PlatformThreadId init_thread_id_; 106 107 CefSettings settings_; 108 CefRefPtr<CefApp> application_; 109 110 std::unique_ptr<CefMainRunner> main_runner_; 111 std::unique_ptr<CefTraceSubscriber> trace_subscriber_; 112 std::unique_ptr<CefBrowserInfoManager> browser_info_manager_; 113 114 // Observers that want to be notified of changes to this object. 115 base::ObserverList<Observer>::Unchecked observers_; 116 }; 117 118 // Helper macro that returns true if the global context is in a valid state. 119 #define CONTEXT_STATE_VALID() \ 120 (CefContext::Get() && CefContext::Get()->initialized() && \ 121 !CefContext::Get()->shutting_down()) 122 123 #endif // CEF_LIBCEF_BROWSER_CONTEXT_H_ 124