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_CONTROLLER_H_ 6 #define CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_CONTROLLER_H_ 7 8 #include <memory> 9 10 #include "content/public/browser/devtools_agent_host_client.h" 11 12 #include "base/containers/span.h" 13 #include "base/memory/scoped_refptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/observer_list.h" 16 #include "base/values.h" 17 18 namespace content { 19 class WebContents; 20 } 21 22 class CefDevToolsController : public content::DevToolsAgentHostClient { 23 public: 24 class Observer : public base::CheckedObserver { 25 public: 26 // See CefDevToolsMessageObserver documentation. 27 virtual bool OnDevToolsMessage(const base::StringPiece& message) = 0; 28 virtual void OnDevToolsMethodResult(int message_id, 29 bool success, 30 const base::StringPiece& result) = 0; 31 virtual void OnDevToolsEvent(const base::StringPiece& method, 32 const base::StringPiece& params) = 0; 33 virtual void OnDevToolsAgentAttached() = 0; 34 virtual void OnDevToolsAgentDetached() = 0; 35 36 virtual void OnDevToolsControllerDestroyed() = 0; 37 38 protected: ~Observer()39 ~Observer() override {} 40 }; 41 42 // |inspected_contents| will outlive this object. 43 explicit CefDevToolsController(content::WebContents* inspected_contents); 44 ~CefDevToolsController() override; 45 46 // See CefBrowserHost methods of the same name for documentation. 47 bool SendDevToolsMessage(const base::StringPiece& message); 48 int ExecuteDevToolsMethod(int message_id, 49 const std::string& method, 50 const base::DictionaryValue* params); 51 52 // |observer| must outlive this object or be removed. 53 void AddObserver(Observer* observer); 54 void RemoveObserver(Observer* observer); 55 GetWeakPtr()56 base::WeakPtr<CefDevToolsController> GetWeakPtr() { 57 return weak_ptr_factory_.GetWeakPtr(); 58 } 59 60 private: 61 // content::DevToolsAgentHostClient implementation: 62 void AgentHostClosed(content::DevToolsAgentHost* agent_host) override; 63 void DispatchProtocolMessage(content::DevToolsAgentHost* agent_host, 64 base::span<const uint8_t> message) override; 65 66 bool EnsureAgentHost(); 67 68 content::WebContents* const inspected_contents_; 69 scoped_refptr<content::DevToolsAgentHost> agent_host_; 70 int next_message_id_ = 1; 71 72 base::ObserverList<Observer> observers_; 73 74 base::WeakPtrFactory<CefDevToolsController> weak_ptr_factory_; 75 76 DISALLOW_COPY_AND_ASSIGN(CefDevToolsController); 77 }; 78 79 #endif // CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_CONTROLLER_H_ 80