• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
45   CefDevToolsController(const CefDevToolsController&) = delete;
46   CefDevToolsController& operator=(const CefDevToolsController&) = delete;
47 
48   ~CefDevToolsController() override;
49 
50   // See CefBrowserHost methods of the same name for documentation.
51   bool SendDevToolsMessage(const base::StringPiece& message);
52   int ExecuteDevToolsMethod(int message_id,
53                             const std::string& method,
54                             const base::DictionaryValue* params);
55 
56   // |observer| must outlive this object or be removed.
57   void AddObserver(Observer* observer);
58   void RemoveObserver(Observer* observer);
59 
GetWeakPtr()60   base::WeakPtr<CefDevToolsController> GetWeakPtr() {
61     return weak_ptr_factory_.GetWeakPtr();
62   }
63 
64  private:
65   // content::DevToolsAgentHostClient implementation:
66   void AgentHostClosed(content::DevToolsAgentHost* agent_host) override;
67   void DispatchProtocolMessage(content::DevToolsAgentHost* agent_host,
68                                base::span<const uint8_t> message) override;
69 
70   bool EnsureAgentHost();
71 
72   content::WebContents* const inspected_contents_;
73   scoped_refptr<content::DevToolsAgentHost> agent_host_;
74   int next_message_id_ = 1;
75 
76   base::ObserverList<Observer> observers_;
77 
78   base::WeakPtrFactory<CefDevToolsController> weak_ptr_factory_;
79 };
80 
81 #endif  // CEF_LIBCEF_BROWSER_DEVTOOLS_DEVTOOLS_CONTROLLER_H_
82