1 // Copyright (c) 2012 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 #include "chrome/browser/ui/webui/inspect_ui.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/devtools/devtools_adb_bridge.h"
10 #include "chrome/browser/devtools/devtools_targets_ui.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser_navigator.h"
13 #include "chrome/browser/ui/singleton_tabs.h"
14 #include "chrome/browser/ui/webui/theme_source.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_source.h"
19 #include "content/public/browser/notification_types.h"
20 #include "content/public/browser/user_metrics.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_data_source.h"
24 #include "content/public/browser/web_ui_message_handler.h"
25 #include "grit/browser_resources.h"
27 using content::WebContents
;
28 using content::WebUIMessageHandler
;
32 const char kInitUICommand
[] = "init-ui";
33 const char kInspectCommand
[] = "inspect";
34 const char kActivateCommand
[] = "activate";
35 const char kCloseCommand
[] = "close";
36 const char kReloadCommand
[] = "reload";
37 const char kOpenCommand
[] = "open";
39 const char kDiscoverUsbDevicesEnabledCommand
[] =
40 "set-discover-usb-devices-enabled";
41 const char kPortForwardingEnabledCommand
[] =
42 "set-port-forwarding-enabled";
43 const char kPortForwardingConfigCommand
[] = "set-port-forwarding-config";
45 const char kPortForwardingDefaultPort
[] = "8080";
46 const char kPortForwardingDefaultLocation
[] = "localhost:8080";
48 class InspectMessageHandler
: public WebUIMessageHandler
{
50 explicit InspectMessageHandler(InspectUI
* inspect_ui
)
51 : inspect_ui_(inspect_ui
) {}
52 virtual ~InspectMessageHandler() {}
55 // WebUIMessageHandler implementation.
56 virtual void RegisterMessages() OVERRIDE
;
58 void HandleInitUICommand(const base::ListValue
* args
);
59 void HandleInspectCommand(const base::ListValue
* args
);
60 void HandleActivateCommand(const base::ListValue
* args
);
61 void HandleCloseCommand(const base::ListValue
* args
);
62 void HandleReloadCommand(const base::ListValue
* args
);
63 void HandleOpenCommand(const base::ListValue
* args
);
64 void HandleBooleanPrefChanged(const char* pref_name
,
65 const base::ListValue
* args
);
66 void HandlePortForwardingConfigCommand(const base::ListValue
* args
);
68 InspectUI
* inspect_ui_
;
70 DISALLOW_COPY_AND_ASSIGN(InspectMessageHandler
);
73 void InspectMessageHandler::RegisterMessages() {
74 web_ui()->RegisterMessageCallback(kInitUICommand
,
75 base::Bind(&InspectMessageHandler::HandleInitUICommand
,
76 base::Unretained(this)));
77 web_ui()->RegisterMessageCallback(kInspectCommand
,
78 base::Bind(&InspectMessageHandler::HandleInspectCommand
,
79 base::Unretained(this)));
80 web_ui()->RegisterMessageCallback(kActivateCommand
,
81 base::Bind(&InspectMessageHandler::HandleActivateCommand
,
82 base::Unretained(this)));
83 web_ui()->RegisterMessageCallback(kCloseCommand
,
84 base::Bind(&InspectMessageHandler::HandleCloseCommand
,
85 base::Unretained(this)));
86 web_ui()->RegisterMessageCallback(kDiscoverUsbDevicesEnabledCommand
,
87 base::Bind(&InspectMessageHandler::HandleBooleanPrefChanged
,
88 base::Unretained(this),
89 &prefs::kDevToolsDiscoverUsbDevicesEnabled
[0]));
90 web_ui()->RegisterMessageCallback(kPortForwardingEnabledCommand
,
91 base::Bind(&InspectMessageHandler::HandleBooleanPrefChanged
,
92 base::Unretained(this),
93 &prefs::kDevToolsPortForwardingEnabled
[0]));
94 web_ui()->RegisterMessageCallback(kPortForwardingConfigCommand
,
95 base::Bind(&InspectMessageHandler::HandlePortForwardingConfigCommand
,
96 base::Unretained(this)));
97 web_ui()->RegisterMessageCallback(kReloadCommand
,
98 base::Bind(&InspectMessageHandler::HandleReloadCommand
,
99 base::Unretained(this)));
100 web_ui()->RegisterMessageCallback(kOpenCommand
,
101 base::Bind(&InspectMessageHandler::HandleOpenCommand
,
102 base::Unretained(this)));
105 void InspectMessageHandler::HandleInitUICommand(const base::ListValue
*) {
106 inspect_ui_
->InitUI();
109 static bool ParseStringArgs(const base::ListValue
* args
,
112 std::string
* arg2
= 0) {
113 int arg_size
= args
->GetSize();
114 return (!arg0
|| (arg_size
> 0 && args
->GetString(0, arg0
))) &&
115 (!arg1
|| (arg_size
> 1 && args
->GetString(1, arg1
))) &&
116 (!arg2
|| (arg_size
> 2 && args
->GetString(2, arg2
)));
119 void InspectMessageHandler::HandleInspectCommand(const base::ListValue
* args
) {
122 if (ParseStringArgs(args
, &source
, &id
))
123 inspect_ui_
->Inspect(source
, id
);
126 void InspectMessageHandler::HandleActivateCommand(const base::ListValue
* args
) {
129 if (ParseStringArgs(args
, &source
, &id
))
130 inspect_ui_
->Activate(source
, id
);
133 void InspectMessageHandler::HandleCloseCommand(const base::ListValue
* args
) {
136 if (ParseStringArgs(args
, &source
, &id
))
137 inspect_ui_
->Close(source
, id
);
140 void InspectMessageHandler::HandleReloadCommand(const base::ListValue
* args
) {
143 if (ParseStringArgs(args
, &source
, &id
))
144 inspect_ui_
->Reload(source
, id
);
147 void InspectMessageHandler::HandleOpenCommand(const base::ListValue
* args
) {
148 std::string source_id
;
149 std::string browser_id
;
151 if (ParseStringArgs(args
, &source_id
, &browser_id
, &url
))
152 inspect_ui_
->Open(source_id
, browser_id
, url
);
155 void InspectMessageHandler::HandleBooleanPrefChanged(
156 const char* pref_name
,
157 const base::ListValue
* args
) {
158 Profile
* profile
= Profile::FromWebUI(web_ui());
163 if (args
->GetSize() == 1 && args
->GetBoolean(0, &enabled
))
164 profile
->GetPrefs()->SetBoolean(pref_name
, enabled
);
167 void InspectMessageHandler::HandlePortForwardingConfigCommand(
168 const base::ListValue
* args
) {
169 Profile
* profile
= Profile::FromWebUI(web_ui());
173 const base::DictionaryValue
* dict_src
;
174 if (args
->GetSize() == 1 && args
->GetDictionary(0, &dict_src
))
175 profile
->GetPrefs()->Set(prefs::kDevToolsPortForwardingConfig
, *dict_src
);
180 InspectUI::InspectUI(content::WebUI
* web_ui
)
181 : WebUIController(web_ui
) {
182 web_ui
->AddMessageHandler(new InspectMessageHandler(this));
183 Profile
* profile
= Profile::FromWebUI(web_ui
);
184 content::WebUIDataSource::Add(profile
, CreateInspectUIHTMLSource());
186 // Set up the chrome://theme/ source.
187 ThemeSource
* theme
= new ThemeSource(profile
);
188 content::URLDataSource::Add(profile
, theme
);
191 InspectUI::~InspectUI() {
192 StopListeningNotifications();
195 void InspectUI::InitUI() {
196 SetPortForwardingDefaults();
197 StartListeningNotifications();
198 UpdateDiscoverUsbDevicesEnabled();
199 UpdatePortForwardingEnabled();
200 UpdatePortForwardingConfig();
203 void InspectUI::Inspect(const std::string
& source_id
,
204 const std::string
& target_id
) {
205 DevToolsTargetsUIHandler
* handler
= FindTargetHandler(source_id
);
207 handler
->Inspect(target_id
, Profile::FromWebUI(web_ui()));
210 void InspectUI::Activate(const std::string
& source_id
,
211 const std::string
& target_id
) {
212 DevToolsTargetsUIHandler
* handler
= FindTargetHandler(source_id
);
214 handler
->Activate(target_id
);
217 void InspectUI::Close(const std::string
& source_id
,
218 const std::string
& target_id
) {
219 DevToolsTargetsUIHandler
* handler
= FindTargetHandler(source_id
);
221 handler
->Close(target_id
);
224 void InspectUI::Reload(const std::string
& source_id
,
225 const std::string
& target_id
) {
226 DevToolsTargetsUIHandler
* handler
= FindTargetHandler(source_id
);
228 handler
->Reload(target_id
);
231 void InspectUI::Open(const std::string
& source_id
,
232 const std::string
& browser_id
,
233 const std::string
& url
) {
234 DevToolsRemoteTargetsUIHandler
* handler
= FindRemoteTargetHandler(source_id
);
236 handler
->Open(browser_id
, url
);
239 void InspectUI::InspectDevices(Browser
* browser
) {
240 content::RecordAction(base::UserMetricsAction("InspectDevices"));
241 chrome::NavigateParams
params(chrome::GetSingletonTabNavigateParams(
242 browser
, GURL(chrome::kChromeUIInspectURL
)));
243 params
.path_behavior
= chrome::NavigateParams::IGNORE_AND_NAVIGATE
;
244 ShowSingletonTabOverwritingNTP(browser
, params
);
247 void InspectUI::Observe(int type
,
248 const content::NotificationSource
& source
,
249 const content::NotificationDetails
& details
) {
250 if (source
== content::Source
<WebContents
>(web_ui()->GetWebContents()))
251 StopListeningNotifications();
254 void InspectUI::StartListeningNotifications() {
255 if (!target_handlers_
.empty()) // Possible when reloading the page.
256 StopListeningNotifications();
258 Profile
* profile
= Profile::FromWebUI(web_ui());
260 DevToolsTargetsUIHandler::Callback callback
=
261 base::Bind(&InspectUI::PopulateTargets
, base::Unretained(this));
264 DevToolsTargetsUIHandler::CreateForRenderers(callback
));
266 DevToolsTargetsUIHandler::CreateForWorkers(callback
));
267 AddRemoteTargetUIHandler(
268 DevToolsRemoteTargetsUIHandler::CreateForAdb(callback
, profile
));
270 notification_registrar_
.Add(this,
271 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED
,
272 content::NotificationService::AllSources());
274 pref_change_registrar_
.Init(profile
->GetPrefs());
275 pref_change_registrar_
.Add(prefs::kDevToolsDiscoverUsbDevicesEnabled
,
276 base::Bind(&InspectUI::UpdateDiscoverUsbDevicesEnabled
,
277 base::Unretained(this)));
278 pref_change_registrar_
.Add(prefs::kDevToolsPortForwardingEnabled
,
279 base::Bind(&InspectUI::UpdatePortForwardingEnabled
,
280 base::Unretained(this)));
281 pref_change_registrar_
.Add(prefs::kDevToolsPortForwardingConfig
,
282 base::Bind(&InspectUI::UpdatePortForwardingConfig
,
283 base::Unretained(this)));
286 void InspectUI::StopListeningNotifications() {
287 if (target_handlers_
.empty())
290 STLDeleteValues(&target_handlers_
);
291 STLDeleteValues(&remote_target_handlers_
);
293 notification_registrar_
.RemoveAll();
294 pref_change_registrar_
.RemoveAll();
297 content::WebUIDataSource
* InspectUI::CreateInspectUIHTMLSource() {
298 content::WebUIDataSource
* source
=
299 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost
);
300 source
->AddResourcePath("inspect.css", IDR_INSPECT_CSS
);
301 source
->AddResourcePath("inspect.js", IDR_INSPECT_JS
);
302 source
->SetDefaultResource(IDR_INSPECT_HTML
);
306 void InspectUI::UpdateDiscoverUsbDevicesEnabled() {
307 const base::Value
* value
=
308 GetPrefValue(prefs::kDevToolsDiscoverUsbDevicesEnabled
);
309 web_ui()->CallJavascriptFunction("updateDiscoverUsbDevicesEnabled", *value
);
311 // Configure adb bridge.
312 Profile
* profile
= Profile::FromWebUI(web_ui());
313 DevToolsAdbBridge
* adb_bridge
=
314 DevToolsAdbBridge::Factory::GetForProfile(profile
);
316 bool enabled
= false;
317 value
->GetAsBoolean(&enabled
);
319 DevToolsAdbBridge::DeviceProviders device_providers
;
321 #if defined(DEBUG_DEVTOOLS)
322 device_providers
.push_back(
323 AndroidDeviceProvider::GetSelfAsDeviceProvider());
326 device_providers
.push_back(AndroidDeviceProvider::GetAdbDeviceProvider());
329 device_providers
.push_back(
330 AndroidDeviceProvider::GetUsbDeviceProvider(profile
));
333 adb_bridge
->set_device_providers(device_providers
);
337 void InspectUI::UpdatePortForwardingEnabled() {
338 web_ui()->CallJavascriptFunction("updatePortForwardingEnabled",
339 *GetPrefValue(prefs::kDevToolsPortForwardingEnabled
));
343 void InspectUI::UpdatePortForwardingConfig() {
344 web_ui()->CallJavascriptFunction("updatePortForwardingConfig",
345 *GetPrefValue(prefs::kDevToolsPortForwardingConfig
));
348 void InspectUI::SetPortForwardingDefaults() {
349 Profile
* profile
= Profile::FromWebUI(web_ui());
350 PrefService
* prefs
= profile
->GetPrefs();
353 if (!GetPrefValue(prefs::kDevToolsPortForwardingDefaultSet
)->
354 GetAsBoolean(&default_set
) || default_set
) {
358 // This is the first chrome://inspect invocation on a fresh profile or after
359 // upgrade from a version that did not have kDevToolsPortForwardingDefaultSet.
360 prefs
->SetBoolean(prefs::kDevToolsPortForwardingDefaultSet
, true);
363 const base::DictionaryValue
* config
;
364 if (!GetPrefValue(prefs::kDevToolsPortForwardingEnabled
)->
365 GetAsBoolean(&enabled
) ||
366 !GetPrefValue(prefs::kDevToolsPortForwardingConfig
)->
367 GetAsDictionary(&config
)) {
371 // Do nothing if user already took explicit action.
372 if (enabled
|| config
->size() != 0)
375 base::DictionaryValue default_config
;
376 default_config
.SetString(
377 kPortForwardingDefaultPort
, kPortForwardingDefaultLocation
);
378 prefs
->Set(prefs::kDevToolsPortForwardingConfig
, default_config
);
381 const base::Value
* InspectUI::GetPrefValue(const char* name
) {
382 Profile
* profile
= Profile::FromWebUI(web_ui());
383 return profile
->GetPrefs()->FindPreference(name
)->GetValue();
386 void InspectUI::AddTargetUIHandler(
387 scoped_ptr
<DevToolsTargetsUIHandler
> handler
) {
388 DevToolsTargetsUIHandler
* handler_ptr
= handler
.release();
389 target_handlers_
[handler_ptr
->source_id()] = handler_ptr
;
392 void InspectUI::AddRemoteTargetUIHandler(
393 scoped_ptr
<DevToolsRemoteTargetsUIHandler
> handler
) {
394 DevToolsRemoteTargetsUIHandler
* handler_ptr
= handler
.release();
395 remote_target_handlers_
[handler_ptr
->source_id()] = handler_ptr
;
398 DevToolsTargetsUIHandler
* InspectUI::FindTargetHandler(
399 const std::string
& source_id
) {
400 TargetHandlerMap::iterator it
= target_handlers_
.find(source_id
);
401 return it
!= target_handlers_
.end() ?
403 FindRemoteTargetHandler(source_id
);
406 DevToolsRemoteTargetsUIHandler
* InspectUI::FindRemoteTargetHandler(
407 const std::string
& source_id
) {
408 RemoteTargetHandlerMap::iterator it
= remote_target_handlers_
.find(source_id
);
409 return it
!= remote_target_handlers_
.end() ? it
->second
: NULL
;
412 void InspectUI::PopulateTargets(const std::string
& source
,
413 scoped_ptr
<base::ListValue
> targets
) {
414 scoped_ptr
<base::Value
> source_value(base::Value::CreateStringValue(source
));
415 web_ui()->CallJavascriptFunction(