1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_UI_BINDINGS_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_UI_BINDINGS_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/strings/string16.h"
15 #include "chrome/browser/devtools/device/devtools_android_bridge.h"
16 #include "chrome/browser/devtools/devtools_embedder_message_dispatcher.h"
17 #include "chrome/browser/devtools/devtools_file_helper.h"
18 #include "chrome/browser/devtools/devtools_file_system_indexer.h"
19 #include "chrome/browser/devtools/devtools_targets_ui.h"
20 #include "content/public/browser/devtools_agent_host.h"
21 #include "content/public/browser/devtools_frontend_host.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "net/url_request/url_fetcher_delegate.h"
25 #include "ui/gfx/geometry/size.h"
31 struct FileChooserParams
;
35 // Base implementation of DevTools bindings around front-end.
36 class DevToolsUIBindings
: public content::NotificationObserver
,
37 public content::DevToolsFrontendHost::Delegate
,
38 public DevToolsEmbedderMessageDispatcher::Delegate
,
39 public DevToolsAndroidBridge::DeviceCountListener
,
40 public content::DevToolsAgentHostClient
,
41 public net::URLFetcherDelegate
{
43 static DevToolsUIBindings
* ForWebContents(
44 content::WebContents
* web_contents
);
45 static GURL
ApplyThemeToURL(Profile
* profile
, const GURL
& base_url
);
49 virtual ~Delegate() {}
50 virtual void ActivateWindow() = 0;
51 virtual void CloseWindow() = 0;
52 virtual void SetInspectedPageBounds(const gfx::Rect
& rect
) = 0;
53 virtual void InspectElementCompleted() = 0;
54 virtual void SetIsDocked(bool is_docked
) = 0;
55 virtual void OpenInNewTab(const std::string
& url
) = 0;
56 virtual void SetWhitelistedShortcuts(const std::string
& message
) = 0;
58 virtual void InspectedContentsClosing() = 0;
59 virtual void OnLoadCompleted() = 0;
60 virtual InfoBarService
* GetInfoBarService() = 0;
61 virtual void RenderProcessGone(bool crashed
) = 0;
64 explicit DevToolsUIBindings(content::WebContents
* web_contents
);
65 ~DevToolsUIBindings() override
;
67 content::WebContents
* web_contents() { return web_contents_
; }
68 Profile
* profile() { return profile_
; }
69 content::DevToolsAgentHost
* agent_host() { return agent_host_
.get(); }
71 // Takes ownership over the |delegate|.
72 void SetDelegate(Delegate
* delegate
);
73 void CallClientFunction(const std::string
& function_name
,
74 const base::Value
* arg1
,
75 const base::Value
* arg2
,
76 const base::Value
* arg3
);
77 void AttachTo(const scoped_refptr
<content::DevToolsAgentHost
>& agent_host
);
80 bool IsAttachedTo(content::DevToolsAgentHost
* agent_host
);
83 // content::NotificationObserver overrides.
84 void Observe(int type
,
85 const content::NotificationSource
& source
,
86 const content::NotificationDetails
& details
) override
;
88 // content::DevToolsFrontendHost::Delegate implementation.
89 void HandleMessageFromDevToolsFrontend(const std::string
& message
) override
;
90 void HandleMessageFromDevToolsFrontendToBackend(
91 const std::string
& message
) override
;
93 // content::DevToolsAgentHostClient implementation.
94 void DispatchProtocolMessage(content::DevToolsAgentHost
* agent_host
,
95 const std::string
& message
) override
;
96 void AgentHostClosed(content::DevToolsAgentHost
* agent_host
,
97 bool replaced_with_another_client
) override
;
99 // DevToolsEmbedderMessageDispatcher::Delegate implementation.
100 void ActivateWindow(int request_id
) override
;
101 void CloseWindow(int request_id
) override
;
102 void LoadCompleted(int request_id
) override
;
103 void SetInspectedPageBounds(int request_id
,
104 const gfx::Rect
& rect
) override
;
105 void InspectElementCompleted(int request_id
) override
;
106 void InspectedURLChanged(int request_id
, const std::string
& url
) override
;
107 void LoadNetworkResource(int request_id
,
108 const std::string
& url
,
109 const std::string
& headers
,
110 int stream_id
) override
;
111 void SetIsDocked(int request_id
, bool is_docked
) override
;
112 void OpenInNewTab(int request_id
, const std::string
& url
) override
;
113 void SaveToFile(int request_id
,
114 const std::string
& url
,
115 const std::string
& content
,
116 bool save_as
) override
;
117 void AppendToFile(int request_id
,
118 const std::string
& url
,
119 const std::string
& content
) override
;
120 void RequestFileSystems(int request_id
) override
;
121 void AddFileSystem(int request_id
) override
;
122 void RemoveFileSystem(int request_id
,
123 const std::string
& file_system_path
) override
;
124 void UpgradeDraggedFileSystemPermissions(
126 const std::string
& file_system_url
) override
;
127 void IndexPath(int request_id
,
128 int index_request_id
,
129 const std::string
& file_system_path
) override
;
130 void StopIndexing(int request_id
, int index_request_id
) override
;
131 void SearchInPath(int request_id
,
132 int search_request_id
,
133 const std::string
& file_system_path
,
134 const std::string
& query
) override
;
135 void SetWhitelistedShortcuts(int request_id
,
136 const std::string
& message
) override
;
137 void ZoomIn(int request_id
) override
;
138 void ZoomOut(int request_id
) override
;
139 void ResetZoom(int request_id
) override
;
140 void OpenUrlOnRemoteDeviceAndInspect(int request_id
,
141 const std::string
& browser_id
,
142 const std::string
& url
) override
;
143 void SetDeviceCountUpdatesEnabled(int request_id
, bool enabled
) override
;
144 void SetDevicesUpdatesEnabled(int request_id
, bool enabled
) override
;
145 void SendMessageToBrowser(int request_id
,
146 const std::string
& message
) override
;
147 void RecordActionUMA(int request_id
,
148 const std::string
& name
,
149 int action
) override
;
151 // net::URLFetcherDelegate overrides.
152 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
154 void EnableRemoteDeviceCounter(bool enable
);
156 void SendMessageAck(int request_id
,
157 const base::Value
* arg1
);
159 // DevToolsAndroidBridge::DeviceCountListener override:
160 void DeviceCountChanged(int count
) override
;
162 // Forwards discovered devices to frontend.
163 virtual void DevicesUpdated(const std::string
& source
,
164 const base::ListValue
& targets
);
166 void DocumentOnLoadCompletedInMainFrame();
167 void DidNavigateMainFrame();
168 void FrontendLoaded();
170 // DevToolsFileHelper callbacks.
171 void FileSavedAs(const std::string
& url
);
172 void CanceledFileSaveAs(const std::string
& url
);
173 void AppendedTo(const std::string
& url
);
174 void FileSystemsLoaded(
175 const std::vector
<DevToolsFileHelper::FileSystem
>& file_systems
);
176 void FileSystemAdded(const DevToolsFileHelper::FileSystem
& file_system
);
177 void IndexingTotalWorkCalculated(int request_id
,
178 const std::string
& file_system_path
,
180 void IndexingWorked(int request_id
,
181 const std::string
& file_system_path
,
183 void IndexingDone(int request_id
, const std::string
& file_system_path
);
184 void SearchCompleted(int request_id
,
185 const std::string
& file_system_path
,
186 const std::vector
<std::string
>& file_paths
);
187 typedef base::Callback
<void(bool)> InfoBarCallback
;
188 void ShowDevToolsConfirmInfoBar(const base::string16
& message
,
189 const InfoBarCallback
& callback
);
191 // Theme and extensions support.
193 void AddDevToolsExtensionsToClient();
195 class FrontendWebContentsObserver
;
196 friend class FrontendWebContentsObserver
;
197 scoped_ptr
<FrontendWebContentsObserver
> frontend_contents_observer_
;
200 content::WebContents
* web_contents_
;
201 scoped_ptr
<Delegate
> delegate_
;
202 scoped_refptr
<content::DevToolsAgentHost
> agent_host_
;
203 content::NotificationRegistrar registrar_
;
204 scoped_ptr
<content::DevToolsFrontendHost
> frontend_host_
;
205 scoped_ptr
<DevToolsFileHelper
> file_helper_
;
206 scoped_refptr
<DevToolsFileSystemIndexer
> file_system_indexer_
;
209 scoped_refptr
<DevToolsFileSystemIndexer::FileSystemIndexingJob
> >
211 IndexingJobsMap indexing_jobs_
;
213 bool device_count_updates_enabled_
;
214 bool devices_updates_enabled_
;
215 bool frontend_loaded_
;
216 scoped_ptr
<DevToolsTargetsUIHandler
> remote_targets_handler_
;
217 scoped_ptr
<DevToolsEmbedderMessageDispatcher
> embedder_message_dispatcher_
;
219 using PendingRequestsMap
= std::map
<const net::URLFetcher
*, int>;
220 PendingRequestsMap pending_requests_
;
221 base::WeakPtrFactory
<DevToolsUIBindings
> weak_factory_
;
223 DISALLOW_COPY_AND_ASSIGN(DevToolsUIBindings
);
226 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_UI_BINDINGS_H_