downloads: clicking "remove" on chrome://downloads should hide the shelf item.
[chromium-blink-merge.git] / chrome / browser / ui / webui / inspect_ui.cc
blob77ac2188a91308e0b6c1bc36c0e53280d0f2074a
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;
36 namespace {
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 {
59 public:
60 explicit InspectMessageHandler(InspectUI* inspect_ui)
61 : inspect_ui_(inspect_ui) {}
62 ~InspectMessageHandler() override {}
64 private:
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,
124 std::string* arg0,
125 std::string* arg1,
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) {
134 std::string source;
135 std::string id;
136 if (ParseStringArgs(args, &source, &id))
137 inspect_ui_->Inspect(source, id);
140 void InspectMessageHandler::HandleActivateCommand(const base::ListValue* args) {
141 std::string source;
142 std::string id;
143 if (ParseStringArgs(args, &source, &id))
144 inspect_ui_->Activate(source, id);
147 void InspectMessageHandler::HandleCloseCommand(const base::ListValue* args) {
148 std::string source;
149 std::string id;
150 if (ParseStringArgs(args, &source, &id))
151 inspect_ui_->Close(source, id);
154 void InspectMessageHandler::HandleReloadCommand(const base::ListValue* args) {
155 std::string source;
156 std::string id;
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;
164 std::string url;
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());
184 if (!profile)
185 return;
187 bool enabled;
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());
195 if (!profile)
196 return;
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 {
207 public:
208 DevToolsUIBindingsEnabler(WebContents* web_contents,
209 const GURL& url);
210 ~DevToolsUIBindingsEnabler() override {}
212 DevToolsUIBindings* GetBindings();
214 private:
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_;
222 GURL url_;
223 DISALLOW_COPY_AND_ASSIGN(DevToolsUIBindingsEnabler);
226 DevToolsUIBindingsEnabler::DevToolsUIBindingsEnabler(
227 WebContents* web_contents,
228 const GURL& url)
229 : WebContentsObserver(web_contents),
230 bindings_(web_contents),
231 url_(url) {
234 DevToolsUIBindings* DevToolsUIBindingsEnabler::GetBindings() {
235 return &bindings_;
238 void DevToolsUIBindingsEnabler::WebContentsDestroyed() {
239 delete this;
242 void DevToolsUIBindingsEnabler::DidNavigateMainFrame(
243 const content::LoadCommittedDetails& details,
244 const content::FrameNavigateParams& params) {
245 if (url_ != params.url)
246 delete this;
249 } // namespace
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);
279 if (target) {
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);
289 if (target) {
290 const std::string target_type = target->GetType();
291 target->Activate();
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);
299 if (target) {
300 const std::string target_type = target->GetType();
301 target->Close();
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);
309 if (target) {
310 const std::string target_type = target->GetType();
311 target->Reload();
312 ForceUpdateIfNeeded(source_id, target_type);
316 static void NoOp(DevToolsTargetImpl*) {}
318 void InspectUI::Open(const std::string& source_id,
319 const std::string& browser_id,
320 const std::string& url) {
321 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id);
322 if (handler)
323 handler->Open(browser_id, url, base::Bind(&NoOp));
326 void InspectUI::InspectBrowserWithCustomFrontend(
327 const std::string& source_id,
328 const std::string& browser_id,
329 const GURL& frontend_url) {
330 if (!frontend_url.SchemeIs(content::kChromeUIScheme) &&
331 !frontend_url.SchemeIs(content::kChromeDevToolsScheme) &&
332 frontend_url.host() != kLocalHost) {
333 return;
336 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id);
337 if (!handler)
338 return;
340 // Fetch agent host from remote browser.
341 scoped_refptr<content::DevToolsAgentHost> agent_host =
342 handler->GetBrowserAgentHost(browser_id);
343 if (agent_host->IsAttached())
344 return;
346 // Create web contents for the front-end.
347 WebContents* inspect_ui = web_ui()->GetWebContents();
348 WebContents* front_end = inspect_ui->GetDelegate()->OpenURLFromTab(
349 inspect_ui,
350 content::OpenURLParams(frontend_url,
351 content::Referrer(),
352 NEW_FOREGROUND_TAB,
353 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
354 false));
356 // Install devtools bindings.
357 DevToolsUIBindingsEnabler* bindings_enabler =
358 new DevToolsUIBindingsEnabler(front_end, frontend_url);
359 bindings_enabler->GetBindings()->AttachTo(agent_host);
362 void InspectUI::InspectDevices(Browser* browser) {
363 content::RecordAction(base::UserMetricsAction("InspectDevices"));
364 chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams(
365 browser, GURL(chrome::kChromeUIInspectURL)));
366 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
367 ShowSingletonTabOverwritingNTP(browser, params);
370 void InspectUI::Observe(int type,
371 const content::NotificationSource& source,
372 const content::NotificationDetails& details) {
373 if (source == content::Source<WebContents>(web_ui()->GetWebContents()))
374 StopListeningNotifications();
377 void InspectUI::StartListeningNotifications() {
378 if (!target_handlers_.empty()) // Possible when reloading the page.
379 StopListeningNotifications();
381 Profile* profile = Profile::FromWebUI(web_ui());
383 DevToolsTargetsUIHandler::Callback callback =
384 base::Bind(&InspectUI::PopulateTargets, base::Unretained(this));
386 AddTargetUIHandler(
387 DevToolsTargetsUIHandler::CreateForLocal(callback));
388 if (profile->IsOffTheRecord()) {
389 ShowIncognitoWarning();
390 } else {
391 AddTargetUIHandler(
392 DevToolsTargetsUIHandler::CreateForAdb(callback, profile));
395 port_status_serializer_.reset(
396 new PortForwardingStatusSerializer(
397 base::Bind(&InspectUI::PopulatePortStatus, base::Unretained(this)),
398 profile));
400 notification_registrar_.Add(this,
401 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
402 content::NotificationService::AllSources());
404 pref_change_registrar_.Init(profile->GetPrefs());
405 pref_change_registrar_.Add(prefs::kDevToolsDiscoverUsbDevicesEnabled,
406 base::Bind(&InspectUI::UpdateDiscoverUsbDevicesEnabled,
407 base::Unretained(this)));
408 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled,
409 base::Bind(&InspectUI::UpdatePortForwardingEnabled,
410 base::Unretained(this)));
411 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig,
412 base::Bind(&InspectUI::UpdatePortForwardingConfig,
413 base::Unretained(this)));
416 void InspectUI::StopListeningNotifications() {
417 if (target_handlers_.empty())
418 return;
420 STLDeleteValues(&target_handlers_);
422 port_status_serializer_.reset();
424 notification_registrar_.RemoveAll();
425 pref_change_registrar_.RemoveAll();
428 content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() {
429 content::WebUIDataSource* source =
430 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost);
431 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS);
432 source->AddResourcePath("inspect.js", IDR_INSPECT_JS);
433 source->SetDefaultResource(IDR_INSPECT_HTML);
434 return source;
437 void InspectUI::UpdateDiscoverUsbDevicesEnabled() {
438 web_ui()->CallJavascriptFunction(
439 "updateDiscoverUsbDevicesEnabled",
440 *GetPrefValue(prefs::kDevToolsDiscoverUsbDevicesEnabled));
443 void InspectUI::UpdatePortForwardingEnabled() {
444 web_ui()->CallJavascriptFunction(
445 "updatePortForwardingEnabled",
446 *GetPrefValue(prefs::kDevToolsPortForwardingEnabled));
449 void InspectUI::UpdatePortForwardingConfig() {
450 web_ui()->CallJavascriptFunction(
451 "updatePortForwardingConfig",
452 *GetPrefValue(prefs::kDevToolsPortForwardingConfig));
455 void InspectUI::SetPortForwardingDefaults() {
456 Profile* profile = Profile::FromWebUI(web_ui());
457 PrefService* prefs = profile->GetPrefs();
459 bool default_set;
460 if (!GetPrefValue(prefs::kDevToolsPortForwardingDefaultSet)->
461 GetAsBoolean(&default_set) || default_set) {
462 return;
465 // This is the first chrome://inspect invocation on a fresh profile or after
466 // upgrade from a version that did not have kDevToolsPortForwardingDefaultSet.
467 prefs->SetBoolean(prefs::kDevToolsPortForwardingDefaultSet, true);
469 bool enabled;
470 const base::DictionaryValue* config;
471 if (!GetPrefValue(prefs::kDevToolsPortForwardingEnabled)->
472 GetAsBoolean(&enabled) ||
473 !GetPrefValue(prefs::kDevToolsPortForwardingConfig)->
474 GetAsDictionary(&config)) {
475 return;
478 // Do nothing if user already took explicit action.
479 if (enabled || config->size() != 0)
480 return;
482 base::DictionaryValue default_config;
483 default_config.SetString(
484 kPortForwardingDefaultPort, kPortForwardingDefaultLocation);
485 prefs->Set(prefs::kDevToolsPortForwardingConfig, default_config);
488 const base::Value* InspectUI::GetPrefValue(const char* name) {
489 Profile* profile = Profile::FromWebUI(web_ui());
490 return profile->GetPrefs()->FindPreference(name)->GetValue();
493 void InspectUI::AddTargetUIHandler(
494 scoped_ptr<DevToolsTargetsUIHandler> handler) {
495 DevToolsTargetsUIHandler* handler_ptr = handler.release();
496 target_handlers_[handler_ptr->source_id()] = handler_ptr;
499 DevToolsTargetsUIHandler* InspectUI::FindTargetHandler(
500 const std::string& source_id) {
501 TargetHandlerMap::iterator it = target_handlers_.find(source_id);
502 return it != target_handlers_.end() ? it->second : NULL;
505 DevToolsTargetImpl* InspectUI::FindTarget(
506 const std::string& source_id, const std::string& target_id) {
507 TargetHandlerMap::iterator it = target_handlers_.find(source_id);
508 return it != target_handlers_.end() ?
509 it->second->GetTarget(target_id) : NULL;
512 void InspectUI::PopulateTargets(const std::string& source,
513 const base::ListValue& targets) {
514 web_ui()->CallJavascriptFunction("populateTargets",
515 base::StringValue(source),
516 targets);
519 void InspectUI::ForceUpdateIfNeeded(const std::string& source_id,
520 const std::string& target_type) {
521 // TODO(dgozman): remove this after moving discovery to protocol.
522 // See crbug.com/398049.
523 if (target_type != DevToolsTargetImpl::kTargetTypeServiceWorker)
524 return;
525 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id);
526 if (handler)
527 handler->ForceUpdate();
530 void InspectUI::PopulatePortStatus(const base::Value& status) {
531 web_ui()->CallJavascriptFunction("populatePortStatus", status);
534 void InspectUI::ShowIncognitoWarning() {
535 web_ui()->CallJavascriptFunction("showIncognitoWarning");