1 // Copyright (c) 2020 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_MANAGER_H_ 6 #define CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_MANAGER_H_ 7 #pragma once 8 9 #include "include/cef_browser.h" 10 11 #include "base/memory/weak_ptr.h" 12 13 class CefBrowserHostBase; 14 class CefDevToolsController; 15 class CefDevToolsFrontend; 16 17 namespace content { 18 class WebContents; 19 } 20 21 // Manages DevTools instances. Methods must be called on the UI thread unless 22 // otherwise indicated. 23 class CefDevToolsManager { 24 public: 25 // |inspected_browser| will outlive this object. 26 explicit CefDevToolsManager(CefBrowserHostBase* inspected_browser); 27 28 CefDevToolsManager(const CefDevToolsManager&) = delete; 29 CefDevToolsManager& operator=(const CefDevToolsManager&) = delete; 30 31 ~CefDevToolsManager(); 32 33 // See CefBrowserHost methods of the same name for documentation. 34 void ShowDevTools(const CefWindowInfo& windowInfo, 35 CefRefPtr<CefClient> client, 36 const CefBrowserSettings& settings, 37 const CefPoint& inspect_element_at); 38 void CloseDevTools(); 39 bool HasDevTools(); 40 bool SendDevToolsMessage(const void* message, size_t message_size); 41 int ExecuteDevToolsMethod(int message_id, 42 const CefString& method, 43 CefRefPtr<CefDictionaryValue> param); 44 45 // These methods are used to implement 46 // CefBrowserHost::AddDevToolsMessageObserver. CreateRegistration is safe to 47 // call on any thread. InitializeRegistrationOnUIThread should be called 48 // immediately afterwards on the UI thread. 49 static CefRefPtr<CefRegistration> CreateRegistration( 50 CefRefPtr<CefDevToolsMessageObserver> observer); 51 void InitializeRegistrationOnUIThread( 52 CefRefPtr<CefRegistration> registration); 53 54 private: 55 void OnFrontEndDestroyed(); 56 57 bool EnsureController(); 58 59 CefBrowserHostBase* const inspected_browser_; 60 61 // CefDevToolsFrontend will delete itself when the frontend WebContents is 62 // destroyed. 63 CefDevToolsFrontend* devtools_frontend_ = nullptr; 64 65 // Used for sending DevTools protocol messages without an active frontend. 66 std::unique_ptr<CefDevToolsController> devtools_controller_; 67 68 base::WeakPtrFactory<CefDevToolsManager> weak_ptr_factory_; 69 }; 70 71 #endif // CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_MANAGER_H_ 72