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 // Implements the Chrome Extensions Debugger API. | 5 // Implements the Chrome Extensions Debugger API. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/debugger/debugger_api.h" | 7 #include "chrome/browser/extensions/api/debugger/debugger_api.h" |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 | 11 |
| 12 #include "base/command_line.h" | |
| 12 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 13 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" | 20 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" |
| 20 #include "chrome/browser/api/infobars/infobar_service.h" | 21 #include "chrome/browser/api/infobars/infobar_service.h" |
| 21 #include "chrome/browser/extensions/api/debugger/debugger_api_constants.h" | 22 #include "chrome/browser/extensions/api/debugger/debugger_api_constants.h" |
| 22 #include "chrome/browser/extensions/event_router.h" | 23 #include "chrome/browser/extensions/event_router.h" |
| 24 #include "chrome/browser/extensions/extension_service.h" | |
| 23 #include "chrome/browser/extensions/extension_system.h" | 25 #include "chrome/browser/extensions/extension_system.h" |
| 24 #include "chrome/browser/extensions/extension_tab_util.h" | 26 #include "chrome/browser/extensions/extension_tab_util.h" |
| 25 #include "chrome/browser/infobars/infobar.h" | 27 #include "chrome/browser/infobars/infobar.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" | 29 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
| 28 #include "chrome/common/chrome_notification_types.h" | 30 #include "chrome/common/chrome_notification_types.h" |
| 29 #include "chrome/common/extensions/api/debugger.h" | 31 #include "chrome/common/chrome_switches.h" |
| 30 #include "chrome/common/extensions/extension.h" | 32 #include "chrome/common/extensions/extension.h" |
| 31 #include "content/public/browser/devtools_agent_host.h" | 33 #include "content/public/browser/devtools_agent_host.h" |
| 32 #include "content/public/browser/devtools_client_host.h" | 34 #include "content/public/browser/devtools_client_host.h" |
| 33 #include "content/public/browser/devtools_manager.h" | 35 #include "content/public/browser/devtools_manager.h" |
| 34 #include "content/public/browser/notification_service.h" | 36 #include "content/public/browser/notification_service.h" |
| 35 #include "content/public/browser/notification_source.h" | 37 #include "content/public/browser/notification_source.h" |
| 38 #include "content/public/browser/render_process_host.h" | |
| 36 #include "content/public/browser/render_view_host.h" | 39 #include "content/public/browser/render_view_host.h" |
| 40 #include "content/public/browser/render_widget_host.h" | |
| 37 #include "content/public/browser/web_contents.h" | 41 #include "content/public/browser/web_contents.h" |
| 38 #include "content/public/common/content_client.h" | 42 #include "content/public/common/content_client.h" |
| 39 #include "content/public/common/url_constants.h" | 43 #include "content/public/common/url_constants.h" |
| 40 #include "extensions/common/error_utils.h" | 44 #include "extensions/common/error_utils.h" |
| 41 #include "grit/generated_resources.h" | 45 #include "grit/generated_resources.h" |
| 42 #include "ui/base/l10n/l10n_util.h" | 46 #include "ui/base/l10n/l10n_util.h" |
| 43 #include "webkit/glue/webkit_glue.h" | 47 #include "webkit/glue/webkit_glue.h" |
| 44 | 48 |
| 45 using content::DevToolsAgentHost; | 49 using content::DevToolsAgentHost; |
| 46 using content::DevToolsClientHost; | 50 using content::DevToolsClientHost; |
| 47 using content::DevToolsManager; | 51 using content::DevToolsManager; |
| 52 using content::RenderProcessHost; | |
| 53 using content::RenderViewHost; | |
| 54 using content::RenderWidgetHost; | |
| 48 using content::WebContents; | 55 using content::WebContents; |
| 49 using extensions::api::debugger::Debuggee; | |
| 50 using extensions::ErrorUtils; | 56 using extensions::ErrorUtils; |
| 51 | 57 |
| 52 namespace keys = debugger_api_constants; | 58 namespace keys = debugger_api_constants; |
| 53 namespace Attach = extensions::api::debugger::Attach; | 59 namespace Attach = extensions::api::debugger::Attach; |
| 54 namespace Detach = extensions::api::debugger::Detach; | 60 namespace Detach = extensions::api::debugger::Detach; |
| 55 namespace OnDetach = extensions::api::debugger::OnDetach; | 61 namespace OnDetach = extensions::api::debugger::OnDetach; |
| 56 namespace OnEvent = extensions::api::debugger::OnEvent; | 62 namespace OnEvent = extensions::api::debugger::OnEvent; |
| 57 namespace SendCommand = extensions::api::debugger::SendCommand; | 63 namespace SendCommand = extensions::api::debugger::SendCommand; |
| 58 | 64 |
| 59 class ExtensionDevToolsClientHost; | 65 class ExtensionDevToolsClientHost; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 ExtensionDevToolsClientHost* client_host_; | 97 ExtensionDevToolsClientHost* client_host_; |
| 92 DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsInfoBarDelegate); | 98 DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsInfoBarDelegate); |
| 93 }; | 99 }; |
| 94 | 100 |
| 95 class ExtensionDevToolsClientHost : public DevToolsClientHost, | 101 class ExtensionDevToolsClientHost : public DevToolsClientHost, |
| 96 public content::NotificationObserver { | 102 public content::NotificationObserver { |
| 97 public: | 103 public: |
| 98 ExtensionDevToolsClientHost(WebContents* web_contents, | 104 ExtensionDevToolsClientHost(WebContents* web_contents, |
| 99 const std::string& extension_id, | 105 const std::string& extension_id, |
| 100 const std::string& extension_name, | 106 const std::string& extension_name, |
| 101 int tab_id); | 107 const Debuggee& debuggee); |
| 102 | 108 |
| 103 virtual ~ExtensionDevToolsClientHost(); | 109 virtual ~ExtensionDevToolsClientHost(); |
| 104 | 110 |
| 105 bool MatchesContentsAndExtensionId(WebContents* web_contents, | 111 bool MatchesContentsAndExtensionId(WebContents* web_contents, |
| 106 const std::string& extension_id); | 112 const std::string& extension_id); |
| 107 void Close(); | 113 void Close(); |
| 108 void SendMessageToBackend(DebuggerSendCommandFunction* function, | 114 void SendMessageToBackend(DebuggerSendCommandFunction* function, |
| 109 const std::string& method, | 115 const std::string& method, |
| 110 SendCommand::Params::CommandParams* command_params); | 116 SendCommand::Params::CommandParams* command_params); |
| 111 | 117 |
| 112 // Marks connection as to-be-terminated by the user. | 118 // Marks connection as to-be-terminated by the user. |
| 113 void MarkAsDismissed(); | 119 void MarkAsDismissed(); |
| 114 | 120 |
| 115 // DevToolsClientHost interface | 121 // DevToolsClientHost interface |
| 116 virtual void InspectedContentsClosing() OVERRIDE; | 122 virtual void InspectedContentsClosing() OVERRIDE; |
| 117 virtual void DispatchOnInspectorFrontend(const std::string& message) OVERRIDE; | 123 virtual void DispatchOnInspectorFrontend(const std::string& message) OVERRIDE; |
| 118 virtual void ReplacedWithAnotherClient() OVERRIDE; | 124 virtual void ReplacedWithAnotherClient() OVERRIDE; |
| 119 | 125 |
| 120 private: | 126 private: |
| 121 void SendDetachedEvent(); | 127 void SendDetachedEvent(); |
| 122 | 128 |
| 123 // content::NotificationObserver implementation. | 129 // content::NotificationObserver implementation. |
| 124 virtual void Observe(int type, | 130 virtual void Observe(int type, |
| 125 const content::NotificationSource& source, | 131 const content::NotificationSource& source, |
| 126 const content::NotificationDetails& details) OVERRIDE; | 132 const content::NotificationDetails& details) OVERRIDE; |
| 127 | 133 |
| 128 WebContents* web_contents_; | 134 WebContents* web_contents_; |
| 129 std::string extension_id_; | 135 std::string extension_id_; |
| 130 int tab_id_; | 136 Debuggee debuggee_; |
| 131 content::NotificationRegistrar registrar_; | 137 content::NotificationRegistrar registrar_; |
| 132 int last_request_id_; | 138 int last_request_id_; |
| 133 typedef std::map<int, scoped_refptr<DebuggerSendCommandFunction> > | 139 typedef std::map<int, scoped_refptr<DebuggerSendCommandFunction> > |
| 134 PendingRequests; | 140 PendingRequests; |
| 135 PendingRequests pending_requests_; | 141 PendingRequests pending_requests_; |
| 136 ExtensionDevToolsInfoBarDelegate* infobar_delegate_; | 142 ExtensionDevToolsInfoBarDelegate* infobar_delegate_; |
| 137 OnDetach::Reason detach_reason_; | 143 OnDetach::Reason detach_reason_; |
| 138 | 144 |
| 139 DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsClientHost); | 145 DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsClientHost); |
| 140 }; | 146 }; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 171 } | 177 } |
| 172 return NULL; | 178 return NULL; |
| 173 } | 179 } |
| 174 | 180 |
| 175 private: | 181 private: |
| 176 std::set<DevToolsClientHost*> client_hosts_; | 182 std::set<DevToolsClientHost*> client_hosts_; |
| 177 }; | 183 }; |
| 178 | 184 |
| 179 } // namespace | 185 } // namespace |
| 180 | 186 |
| 187 static void CopyDebuggee(Debuggee & dst, const Debuggee& src) { | |
| 188 if (src.tab_id) | |
| 189 dst.tab_id.reset(new int(*src.tab_id)); | |
| 190 if (src.extension_id) | |
| 191 dst.extension_id.reset(new std::string(*src.extension_id)); | |
| 192 } | |
| 193 | |
| 181 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( | 194 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( |
| 182 WebContents* web_contents, | 195 WebContents* web_contents, |
| 183 const std::string& extension_id, | 196 const std::string& extension_id, |
| 184 const std::string& extension_name, | 197 const std::string& extension_name, |
| 185 int tab_id) | 198 const Debuggee& debuggee) |
| 186 : web_contents_(web_contents), | 199 : web_contents_(web_contents), |
| 187 extension_id_(extension_id), | 200 extension_id_(extension_id), |
| 188 tab_id_(tab_id), | |
| 189 last_request_id_(0), | 201 last_request_id_(0), |
| 190 infobar_delegate_(NULL), | 202 infobar_delegate_(NULL), |
| 191 detach_reason_(OnDetach::REASON_TARGET_CLOSED) { | 203 detach_reason_(OnDetach::REASON_TARGET_CLOSED) { |
| 204 CopyDebuggee(debuggee_, debuggee); | |
| 205 | |
| 192 AttachedClientHosts::GetInstance()->Add(this); | 206 AttachedClientHosts::GetInstance()->Add(this); |
| 193 | 207 |
| 194 // Detach from debugger when extension unloads. | 208 // Detach from debugger when extension unloads. |
| 195 Profile* profile = | 209 Profile* profile = |
| 196 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 210 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 197 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 211 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 198 content::Source<Profile>(profile)); | 212 content::Source<Profile>(profile)); |
| 199 | 213 |
| 200 // Attach to debugger and tell it we are ready. | 214 // Attach to debugger and tell it we are ready. |
| 201 scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetFor( | 215 scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetFor( |
| 202 web_contents_->GetRenderViewHost())); | 216 web_contents_->GetRenderViewHost())); |
| 203 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(agent, this); | 217 DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(agent, this); |
| 204 | 218 |
| 205 InfoBarService* infobar_service = | 219 if (!CommandLine::ForCurrentProcess()-> |
| 206 InfoBarService::FromWebContents(web_contents_); | 220 HasSwitch(switches::kExtensionDebuggingSilent)) { |
| 207 infobar_delegate_ = ExtensionDevToolsInfoBarDelegate::Create(infobar_service, | 221 InfoBarService* infobar_service = |
| 208 extension_name, | 222 InfoBarService::FromWebContents(web_contents_); |
| 209 this); | 223 infobar_delegate_ = ExtensionDevToolsInfoBarDelegate::Create( |
| 210 if (infobar_delegate_) { | 224 infobar_service, extension_name, this); |
| 211 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 225 if (infobar_delegate_) { |
| 212 content::Source<InfoBarService>(infobar_service)); | 226 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 227 content::Source<InfoBarService>(infobar_service)); | |
| 228 } | |
| 213 } | 229 } |
| 214 } | 230 } |
| 215 | 231 |
| 216 ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { | 232 ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { |
| 217 // Ensure calling RemoveInfoBar() below won't result in Observe() trying to | 233 // Ensure calling RemoveInfoBar() below won't result in Observe() trying to |
| 218 // Close() us. | 234 // Close() us. |
| 219 registrar_.RemoveAll(); | 235 registrar_.RemoveAll(); |
| 220 | 236 |
| 221 if (infobar_delegate_) { | 237 if (infobar_delegate_) { |
| 222 infobar_delegate_->DiscardClientHost(); | 238 infobar_delegate_->DiscardClientHost(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 | 286 |
| 271 void ExtensionDevToolsClientHost::MarkAsDismissed() { | 287 void ExtensionDevToolsClientHost::MarkAsDismissed() { |
| 272 detach_reason_ = OnDetach::REASON_CANCELED_BY_USER; | 288 detach_reason_ = OnDetach::REASON_CANCELED_BY_USER; |
| 273 } | 289 } |
| 274 | 290 |
| 275 void ExtensionDevToolsClientHost::SendDetachedEvent() { | 291 void ExtensionDevToolsClientHost::SendDetachedEvent() { |
| 276 Profile* profile = | 292 Profile* profile = |
| 277 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 293 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 278 if (profile != NULL && | 294 if (profile != NULL && |
| 279 extensions::ExtensionSystem::Get(profile)->event_router()) { | 295 extensions::ExtensionSystem::Get(profile)->event_router()) { |
| 280 Debuggee debuggee; | 296 scoped_ptr<base::ListValue> args(OnDetach::Create(debuggee_, |
| 281 debuggee.tab_id = tab_id_; | |
| 282 scoped_ptr<base::ListValue> args(OnDetach::Create(debuggee, | |
| 283 detach_reason_)); | 297 detach_reason_)); |
| 284 scoped_ptr<extensions::Event> event(new extensions::Event( | 298 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 285 keys::kOnDetach, args.Pass())); | 299 keys::kOnDetach, args.Pass())); |
| 286 event->restrict_to_profile = profile; | 300 event->restrict_to_profile = profile; |
| 287 extensions::ExtensionSystem::Get(profile)->event_router()-> | 301 extensions::ExtensionSystem::Get(profile)->event_router()-> |
| 288 DispatchEventToExtension(extension_id_, event.Pass()); | 302 DispatchEventToExtension(extension_id_, event.Pass()); |
| 289 } | 303 } |
| 290 } | 304 } |
| 291 | 305 |
| 292 void ExtensionDevToolsClientHost::Observe( | 306 void ExtensionDevToolsClientHost::Observe( |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 322 if (!result->IsType(Value::TYPE_DICTIONARY)) | 336 if (!result->IsType(Value::TYPE_DICTIONARY)) |
| 323 return; | 337 return; |
| 324 DictionaryValue* dictionary = static_cast<DictionaryValue*>(result.get()); | 338 DictionaryValue* dictionary = static_cast<DictionaryValue*>(result.get()); |
| 325 | 339 |
| 326 int id; | 340 int id; |
| 327 if (!dictionary->GetInteger("id", &id)) { | 341 if (!dictionary->GetInteger("id", &id)) { |
| 328 std::string method_name; | 342 std::string method_name; |
| 329 if (!dictionary->GetString("method", &method_name)) | 343 if (!dictionary->GetString("method", &method_name)) |
| 330 return; | 344 return; |
| 331 | 345 |
| 332 Debuggee debuggee; | |
| 333 debuggee.tab_id = tab_id_; | |
| 334 | |
| 335 OnEvent::Params params; | 346 OnEvent::Params params; |
| 336 DictionaryValue* params_value; | 347 DictionaryValue* params_value; |
| 337 if (dictionary->GetDictionary("params", ¶ms_value)) | 348 if (dictionary->GetDictionary("params", ¶ms_value)) |
| 338 params.additional_properties.Swap(params_value); | 349 params.additional_properties.Swap(params_value); |
| 339 | 350 |
| 340 scoped_ptr<ListValue> args(OnEvent::Create(debuggee, method_name, params)); | 351 scoped_ptr<ListValue> args(OnEvent::Create(debuggee_, method_name, params)); |
| 341 scoped_ptr<extensions::Event> event(new extensions::Event( | 352 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 342 keys::kOnEvent, args.Pass())); | 353 keys::kOnEvent, args.Pass())); |
| 343 event->restrict_to_profile = profile; | 354 event->restrict_to_profile = profile; |
| 344 extensions::ExtensionSystem::Get(profile)->event_router()-> | 355 extensions::ExtensionSystem::Get(profile)->event_router()-> |
| 345 DispatchEventToExtension(extension_id_, event.Pass()); | 356 DispatchEventToExtension(extension_id_, event.Pass()); |
| 346 } else { | 357 } else { |
| 347 DebuggerSendCommandFunction* function = pending_requests_[id]; | 358 DebuggerSendCommandFunction* function = pending_requests_[id]; |
| 348 if (!function) | 359 if (!function) |
| 349 return; | 360 return; |
| 350 | 361 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 } | 415 } |
| 405 | 416 |
| 406 bool ExtensionDevToolsInfoBarDelegate::Cancel() { | 417 bool ExtensionDevToolsInfoBarDelegate::Cancel() { |
| 407 if (client_host_) | 418 if (client_host_) |
| 408 client_host_->MarkAsDismissed(); | 419 client_host_->MarkAsDismissed(); |
| 409 return true; | 420 return true; |
| 410 } | 421 } |
| 411 | 422 |
| 412 DebuggerFunction::DebuggerFunction() | 423 DebuggerFunction::DebuggerFunction() |
| 413 : contents_(0), | 424 : contents_(0), |
| 414 tab_id_(0), | |
| 415 client_host_(0) { | 425 client_host_(0) { |
| 416 } | 426 } |
| 417 | 427 |
| 428 void DebuggerFunction::FormatErrorMessage(const std::string& format) { | |
| 429 error_ = ErrorUtils::FormatErrorMessage( | |
| 430 format, | |
| 431 debuggee_.tab_id ? | |
| 432 keys::kTabTargetType : | |
| 433 keys::kExtensionTargetType, | |
| 434 debuggee_.tab_id ? | |
| 435 base::IntToString(*debuggee_.tab_id) : | |
| 436 *debuggee_.extension_id); | |
| 437 } | |
| 438 | |
| 439 static WebContents* FindWebContentsForExtension( | |
|
Matt Perry
2013/02/20 19:52:01
There can be multiple WebContents for an extension
Vladislav Kaznacheev
2013/02/21 09:39:09
Thanks for the pointer! Done.
On 2013/02/20 19:52:
| |
| 440 const extensions::Extension* extension) { | |
| 441 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | |
| 442 !it.IsAtEnd(); it.Advance()) { | |
| 443 RenderProcessHost* render_process_host = it.GetCurrentValue(); | |
| 444 DCHECK(render_process_host); | |
| 445 | |
| 446 // Ignore processes that don't have a connection, such as crashed tabs. | |
| 447 if (!render_process_host->HasConnection()) | |
| 448 continue; | |
| 449 | |
| 450 RenderProcessHost::RenderWidgetHostsIterator rwit( | |
| 451 render_process_host->GetRenderWidgetHostsIterator()); | |
| 452 for (; !rwit.IsAtEnd(); rwit.Advance()) { | |
| 453 const RenderWidgetHost* widget = rwit.GetCurrentValue(); | |
| 454 DCHECK(widget); | |
| 455 if (!widget || !widget->IsRenderView()) | |
| 456 continue; | |
| 457 | |
| 458 RenderViewHost* rvh = | |
| 459 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget)); | |
| 460 | |
| 461 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); | |
| 462 if (web_contents) { | |
| 463 GURL url = web_contents->GetURL(); | |
| 464 if (url.host() == extension->id()) { | |
| 465 return web_contents; | |
| 466 } | |
| 467 } | |
| 468 } | |
| 469 } | |
| 470 return NULL; | |
| 471 } | |
| 472 | |
| 418 bool DebuggerFunction::InitWebContents() { | 473 bool DebuggerFunction::InitWebContents() { |
| 419 // Find the WebContents that contains this tab id. | 474 // Find the WebContents that contains this tab id. |
| 420 contents_ = NULL; | 475 contents_ = NULL; |
| 421 WebContents* web_contents = NULL; | 476 if (debuggee_.tab_id) { |
| 422 bool result = ExtensionTabUtil::GetTabById( | 477 WebContents* web_contents = NULL; |
| 423 tab_id_, profile(), include_incognito(), NULL, NULL, &web_contents, NULL); | 478 bool result = ExtensionTabUtil::GetTabById( |
| 424 if (!result || !web_contents) { | 479 *debuggee_.tab_id, profile(), include_incognito(), NULL, NULL, |
| 425 error_ = ErrorUtils::FormatErrorMessage( | 480 &web_contents, NULL); |
| 426 keys::kNoTabError, | 481 if (!result || !web_contents) { |
| 427 base::IntToString(tab_id_)); | 482 FormatErrorMessage(keys::kNoTargetError); |
| 428 return false; | 483 return false; |
| 484 } | |
| 485 contents_ = web_contents; | |
| 486 | |
| 487 if (content::HasWebUIScheme(contents_->GetURL())) { | |
| 488 error_ = ErrorUtils::FormatErrorMessage( | |
| 489 keys::kAttachToWebUIError, | |
| 490 contents_->GetURL().scheme()); | |
| 491 return false; | |
| 492 } | |
| 493 | |
| 494 return true; | |
| 429 } | 495 } |
| 430 contents_ = web_contents; | |
| 431 | 496 |
| 432 if (content::HasWebUIScheme(contents_->GetURL())) { | 497 if (debuggee_.extension_id) { |
| 433 error_ = ErrorUtils::FormatErrorMessage( | 498 if (!CommandLine::ForCurrentProcess()-> |
| 434 keys::kAttachToWebUIError, | 499 HasSwitch(switches::kExtensionDebuggingSilent)) { |
| 435 contents_->GetURL().scheme()); | 500 error_ = ErrorUtils::FormatErrorMessage( |
| 501 keys::kSilentDebuggingRequired, | |
| 502 switches::kExtensionDebuggingSilent); | |
| 503 return false; | |
| 504 } | |
| 505 | |
| 506 const extensions::Extension* extension = profile()->GetExtensionService()-> | |
| 507 extensions()->GetByID(*debuggee_.extension_id); | |
| 508 if (extension) { | |
| 509 contents_ = FindWebContentsForExtension(extension); | |
| 510 if (contents_) | |
| 511 return true; | |
| 512 } | |
| 513 | |
| 514 FormatErrorMessage(keys::kNoTargetError); | |
| 436 return false; | 515 return false; |
| 437 } | 516 } |
| 438 | 517 |
| 439 return true; | 518 error_ = keys::kInvalidTargetError; |
| 519 return false; | |
| 440 } | 520 } |
| 441 | 521 |
| 442 bool DebuggerFunction::InitClientHost() { | 522 bool DebuggerFunction::InitClientHost() { |
| 443 if (!InitWebContents()) | 523 if (!InitWebContents()) |
| 444 return false; | 524 return false; |
| 445 | 525 |
| 446 // Don't fetch rvh from the contents since it'll be wrong upon navigation. | 526 // Don't fetch rvh from the contents since it'll be wrong upon navigation. |
| 447 client_host_ = AttachedClientHosts::GetInstance()->Lookup(contents_); | 527 client_host_ = AttachedClientHosts::GetInstance()->Lookup(contents_); |
| 448 | 528 |
| 449 if (!client_host_ || | 529 if (!client_host_ || |
| 450 !client_host_->MatchesContentsAndExtensionId(contents_, | 530 !client_host_->MatchesContentsAndExtensionId(contents_, |
| 451 GetExtension()->id())) { | 531 GetExtension()->id())) { |
| 452 error_ = ErrorUtils::FormatErrorMessage( | 532 FormatErrorMessage(keys::kNotAttachedError); |
| 453 keys::kNotAttachedError, | |
| 454 base::IntToString(tab_id_)); | |
| 455 return false; | 533 return false; |
| 456 } | 534 } |
| 457 return true; | 535 return true; |
| 458 } | 536 } |
| 459 | 537 |
| 460 DebuggerAttachFunction::DebuggerAttachFunction() {} | 538 DebuggerAttachFunction::DebuggerAttachFunction() {} |
| 461 | 539 |
| 462 DebuggerAttachFunction::~DebuggerAttachFunction() {} | 540 DebuggerAttachFunction::~DebuggerAttachFunction() {} |
| 463 | 541 |
| 464 bool DebuggerAttachFunction::RunImpl() { | 542 bool DebuggerAttachFunction::RunImpl() { |
| 465 scoped_ptr<Attach::Params> params(Attach::Params::Create(*args_)); | 543 scoped_ptr<Attach::Params> params(Attach::Params::Create(*args_)); |
| 466 EXTENSION_FUNCTION_VALIDATE(params.get()); | 544 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 467 | 545 |
| 468 tab_id_ = params->target.tab_id; | 546 CopyDebuggee(debuggee_, params->target); |
| 469 if (!InitWebContents()) | 547 if (!InitWebContents()) |
| 470 return false; | 548 return false; |
| 471 | 549 |
| 472 if (!webkit_glue::IsInspectorProtocolVersionSupported( | 550 if (!webkit_glue::IsInspectorProtocolVersionSupported( |
| 473 params->required_version)) { | 551 params->required_version)) { |
| 474 error_ = ErrorUtils::FormatErrorMessage( | 552 error_ = ErrorUtils::FormatErrorMessage( |
| 475 keys::kProtocolVersionNotSupportedError, | 553 keys::kProtocolVersionNotSupportedError, |
| 476 params->required_version); | 554 params->required_version); |
| 477 return false; | 555 return false; |
| 478 } | 556 } |
| 479 | 557 |
| 480 scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetFor( | 558 scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetFor( |
| 481 contents_->GetRenderViewHost())); | 559 contents_->GetRenderViewHost())); |
| 482 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> | 560 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> |
| 483 GetDevToolsClientHostFor(agent); | 561 GetDevToolsClientHostFor(agent); |
| 484 | 562 |
| 485 if (client_host != NULL) { | 563 if (client_host != NULL) { |
| 486 error_ = ErrorUtils::FormatErrorMessage( | 564 FormatErrorMessage(keys::kAlreadyAttachedError); |
| 487 keys::kAlreadyAttachedError, | |
| 488 base::IntToString(tab_id_)); | |
| 489 return false; | 565 return false; |
| 490 } | 566 } |
| 491 | 567 |
| 492 new ExtensionDevToolsClientHost(contents_, | 568 new ExtensionDevToolsClientHost(contents_, |
| 493 GetExtension()->id(), | 569 GetExtension()->id(), |
| 494 GetExtension()->name(), | 570 GetExtension()->name(), |
| 495 tab_id_); | 571 debuggee_); |
| 496 SendResponse(true); | 572 SendResponse(true); |
| 497 return true; | 573 return true; |
| 498 } | 574 } |
| 499 | 575 |
| 500 DebuggerDetachFunction::DebuggerDetachFunction() {} | 576 DebuggerDetachFunction::DebuggerDetachFunction() {} |
| 501 | 577 |
| 502 DebuggerDetachFunction::~DebuggerDetachFunction() {} | 578 DebuggerDetachFunction::~DebuggerDetachFunction() {} |
| 503 | 579 |
| 504 bool DebuggerDetachFunction::RunImpl() { | 580 bool DebuggerDetachFunction::RunImpl() { |
| 505 scoped_ptr<Detach::Params> params(Detach::Params::Create(*args_)); | 581 scoped_ptr<Detach::Params> params(Detach::Params::Create(*args_)); |
| 506 EXTENSION_FUNCTION_VALIDATE(params.get()); | 582 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 507 | 583 |
| 508 tab_id_ = params->target.tab_id; | 584 CopyDebuggee(debuggee_, params->target); |
| 509 if (!InitClientHost()) | 585 if (!InitClientHost()) |
| 510 return false; | 586 return false; |
| 511 | 587 |
| 512 client_host_->Close(); | 588 client_host_->Close(); |
| 513 SendResponse(true); | 589 SendResponse(true); |
| 514 return true; | 590 return true; |
| 515 } | 591 } |
| 516 | 592 |
| 517 DebuggerSendCommandFunction::DebuggerSendCommandFunction() {} | 593 DebuggerSendCommandFunction::DebuggerSendCommandFunction() {} |
| 518 | 594 |
| 519 DebuggerSendCommandFunction::~DebuggerSendCommandFunction() {} | 595 DebuggerSendCommandFunction::~DebuggerSendCommandFunction() {} |
| 520 | 596 |
| 521 bool DebuggerSendCommandFunction::RunImpl() { | 597 bool DebuggerSendCommandFunction::RunImpl() { |
| 522 scoped_ptr<SendCommand::Params> params(SendCommand::Params::Create(*args_)); | 598 scoped_ptr<SendCommand::Params> params(SendCommand::Params::Create(*args_)); |
| 523 EXTENSION_FUNCTION_VALIDATE(params.get()); | 599 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 524 | 600 |
| 525 tab_id_ = params->target.tab_id; | 601 CopyDebuggee(debuggee_, params->target); |
| 526 if (!InitClientHost()) | 602 if (!InitClientHost()) |
| 527 return false; | 603 return false; |
| 528 | 604 |
| 529 client_host_->SendMessageToBackend(this, params->method, | 605 client_host_->SendMessageToBackend(this, params->method, |
| 530 params->command_params.get()); | 606 params->command_params.get()); |
| 531 return true; | 607 return true; |
| 532 } | 608 } |
| 533 | 609 |
| 534 void DebuggerSendCommandFunction::SendResponseBody( | 610 void DebuggerSendCommandFunction::SendResponseBody( |
| 535 DictionaryValue* response) { | 611 DictionaryValue* response) { |
| 536 Value* error_body; | 612 Value* error_body; |
| 537 if (response->Get("error", &error_body)) { | 613 if (response->Get("error", &error_body)) { |
| 538 base::JSONWriter::Write(error_body, &error_); | 614 base::JSONWriter::Write(error_body, &error_); |
| 539 SendResponse(false); | 615 SendResponse(false); |
| 540 return; | 616 return; |
| 541 } | 617 } |
| 542 | 618 |
| 543 DictionaryValue* result_body; | 619 DictionaryValue* result_body; |
| 544 SendCommand::Results::Result result; | 620 SendCommand::Results::Result result; |
| 545 if (response->GetDictionary("result", &result_body)) | 621 if (response->GetDictionary("result", &result_body)) |
| 546 result.additional_properties.Swap(result_body); | 622 result.additional_properties.Swap(result_body); |
| 547 | 623 |
| 548 results_ = SendCommand::Results::Create(result); | 624 results_ = SendCommand::Results::Create(result); |
| 549 SendResponse(true); | 625 SendResponse(true); |
| 550 } | 626 } |
| OLD | NEW |