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> |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 #include "chrome/browser/extensions/extension_tab_util.h" | 27 #include "chrome/browser/extensions/extension_tab_util.h" |
| 28 #include "chrome/browser/infobars/infobar.h" | 28 #include "chrome/browser/infobars/infobar.h" |
| 29 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
| 30 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" | 30 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
| 31 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
| 32 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
| 33 #include "chrome/common/extensions/extension.h" | 33 #include "chrome/common/extensions/extension.h" |
| 34 #include "content/public/browser/devtools_agent_host.h" | 34 #include "content/public/browser/devtools_agent_host.h" |
| 35 #include "content/public/browser/devtools_client_host.h" | 35 #include "content/public/browser/devtools_client_host.h" |
| 36 #include "content/public/browser/devtools_manager.h" | 36 #include "content/public/browser/devtools_manager.h" |
| 37 #include "content/public/browser/favicon_status.h" | |
| 38 #include "content/public/browser/navigation_entry.h" | |
| 37 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
| 38 #include "content/public/browser/notification_source.h" | 40 #include "content/public/browser/notification_source.h" |
| 39 #include "content/public/browser/render_process_host.h" | 41 #include "content/public/browser/render_process_host.h" |
| 40 #include "content/public/browser/render_view_host.h" | 42 #include "content/public/browser/render_view_host.h" |
| 41 #include "content/public/browser/render_widget_host.h" | 43 #include "content/public/browser/render_widget_host.h" |
| 42 #include "content/public/browser/web_contents.h" | 44 #include "content/public/browser/web_contents.h" |
| 43 #include "content/public/common/content_client.h" | 45 #include "content/public/common/content_client.h" |
| 44 #include "content/public/common/url_constants.h" | 46 #include "content/public/common/url_constants.h" |
| 45 #include "extensions/common/error_utils.h" | 47 #include "extensions/common/error_utils.h" |
| 46 #include "grit/generated_resources.h" | 48 #include "grit/generated_resources.h" |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 if (rvh && WebContents::FromRenderViewHost(rvh) == contents) | 178 if (rvh && WebContents::FromRenderViewHost(rvh) == contents) |
| 177 return static_cast<ExtensionDevToolsClientHost*>(*it); | 179 return static_cast<ExtensionDevToolsClientHost*>(*it); |
| 178 } | 180 } |
| 179 return NULL; | 181 return NULL; |
| 180 } | 182 } |
| 181 | 183 |
| 182 private: | 184 private: |
| 183 std::set<DevToolsClientHost*> client_hosts_; | 185 std::set<DevToolsClientHost*> client_hosts_; |
| 184 }; | 186 }; |
| 185 | 187 |
| 188 static extensions::ExtensionHost* GetExtensionBackgroundHost( | |
| 189 WebContents* web_contents) { | |
| 190 Profile* profile = | |
| 191 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 192 if (!profile) | |
| 193 return NULL; | |
| 194 | |
| 195 extensions::ExtensionHost* extension_host = | |
| 196 extensions::ExtensionSystem::Get(profile)->process_manager()-> | |
| 197 GetBackgroundHostForExtension(web_contents->GetURL().host()); | |
| 198 | |
| 199 if (extension_host && extension_host->host_contents() == web_contents) | |
| 200 return extension_host; | |
| 201 | |
| 202 return NULL; | |
| 203 } | |
| 204 | |
| 205 static base::DictionaryValue* SerializePageInfo(RenderViewHost* rvh) { | |
| 206 DevToolsAgentHost* agent_host = DevToolsAgentHost::GetFor(rvh); | |
| 207 | |
| 208 base::DictionaryValue* dictionary = new base::DictionaryValue; | |
|
pfeldman
2013/03/15 11:25:45
new base::DictionaryValue()
Vladislav Kaznacheev
2013/03/18 06:40:36
Done.
| |
| 209 | |
| 210 dictionary->SetString("id", agent_host->GetId()); | |
|
pfeldman
2013/03/15 11:25:45
You should use constants for these.
Vladislav Kaznacheev
2013/03/18 06:40:37
Done.
| |
| 211 | |
| 212 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); | |
| 213 extensions::ExtensionHost* extension_host = | |
| 214 GetExtensionBackgroundHost(web_contents); | |
| 215 if (extension_host) { | |
| 216 dictionary->SetString("type", "extension"); | |
|
pfeldman
2013/03/15 11:25:45
I don't think we are ready to finalize this field.
Vladislav Kaznacheev
2013/03/18 06:40:37
Do you mean you prefer this called "other"? Please
| |
| 217 dictionary->SetString("title", extension_host->extension()->name()); | |
| 218 } else { | |
| 219 dictionary->SetString("type", "page"); | |
| 220 if (web_contents) | |
| 221 dictionary->SetString("title", | |
| 222 UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle()))); | |
| 223 } | |
| 224 | |
| 225 dictionary->SetBoolean("attached", | |
| 226 !!DevToolsManager::GetInstance()->GetDevToolsClientHostFor(agent_host)); | |
| 227 if (web_contents) { | |
| 228 dictionary->SetString("url", web_contents->GetURL().spec()); | |
| 229 content::NavigationController& controller = web_contents->GetController(); | |
| 230 content::NavigationEntry* entry = controller.GetActiveEntry(); | |
| 231 if (entry != NULL && entry->GetURL().is_valid()) { | |
| 232 dictionary->SetString("faviconUrl", entry->GetFavicon().url.spec()); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 return dictionary; | |
| 237 } | |
| 238 | |
| 186 } // namespace | 239 } // namespace |
| 187 | 240 |
| 188 static void CopyDebuggee(Debuggee & dst, const Debuggee& src) { | 241 static void CopyDebuggee(Debuggee & dst, const Debuggee& src) { |
| 189 if (src.tab_id) | 242 if (src.tab_id) |
| 190 dst.tab_id.reset(new int(*src.tab_id)); | 243 dst.tab_id.reset(new int(*src.tab_id)); |
| 191 if (src.extension_id) | 244 if (src.extension_id) |
| 192 dst.extension_id.reset(new std::string(*src.extension_id)); | 245 dst.extension_id.reset(new std::string(*src.extension_id)); |
| 246 if (src.target_id) | |
| 247 dst.target_id.reset(new std::string(*src.target_id)); | |
| 193 } | 248 } |
| 194 | 249 |
| 195 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( | 250 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( |
| 196 WebContents* web_contents, | 251 WebContents* web_contents, |
| 197 const std::string& extension_id, | 252 const std::string& extension_id, |
| 198 const std::string& extension_name, | 253 const std::string& extension_name, |
| 199 const Debuggee& debuggee) | 254 const Debuggee& debuggee) |
| 200 : web_contents_(web_contents), | 255 : web_contents_(web_contents), |
| 201 extension_id_(extension_id), | 256 extension_id_(extension_id), |
| 202 last_request_id_(0), | 257 last_request_id_(0), |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 424 DebuggerFunction::DebuggerFunction() | 479 DebuggerFunction::DebuggerFunction() |
| 425 : contents_(0), | 480 : contents_(0), |
| 426 client_host_(0) { | 481 client_host_(0) { |
| 427 } | 482 } |
| 428 | 483 |
| 429 void DebuggerFunction::FormatErrorMessage(const std::string& format) { | 484 void DebuggerFunction::FormatErrorMessage(const std::string& format) { |
| 430 error_ = ErrorUtils::FormatErrorMessage( | 485 error_ = ErrorUtils::FormatErrorMessage( |
| 431 format, | 486 format, |
| 432 debuggee_.tab_id ? | 487 debuggee_.tab_id ? |
| 433 keys::kTabTargetType : | 488 keys::kTabTargetType : |
| 434 keys::kExtensionTargetType, | 489 debuggee_.extension_id ? |
|
pfeldman
2013/03/15 11:25:45
Consider extracting this.
Vladislav Kaznacheev
2013/03/18 06:40:37
Done.
| |
| 490 keys::kExtensionTargetType : | |
| 491 keys::kOpaqueTargetType, | |
| 435 debuggee_.tab_id ? | 492 debuggee_.tab_id ? |
| 436 base::IntToString(*debuggee_.tab_id) : | 493 base::IntToString(*debuggee_.tab_id) : |
| 437 *debuggee_.extension_id); | 494 debuggee_.extension_id ? |
| 495 *debuggee_.extension_id : | |
| 496 *debuggee_.target_id); | |
| 438 } | 497 } |
| 439 | 498 |
| 440 bool DebuggerFunction::InitWebContents() { | 499 bool DebuggerFunction::InitWebContents() { |
| 441 // Find the WebContents that contains this tab id. | 500 // Find the WebContents that contains this tab id. |
| 442 contents_ = NULL; | 501 contents_ = NULL; |
| 443 if (debuggee_.tab_id) { | 502 if (debuggee_.tab_id) { |
| 444 WebContents* web_contents = NULL; | 503 WebContents* web_contents = NULL; |
| 445 bool result = ExtensionTabUtil::GetTabById( | 504 bool result = ExtensionTabUtil::GetTabById( |
| 446 *debuggee_.tab_id, profile(), include_incognito(), NULL, NULL, | 505 *debuggee_.tab_id, profile(), include_incognito(), NULL, NULL, |
| 447 &web_contents, NULL); | 506 &web_contents, NULL); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 476 if (host) { | 535 if (host) { |
| 477 contents_ = WebContents::FromRenderViewHost(host->render_view_host()); | 536 contents_ = WebContents::FromRenderViewHost(host->render_view_host()); |
| 478 if (contents_) | 537 if (contents_) |
| 479 return true; | 538 return true; |
| 480 } | 539 } |
| 481 | 540 |
| 482 FormatErrorMessage(keys::kNoTargetError); | 541 FormatErrorMessage(keys::kNoTargetError); |
| 483 return false; | 542 return false; |
| 484 } | 543 } |
| 485 | 544 |
| 545 if (debuggee_.target_id) { | |
| 546 DevToolsAgentHost* agent_host = | |
| 547 DevToolsAgentHost::GetForId(*debuggee_.target_id); | |
| 548 if (agent_host) { | |
| 549 contents_ = WebContents::FromRenderViewHost( | |
| 550 agent_host->GetRenderViewHost()); | |
| 551 | |
| 552 if (!CommandLine::ForCurrentProcess()-> | |
| 553 HasSwitch(switches::kSilentDebuggerExtensionAPI)) { | |
| 554 // Allow only tabs, reject background pages. | |
| 555 if (GetExtensionBackgroundHost(contents_)) { | |
| 556 error_ = ErrorUtils::FormatErrorMessage( | |
| 557 keys::kSilentDebuggingRequired, | |
| 558 switches::kSilentDebuggerExtensionAPI); | |
| 559 return false; | |
| 560 } | |
| 561 } | |
| 562 return true; | |
| 563 } | |
| 564 FormatErrorMessage(keys::kNoTargetError); | |
| 565 return false; | |
| 566 } | |
| 567 | |
| 486 error_ = keys::kInvalidTargetError; | 568 error_ = keys::kInvalidTargetError; |
| 487 return false; | 569 return false; |
| 488 } | 570 } |
| 489 | 571 |
| 490 bool DebuggerFunction::InitClientHost() { | 572 bool DebuggerFunction::InitClientHost() { |
| 491 if (!InitWebContents()) | 573 if (!InitWebContents()) |
| 492 return false; | 574 return false; |
| 493 | 575 |
| 494 // Don't fetch rvh from the contents since it'll be wrong upon navigation. | 576 // Don't fetch rvh from the contents since it'll be wrong upon navigation. |
| 495 client_host_ = AttachedClientHosts::GetInstance()->Lookup(contents_); | 577 client_host_ = AttachedClientHosts::GetInstance()->Lookup(contents_); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 585 } | 667 } |
| 586 | 668 |
| 587 DictionaryValue* result_body; | 669 DictionaryValue* result_body; |
| 588 SendCommand::Results::Result result; | 670 SendCommand::Results::Result result; |
| 589 if (response->GetDictionary("result", &result_body)) | 671 if (response->GetDictionary("result", &result_body)) |
| 590 result.additional_properties.Swap(result_body); | 672 result.additional_properties.Swap(result_body); |
| 591 | 673 |
| 592 results_ = SendCommand::Results::Create(result); | 674 results_ = SendCommand::Results::Create(result); |
| 593 SendResponse(true); | 675 SendResponse(true); |
| 594 } | 676 } |
| 677 | |
| 678 DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() {} | |
| 679 | |
| 680 DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() {} | |
| 681 | |
| 682 bool DebuggerGetTargetsFunction::RunImpl() { | |
| 683 base::ListValue* results_list = new ListValue(); | |
| 684 | |
| 685 std::vector<RenderViewHost*> rvh_list = | |
| 686 DevToolsAgentHost::GetValidRenderViewHosts(); | |
| 687 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin(); | |
| 688 it != rvh_list.end(); ++it) | |
| 689 results_list->Append(SerializePageInfo(*it)); | |
| 690 | |
| 691 SetResult(results_list); | |
| 692 SendResponse(true); | |
| 693 return true; | |
| 694 } | |
| OLD | NEW |