Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/inspect_ui.h" | 5 #include "chrome/browser/ui/webui/inspect_ui.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "chrome/browser/devtools/devtools_target_impl.h" | 9 #include "chrome/browser/devtools/devtools_target_impl.h" |
| 10 #include "chrome/browser/devtools/devtools_targets_ui.h" | 10 #include "chrome/browser/devtools/devtools_targets_ui.h" |
| 11 #include "chrome/browser/devtools/devtools_ui_bindings.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser_navigator.h" | 13 #include "chrome/browser/ui/browser_navigator.h" |
| 13 #include "chrome/browser/ui/singleton_tabs.h" | 14 #include "chrome/browser/ui/singleton_tabs.h" |
| 14 #include "chrome/browser/ui/webui/theme_source.h" | 15 #include "chrome/browser/ui/webui/theme_source.h" |
| 15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/public/browser/devtools_agent_host.h" | |
| 19 #include "content/public/browser/devtools_manager.h" | |
| 17 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 19 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/browser/user_metrics.h" | 23 #include "content/public/browser/user_metrics.h" |
| 21 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/browser/web_contents_delegate.h" | |
| 22 #include "content/public/browser/web_ui.h" | 26 #include "content/public/browser/web_ui.h" |
| 23 #include "content/public/browser/web_ui_data_source.h" | 27 #include "content/public/browser/web_ui_data_source.h" |
| 24 #include "content/public/browser/web_ui_message_handler.h" | 28 #include "content/public/browser/web_ui_message_handler.h" |
| 25 #include "grit/browser_resources.h" | 29 #include "grit/browser_resources.h" |
| 26 | 30 |
| 27 using content::WebContents; | 31 using content::WebContents; |
| 28 using content::WebUIMessageHandler; | 32 using content::WebUIMessageHandler; |
| 29 | 33 |
| 30 namespace { | 34 namespace { |
| 31 | 35 |
| 32 const char kInitUICommand[] = "init-ui"; | 36 const char kInitUICommand[] = "init-ui"; |
| 33 const char kInspectCommand[] = "inspect"; | 37 const char kInspectCommand[] = "inspect"; |
| 34 const char kActivateCommand[] = "activate"; | 38 const char kActivateCommand[] = "activate"; |
| 35 const char kCloseCommand[] = "close"; | 39 const char kCloseCommand[] = "close"; |
| 36 const char kReloadCommand[] = "reload"; | 40 const char kReloadCommand[] = "reload"; |
| 37 const char kOpenCommand[] = "open"; | 41 const char kOpenCommand[] = "open"; |
| 42 const char kInspectBrowser[] = "inspect-browser"; | |
| 38 | 43 |
| 39 const char kDiscoverUsbDevicesEnabledCommand[] = | 44 const char kDiscoverUsbDevicesEnabledCommand[] = |
| 40 "set-discover-usb-devices-enabled"; | 45 "set-discover-usb-devices-enabled"; |
| 41 const char kPortForwardingEnabledCommand[] = | 46 const char kPortForwardingEnabledCommand[] = |
| 42 "set-port-forwarding-enabled"; | 47 "set-port-forwarding-enabled"; |
| 43 const char kPortForwardingConfigCommand[] = "set-port-forwarding-config"; | 48 const char kPortForwardingConfigCommand[] = "set-port-forwarding-config"; |
| 44 | 49 |
| 45 const char kPortForwardingDefaultPort[] = "8080"; | 50 const char kPortForwardingDefaultPort[] = "8080"; |
| 46 const char kPortForwardingDefaultLocation[] = "localhost:8080"; | 51 const char kPortForwardingDefaultLocation[] = "localhost:8080"; |
| 47 | 52 |
| 48 class InspectMessageHandler : public WebUIMessageHandler { | 53 class InspectMessageHandler : public WebUIMessageHandler { |
| 49 public: | 54 public: |
| 50 explicit InspectMessageHandler(InspectUI* inspect_ui) | 55 explicit InspectMessageHandler(InspectUI* inspect_ui) |
| 51 : inspect_ui_(inspect_ui) {} | 56 : inspect_ui_(inspect_ui) {} |
| 52 virtual ~InspectMessageHandler() {} | 57 virtual ~InspectMessageHandler() {} |
| 53 | 58 |
| 54 private: | 59 private: |
| 55 // WebUIMessageHandler implementation. | 60 // WebUIMessageHandler implementation. |
| 56 virtual void RegisterMessages() OVERRIDE; | 61 virtual void RegisterMessages() OVERRIDE; |
| 57 | 62 |
| 58 void HandleInitUICommand(const base::ListValue* args); | 63 void HandleInitUICommand(const base::ListValue* args); |
| 59 void HandleInspectCommand(const base::ListValue* args); | 64 void HandleInspectCommand(const base::ListValue* args); |
| 60 void HandleActivateCommand(const base::ListValue* args); | 65 void HandleActivateCommand(const base::ListValue* args); |
| 61 void HandleCloseCommand(const base::ListValue* args); | 66 void HandleCloseCommand(const base::ListValue* args); |
| 62 void HandleReloadCommand(const base::ListValue* args); | 67 void HandleReloadCommand(const base::ListValue* args); |
| 63 void HandleOpenCommand(const base::ListValue* args); | 68 void HandleOpenCommand(const base::ListValue* args); |
| 69 void HandleInspectBrowserCommand(const base::ListValue* args); | |
| 64 void HandleBooleanPrefChanged(const char* pref_name, | 70 void HandleBooleanPrefChanged(const char* pref_name, |
| 65 const base::ListValue* args); | 71 const base::ListValue* args); |
| 66 void HandlePortForwardingConfigCommand(const base::ListValue* args); | 72 void HandlePortForwardingConfigCommand(const base::ListValue* args); |
| 67 | 73 |
| 68 InspectUI* inspect_ui_; | 74 InspectUI* inspect_ui_; |
| 69 | 75 |
| 70 DISALLOW_COPY_AND_ASSIGN(InspectMessageHandler); | 76 DISALLOW_COPY_AND_ASSIGN(InspectMessageHandler); |
| 71 }; | 77 }; |
| 72 | 78 |
| 73 void InspectMessageHandler::RegisterMessages() { | 79 void InspectMessageHandler::RegisterMessages() { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 93 &prefs::kDevToolsPortForwardingEnabled[0])); | 99 &prefs::kDevToolsPortForwardingEnabled[0])); |
| 94 web_ui()->RegisterMessageCallback(kPortForwardingConfigCommand, | 100 web_ui()->RegisterMessageCallback(kPortForwardingConfigCommand, |
| 95 base::Bind(&InspectMessageHandler::HandlePortForwardingConfigCommand, | 101 base::Bind(&InspectMessageHandler::HandlePortForwardingConfigCommand, |
| 96 base::Unretained(this))); | 102 base::Unretained(this))); |
| 97 web_ui()->RegisterMessageCallback(kReloadCommand, | 103 web_ui()->RegisterMessageCallback(kReloadCommand, |
| 98 base::Bind(&InspectMessageHandler::HandleReloadCommand, | 104 base::Bind(&InspectMessageHandler::HandleReloadCommand, |
| 99 base::Unretained(this))); | 105 base::Unretained(this))); |
| 100 web_ui()->RegisterMessageCallback(kOpenCommand, | 106 web_ui()->RegisterMessageCallback(kOpenCommand, |
| 101 base::Bind(&InspectMessageHandler::HandleOpenCommand, | 107 base::Bind(&InspectMessageHandler::HandleOpenCommand, |
| 102 base::Unretained(this))); | 108 base::Unretained(this))); |
| 109 web_ui()->RegisterMessageCallback(kInspectBrowser, | |
| 110 base::Bind(&InspectMessageHandler::HandleInspectBrowserCommand, | |
| 111 base::Unretained(this))); | |
| 103 } | 112 } |
| 104 | 113 |
| 105 void InspectMessageHandler::HandleInitUICommand(const base::ListValue*) { | 114 void InspectMessageHandler::HandleInitUICommand(const base::ListValue*) { |
| 106 inspect_ui_->InitUI(); | 115 inspect_ui_->InitUI(); |
| 107 } | 116 } |
| 108 | 117 |
| 109 static bool ParseStringArgs(const base::ListValue* args, | 118 static bool ParseStringArgs(const base::ListValue* args, |
| 110 std::string* arg0, | 119 std::string* arg0, |
| 111 std::string* arg1, | 120 std::string* arg1, |
| 112 std::string* arg2 = 0) { | 121 std::string* arg2 = 0) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 } | 154 } |
| 146 | 155 |
| 147 void InspectMessageHandler::HandleOpenCommand(const base::ListValue* args) { | 156 void InspectMessageHandler::HandleOpenCommand(const base::ListValue* args) { |
| 148 std::string source_id; | 157 std::string source_id; |
| 149 std::string browser_id; | 158 std::string browser_id; |
| 150 std::string url; | 159 std::string url; |
| 151 if (ParseStringArgs(args, &source_id, &browser_id, &url)) | 160 if (ParseStringArgs(args, &source_id, &browser_id, &url)) |
| 152 inspect_ui_->Open(source_id, browser_id, url); | 161 inspect_ui_->Open(source_id, browser_id, url); |
| 153 } | 162 } |
| 154 | 163 |
| 164 void InspectMessageHandler::HandleInspectBrowserCommand( | |
| 165 const base::ListValue* args) { | |
| 166 std::string source_id; | |
| 167 std::string browser_id; | |
| 168 std::string front_end; | |
| 169 if (ParseStringArgs(args, &source_id, &browser_id, &front_end)) { | |
| 170 inspect_ui_->InspectBrowserWithCustomFrontend( | |
| 171 source_id, browser_id, GURL(front_end)); | |
| 172 } | |
| 173 } | |
| 174 | |
| 155 void InspectMessageHandler::HandleBooleanPrefChanged( | 175 void InspectMessageHandler::HandleBooleanPrefChanged( |
| 156 const char* pref_name, | 176 const char* pref_name, |
| 157 const base::ListValue* args) { | 177 const base::ListValue* args) { |
| 158 Profile* profile = Profile::FromWebUI(web_ui()); | 178 Profile* profile = Profile::FromWebUI(web_ui()); |
| 159 if (!profile) | 179 if (!profile) |
| 160 return; | 180 return; |
| 161 | 181 |
| 162 bool enabled; | 182 bool enabled; |
| 163 if (args->GetSize() == 1 && args->GetBoolean(0, &enabled)) | 183 if (args->GetSize() == 1 && args->GetBoolean(0, &enabled)) |
| 164 profile->GetPrefs()->SetBoolean(pref_name, enabled); | 184 profile->GetPrefs()->SetBoolean(pref_name, enabled); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 static void NoOp(DevToolsTargetImpl*) {} | 251 static void NoOp(DevToolsTargetImpl*) {} |
| 232 | 252 |
| 233 void InspectUI::Open(const std::string& source_id, | 253 void InspectUI::Open(const std::string& source_id, |
| 234 const std::string& browser_id, | 254 const std::string& browser_id, |
| 235 const std::string& url) { | 255 const std::string& url) { |
| 236 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id); | 256 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id); |
| 237 if (handler) | 257 if (handler) |
| 238 handler->Open(browser_id, url, base::Bind(&NoOp)); | 258 handler->Open(browser_id, url, base::Bind(&NoOp)); |
| 239 } | 259 } |
| 240 | 260 |
| 261 void InspectUI::InspectBrowserWithCustomFrontend( | |
| 262 const std::string& source_id, | |
| 263 const std::string& browser_id, | |
| 264 const GURL& frontend_url) { | |
| 265 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id); | |
| 266 if (!handler) | |
| 267 return; | |
| 268 | |
| 269 // Fetch agent host from remote browser. | |
| 270 scoped_refptr<content::DevToolsAgentHost> agent_host = | |
| 271 handler->GetBrowserAgentHost(browser_id); | |
| 272 if (agent_host->IsAttached()) | |
| 273 return; | |
| 274 | |
| 275 // Create web contents for the front-end. | |
|
Vladislav Kaznacheev
2014/05/16 13:38:37
This class is a UI handler. The tab opening and bi
| |
| 276 WebContents* inspect_ui = web_ui()->GetWebContents(); | |
| 277 WebContents* front_end = inspect_ui->GetDelegate()->OpenURLFromTab( | |
| 278 inspect_ui, | |
| 279 content::OpenURLParams(GURL(content::kAboutBlankURL), | |
| 280 content::Referrer(), | |
| 281 NEW_FOREGROUND_TAB, | |
| 282 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 283 false)); | |
| 284 | |
| 285 // Install devtools bindings. | |
| 286 DevToolsUIBindings* bindings = DevToolsUIBindings::GetOrCreateFor(front_end); | |
| 287 | |
| 288 // Navigate to a page. | |
| 289 front_end->GetController().LoadURL( | |
| 290 frontend_url, content::Referrer(), | |
| 291 content::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string()); | |
| 292 | |
| 293 | |
| 294 // Engage remote debugging between front-end and agent host. | |
| 295 content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( | |
| 296 agent_host, bindings->frontend_host()); | |
| 297 } | |
| 298 | |
| 241 void InspectUI::InspectDevices(Browser* browser) { | 299 void InspectUI::InspectDevices(Browser* browser) { |
| 242 content::RecordAction(base::UserMetricsAction("InspectDevices")); | 300 content::RecordAction(base::UserMetricsAction("InspectDevices")); |
| 243 chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams( | 301 chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams( |
| 244 browser, GURL(chrome::kChromeUIInspectURL))); | 302 browser, GURL(chrome::kChromeUIInspectURL))); |
| 245 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE; | 303 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE; |
| 246 ShowSingletonTabOverwritingNTP(browser, params); | 304 ShowSingletonTabOverwritingNTP(browser, params); |
| 247 } | 305 } |
| 248 | 306 |
| 249 void InspectUI::Observe(int type, | 307 void InspectUI::Observe(int type, |
| 250 const content::NotificationSource& source, | 308 const content::NotificationSource& source, |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 scoped_ptr<base::Value> source_value(base::Value::CreateStringValue(source)); | 449 scoped_ptr<base::Value> source_value(base::Value::CreateStringValue(source)); |
| 392 web_ui()->CallJavascriptFunction( | 450 web_ui()->CallJavascriptFunction( |
| 393 "populateTargets", | 451 "populateTargets", |
| 394 *source_value.get(), | 452 *source_value.get(), |
| 395 *targets.get()); | 453 *targets.get()); |
| 396 } | 454 } |
| 397 | 455 |
| 398 void InspectUI::PopulatePortStatus(const base::Value& status) { | 456 void InspectUI::PopulatePortStatus(const base::Value& status) { |
| 399 web_ui()->CallJavascriptFunction("populatePortStatus", status); | 457 web_ui()->CallJavascriptFunction("populatePortStatus", status); |
| 400 } | 458 } |
| OLD | NEW |