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_target_impl.h"
10 #include "chrome/browser/devtools/devtools_targets_ui.h"
11 #include "chrome/browser/devtools/devtools_ui_bindings.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser_navigator.h"
14 #include "chrome/browser/ui/singleton_tabs.h"
15 #include "chrome/browser/ui/webui/theme_source.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/url_constants.h"
18 #include "content/public/browser/devtools_agent_host.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/notification_types.h"
23 #include "content/public/browser/user_metrics.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_contents_delegate.h"
26 #include "content/public/browser/web_contents_observer.h"
27 #include "content/public/browser/web_ui.h"
28 #include "content/public/browser/web_ui_data_source.h"
29 #include "content/public/browser/web_ui_message_handler.h"
30 #include "content/public/common/frame_navigate_params.h"
31 #include "grit/browser_resources.h"
33 using content::WebContents
;
34 using content::WebUIMessageHandler
;
38 const char kInitUICommand
[] = "init-ui";
39 const char kInspectCommand
[] = "inspect";
40 const char kActivateCommand
[] = "activate";
41 const char kCloseCommand
[] = "close";
42 const char kReloadCommand
[] = "reload";
43 const char kOpenCommand
[] = "open";
44 const char kInspectBrowser
[] = "inspect-browser";
45 const char kLocalHost
[] = "localhost";
47 const char kDiscoverUsbDevicesEnabledCommand
[] =
48 "set-discover-usb-devices-enabled";
49 const char kPortForwardingEnabledCommand
[] =
50 "set-port-forwarding-enabled";
51 const char kPortForwardingConfigCommand
[] = "set-port-forwarding-config";
53 const char kPortForwardingDefaultPort
[] = "8080";
54 const char kPortForwardingDefaultLocation
[] = "localhost:8080";
56 // InspectMessageHandler --------------------------------------------
58 class InspectMessageHandler
: public WebUIMessageHandler
{
60 explicit InspectMessageHandler(InspectUI
* inspect_ui
)
61 : inspect_ui_(inspect_ui
) {}
62 ~InspectMessageHandler() override
{}
65 // WebUIMessageHandler implementation.
66 void RegisterMessages() override
;
68 void HandleInitUICommand(const base::ListValue
* args
);
69 void HandleInspectCommand(const base::ListValue
* args
);
70 void HandleActivateCommand(const base::ListValue
* args
);
71 void HandleCloseCommand(const base::ListValue
* args
);
72 void HandleReloadCommand(const base::ListValue
* args
);
73 void HandleOpenCommand(const base::ListValue
* args
);
74 void HandleInspectBrowserCommand(const base::ListValue
* args
);
75 void HandleBooleanPrefChanged(const char* pref_name
,
76 const base::ListValue
* args
);
77 void HandlePortForwardingConfigCommand(const base::ListValue
* args
);
79 InspectUI
* inspect_ui_
;
81 DISALLOW_COPY_AND_ASSIGN(InspectMessageHandler
);
84 void InspectMessageHandler::RegisterMessages() {
85 web_ui()->RegisterMessageCallback(kInitUICommand
,
86 base::Bind(&InspectMessageHandler::HandleInitUICommand
,
87 base::Unretained(this)));
88 web_ui()->RegisterMessageCallback(kInspectCommand
,
89 base::Bind(&InspectMessageHandler::HandleInspectCommand
,
90 base::Unretained(this)));
91 web_ui()->RegisterMessageCallback(kActivateCommand
,
92 base::Bind(&InspectMessageHandler::HandleActivateCommand
,
93 base::Unretained(this)));
94 web_ui()->RegisterMessageCallback(kCloseCommand
,
95 base::Bind(&InspectMessageHandler::HandleCloseCommand
,
96 base::Unretained(this)));
97 web_ui()->RegisterMessageCallback(kDiscoverUsbDevicesEnabledCommand
,
98 base::Bind(&InspectMessageHandler::HandleBooleanPrefChanged
,
99 base::Unretained(this),
100 &prefs::kDevToolsDiscoverUsbDevicesEnabled
[0]));
101 web_ui()->RegisterMessageCallback(kPortForwardingEnabledCommand
,
102 base::Bind(&InspectMessageHandler::HandleBooleanPrefChanged
,
103 base::Unretained(this),
104 &prefs::kDevToolsPortForwardingEnabled
[0]));
105 web_ui()->RegisterMessageCallback(kPortForwardingConfigCommand
,
106 base::Bind(&InspectMessageHandler::HandlePortForwardingConfigCommand
,
107 base::Unretained(this)));
108 web_ui()->RegisterMessageCallback(kReloadCommand
,
109 base::Bind(&InspectMessageHandler::HandleReloadCommand
,
110 base::Unretained(this)));
111 web_ui()->RegisterMessageCallback(kOpenCommand
,
112 base::Bind(&InspectMessageHandler::HandleOpenCommand
,
113 base::Unretained(this)));
114 web_ui()->RegisterMessageCallback(kInspectBrowser
,
115 base::Bind(&InspectMessageHandler::HandleInspectBrowserCommand
,
116 base::Unretained(this)));
119 void InspectMessageHandler::HandleInitUICommand(const base::ListValue
*) {
120 inspect_ui_
->InitUI();
123 static bool ParseStringArgs(const base::ListValue
* args
,
126 std::string
* arg2
= 0) {
127 int arg_size
= args
->GetSize();
128 return (!arg0
|| (arg_size
> 0 && args
->GetString(0, arg0
))) &&
129 (!arg1
|| (arg_size
> 1 && args
->GetString(1, arg1
))) &&
130 (!arg2
|| (arg_size
> 2 && args
->GetString(2, arg2
)));
133 void InspectMessageHandler::HandleInspectCommand(const base::ListValue
* args
) {
136 if (ParseStringArgs(args
, &source
, &id
))
137 inspect_ui_
->Inspect(source
, id
);
140 void InspectMessageHandler::HandleActivateCommand(const base::ListValue
* args
) {
143 if (ParseStringArgs(args
, &source
, &id
))
144 inspect_ui_
->Activate(source
, id
);
147 void InspectMessageHandler::HandleCloseCommand(const base::ListValue
* args
) {
150 if (ParseStringArgs(args
, &source
, &id
))
151 inspect_ui_
->Close(source
, id
);
154 void InspectMessageHandler::HandleReloadCommand(const base::ListValue
* args
) {
157 if (ParseStringArgs(args
, &source
, &id
))
158 inspect_ui_
->Reload(source
, id
);
161 void InspectMessageHandler::HandleOpenCommand(const base::ListValue
* args
) {
162 std::string source_id
;
163 std::string browser_id
;
165 if (ParseStringArgs(args
, &source_id
, &browser_id
, &url
))
166 inspect_ui_
->Open(source_id
, browser_id
, url
);
169 void InspectMessageHandler::HandleInspectBrowserCommand(
170 const base::ListValue
* args
) {
171 std::string source_id
;
172 std::string browser_id
;
173 std::string front_end
;
174 if (ParseStringArgs(args
, &source_id
, &browser_id
, &front_end
)) {
175 inspect_ui_
->InspectBrowserWithCustomFrontend(
176 source_id
, browser_id
, GURL(front_end
));
180 void InspectMessageHandler::HandleBooleanPrefChanged(
181 const char* pref_name
,
182 const base::ListValue
* args
) {
183 Profile
* profile
= Profile::FromWebUI(web_ui());
188 if (args
->GetSize() == 1 && args
->GetBoolean(0, &enabled
))
189 profile
->GetPrefs()->SetBoolean(pref_name
, enabled
);
192 void InspectMessageHandler::HandlePortForwardingConfigCommand(
193 const base::ListValue
* args
) {
194 Profile
* profile
= Profile::FromWebUI(web_ui());
198 const base::DictionaryValue
* dict_src
;
199 if (args
->GetSize() == 1 && args
->GetDictionary(0, &dict_src
))
200 profile
->GetPrefs()->Set(prefs::kDevToolsPortForwardingConfig
, *dict_src
);
203 // DevToolsUIBindingsEnabler ----------------------------------------
205 class DevToolsUIBindingsEnabler
206 : public content::WebContentsObserver
{
208 DevToolsUIBindingsEnabler(WebContents
* web_contents
,
210 ~DevToolsUIBindingsEnabler() override
{}
212 DevToolsUIBindings
* GetBindings();
215 // contents::WebContentsObserver overrides.
216 void WebContentsDestroyed() override
;
217 void DidNavigateMainFrame(
218 const content::LoadCommittedDetails
& details
,
219 const content::FrameNavigateParams
& params
) override
;
221 DevToolsUIBindings bindings_
;
223 DISALLOW_COPY_AND_ASSIGN(DevToolsUIBindingsEnabler
);
226 DevToolsUIBindingsEnabler::DevToolsUIBindingsEnabler(
227 WebContents
* web_contents
,
229 : WebContentsObserver(web_contents
),
230 bindings_(web_contents
),
234 DevToolsUIBindings
* DevToolsUIBindingsEnabler::GetBindings() {
238 void DevToolsUIBindingsEnabler::WebContentsDestroyed() {
242 void DevToolsUIBindingsEnabler::DidNavigateMainFrame(
243 const content::LoadCommittedDetails
& details
,
244 const content::FrameNavigateParams
& params
) {
245 if (url_
!= params
.url
)
251 // InspectUI --------------------------------------------------------
253 InspectUI::InspectUI(content::WebUI
* web_ui
)
254 : WebUIController(web_ui
) {
255 web_ui
->AddMessageHandler(new InspectMessageHandler(this));
256 Profile
* profile
= Profile::FromWebUI(web_ui
);
257 content::WebUIDataSource::Add(profile
, CreateInspectUIHTMLSource());
259 // Set up the chrome://theme/ source.
260 ThemeSource
* theme
= new ThemeSource(profile
);
261 content::URLDataSource::Add(profile
, theme
);
264 InspectUI::~InspectUI() {
265 StopListeningNotifications();
268 void InspectUI::InitUI() {
269 SetPortForwardingDefaults();
270 StartListeningNotifications();
271 UpdateDiscoverUsbDevicesEnabled();
272 UpdatePortForwardingEnabled();
273 UpdatePortForwardingConfig();
276 void InspectUI::Inspect(const std::string
& source_id
,
277 const std::string
& target_id
) {
278 DevToolsTargetImpl
* target
= FindTarget(source_id
, target_id
);
280 const std::string target_type
= target
->GetType();
281 target
->Inspect(Profile::FromWebUI(web_ui()));
282 ForceUpdateIfNeeded(source_id
, target_type
);
286 void InspectUI::Activate(const std::string
& source_id
,
287 const std::string
& target_id
) {
288 DevToolsTargetImpl
* target
= FindTarget(source_id
, target_id
);
290 const std::string target_type
= target
->GetType();
292 ForceUpdateIfNeeded(source_id
, target_type
);
296 void InspectUI::Close(const std::string
& source_id
,
297 const std::string
& target_id
) {
298 DevToolsTargetImpl
* target
= FindTarget(source_id
, target_id
);
300 const std::string target_type
= target
->GetType();
302 ForceUpdateIfNeeded(source_id
, target_type
);
306 void InspectUI::Reload(const std::string
& source_id
,
307 const std::string
& target_id
) {
308 DevToolsTargetImpl
* target
= FindTarget(source_id
, target_id
);
310 const std::string target_type
= target
->GetType();
312 ForceUpdateIfNeeded(source_id
, target_type
);
316 void InspectUI::Open(const std::string
& source_id
,
317 const std::string
& browser_id
,
318 const std::string
& url
) {
319 DevToolsTargetsUIHandler
* handler
= FindTargetHandler(source_id
);
321 handler
->Open(browser_id
, url
);
324 void InspectUI::InspectBrowserWithCustomFrontend(
325 const std::string
& source_id
,
326 const std::string
& browser_id
,
327 const GURL
& frontend_url
) {
328 if (!frontend_url
.SchemeIs(content::kChromeUIScheme
) &&
329 !frontend_url
.SchemeIs(content::kChromeDevToolsScheme
) &&
330 frontend_url
.host() != kLocalHost
) {
334 DevToolsTargetsUIHandler
* handler
= FindTargetHandler(source_id
);
338 // Fetch agent host from remote browser.
339 scoped_refptr
<content::DevToolsAgentHost
> agent_host
=
340 handler
->GetBrowserAgentHost(browser_id
);
341 if (agent_host
->IsAttached())
344 // Create web contents for the front-end.
345 WebContents
* inspect_ui
= web_ui()->GetWebContents();
346 WebContents
* front_end
= inspect_ui
->GetDelegate()->OpenURLFromTab(
348 content::OpenURLParams(frontend_url
,
351 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
354 // Install devtools bindings.
355 DevToolsUIBindingsEnabler
* bindings_enabler
=
356 new DevToolsUIBindingsEnabler(front_end
, frontend_url
);
357 bindings_enabler
->GetBindings()->AttachTo(agent_host
);
360 void InspectUI::InspectDevices(Browser
* browser
) {
361 content::RecordAction(base::UserMetricsAction("InspectDevices"));
362 chrome::NavigateParams
params(chrome::GetSingletonTabNavigateParams(
363 browser
, GURL(chrome::kChromeUIInspectURL
)));
364 params
.path_behavior
= chrome::NavigateParams::IGNORE_AND_NAVIGATE
;
365 ShowSingletonTabOverwritingNTP(browser
, params
);
368 void InspectUI::Observe(int type
,
369 const content::NotificationSource
& source
,
370 const content::NotificationDetails
& details
) {
371 if (source
== content::Source
<WebContents
>(web_ui()->GetWebContents()))
372 StopListeningNotifications();
375 void InspectUI::StartListeningNotifications() {
376 if (!target_handlers_
.empty()) // Possible when reloading the page.
377 StopListeningNotifications();
379 Profile
* profile
= Profile::FromWebUI(web_ui());
381 DevToolsTargetsUIHandler::Callback callback
=
382 base::Bind(&InspectUI::PopulateTargets
, base::Unretained(this));
385 DevToolsTargetsUIHandler::CreateForLocal(callback
));
386 if (profile
->IsOffTheRecord()) {
387 ShowIncognitoWarning();
390 DevToolsTargetsUIHandler::CreateForAdb(callback
, profile
));
393 port_status_serializer_
.reset(
394 new PortForwardingStatusSerializer(
395 base::Bind(&InspectUI::PopulatePortStatus
, base::Unretained(this)),
398 notification_registrar_
.Add(this,
399 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED
,
400 content::NotificationService::AllSources());
402 pref_change_registrar_
.Init(profile
->GetPrefs());
403 pref_change_registrar_
.Add(prefs::kDevToolsDiscoverUsbDevicesEnabled
,
404 base::Bind(&InspectUI::UpdateDiscoverUsbDevicesEnabled
,
405 base::Unretained(this)));
406 pref_change_registrar_
.Add(prefs::kDevToolsPortForwardingEnabled
,
407 base::Bind(&InspectUI::UpdatePortForwardingEnabled
,
408 base::Unretained(this)));
409 pref_change_registrar_
.Add(prefs::kDevToolsPortForwardingConfig
,
410 base::Bind(&InspectUI::UpdatePortForwardingConfig
,
411 base::Unretained(this)));
414 void InspectUI::StopListeningNotifications() {
415 if (target_handlers_
.empty())
418 STLDeleteValues(&target_handlers_
);
420 port_status_serializer_
.reset();
422 notification_registrar_
.RemoveAll();
423 pref_change_registrar_
.RemoveAll();
426 content::WebUIDataSource
* InspectUI::CreateInspectUIHTMLSource() {
427 content::WebUIDataSource
* source
=
428 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost
);
429 source
->AddResourcePath("inspect.css", IDR_INSPECT_CSS
);
430 source
->AddResourcePath("inspect.js", IDR_INSPECT_JS
);
431 source
->SetDefaultResource(IDR_INSPECT_HTML
);
435 void InspectUI::UpdateDiscoverUsbDevicesEnabled() {
436 web_ui()->CallJavascriptFunction(
437 "updateDiscoverUsbDevicesEnabled",
438 *GetPrefValue(prefs::kDevToolsDiscoverUsbDevicesEnabled
));
441 void InspectUI::UpdatePortForwardingEnabled() {
442 web_ui()->CallJavascriptFunction(
443 "updatePortForwardingEnabled",
444 *GetPrefValue(prefs::kDevToolsPortForwardingEnabled
));
447 void InspectUI::UpdatePortForwardingConfig() {
448 web_ui()->CallJavascriptFunction(
449 "updatePortForwardingConfig",
450 *GetPrefValue(prefs::kDevToolsPortForwardingConfig
));
453 void InspectUI::SetPortForwardingDefaults() {
454 Profile
* profile
= Profile::FromWebUI(web_ui());
455 PrefService
* prefs
= profile
->GetPrefs();
458 if (!GetPrefValue(prefs::kDevToolsPortForwardingDefaultSet
)->
459 GetAsBoolean(&default_set
) || default_set
) {
463 // This is the first chrome://inspect invocation on a fresh profile or after
464 // upgrade from a version that did not have kDevToolsPortForwardingDefaultSet.
465 prefs
->SetBoolean(prefs::kDevToolsPortForwardingDefaultSet
, true);
468 const base::DictionaryValue
* config
;
469 if (!GetPrefValue(prefs::kDevToolsPortForwardingEnabled
)->
470 GetAsBoolean(&enabled
) ||
471 !GetPrefValue(prefs::kDevToolsPortForwardingConfig
)->
472 GetAsDictionary(&config
)) {
476 // Do nothing if user already took explicit action.
477 if (enabled
|| config
->size() != 0)
480 base::DictionaryValue default_config
;
481 default_config
.SetString(
482 kPortForwardingDefaultPort
, kPortForwardingDefaultLocation
);
483 prefs
->Set(prefs::kDevToolsPortForwardingConfig
, default_config
);
486 const base::Value
* InspectUI::GetPrefValue(const char* name
) {
487 Profile
* profile
= Profile::FromWebUI(web_ui());
488 return profile
->GetPrefs()->FindPreference(name
)->GetValue();
491 void InspectUI::AddTargetUIHandler(
492 scoped_ptr
<DevToolsTargetsUIHandler
> handler
) {
493 DevToolsTargetsUIHandler
* handler_ptr
= handler
.release();
494 target_handlers_
[handler_ptr
->source_id()] = handler_ptr
;
497 DevToolsTargetsUIHandler
* InspectUI::FindTargetHandler(
498 const std::string
& source_id
) {
499 TargetHandlerMap::iterator it
= target_handlers_
.find(source_id
);
500 return it
!= target_handlers_
.end() ? it
->second
: NULL
;
503 DevToolsTargetImpl
* InspectUI::FindTarget(
504 const std::string
& source_id
, const std::string
& target_id
) {
505 TargetHandlerMap::iterator it
= target_handlers_
.find(source_id
);
506 return it
!= target_handlers_
.end() ?
507 it
->second
->GetTarget(target_id
) : NULL
;
510 void InspectUI::PopulateTargets(const std::string
& source
,
511 const base::ListValue
& targets
) {
512 web_ui()->CallJavascriptFunction("populateTargets",
513 base::StringValue(source
),
517 void InspectUI::ForceUpdateIfNeeded(const std::string
& source_id
,
518 const std::string
& target_type
) {
519 // TODO(dgozman): remove this after moving discovery to protocol.
520 // See crbug.com/398049.
521 if (target_type
!= DevToolsTargetImpl::kTargetTypeServiceWorker
)
523 DevToolsTargetsUIHandler
* handler
= FindTargetHandler(source_id
);
525 handler
->ForceUpdate();
528 void InspectUI::PopulatePortStatus(const base::Value
& status
) {
529 web_ui()->CallJavascriptFunction("populatePortStatus", status
);
532 void InspectUI::ShowIncognitoWarning() {
533 web_ui()->CallJavascriptFunction("showIncognitoWarning");