Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chrome/browser/extensions/api/debugger/debugger_api.cc

Issue 12377047: Add chrome.debugger.getTargets method to discover available debug targets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@targets
Patch Set: Rebased, addressed comments Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 const char kTargetIdField[] = "id";
206 static const char kTargetTypeField[] = "type";
207 static const char kTargetTitleField[] = "title";
208 static const char kTargetAttachedField[] = "attached";
209 static const char kTargetUrlField[] = "url";
210 static const char kTargetFaviconUrlField[] = "favicon_url";
211
212 static const char kTargetTypePage[] = "page";
213 static const char kTargetTypeExtension[] = "extension";
214
215 static base::DictionaryValue* SerializePageInfo(RenderViewHost* rvh) {
216 DevToolsAgentHost* agent_host = DevToolsAgentHost::GetOrCreateFor(rvh);
217
218 base::DictionaryValue* dictionary = new base::DictionaryValue();
219
220 dictionary->SetString(kTargetIdField, agent_host->GetId());
221
222 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
223 extensions::ExtensionHost* extension_host =
224 GetExtensionBackgroundHost(web_contents);
Matt Perry 2013/03/18 19:53:44 This is a very roundabout way to get the extension
Vladislav Kaznacheev 2013/03/19 05:56:23 It is not only only about the name. I need to know
Matt Perry 2013/03/19 18:02:01 OK, so you really do only want this to apply to ba
Vladislav Kaznacheev 2013/03/20 07:20:49 Done.
225 if (extension_host) {
226 dictionary->SetString(kTargetTypeField, kTargetTypeExtension);
227 dictionary->SetString(kTargetTitleField,
228 extension_host->extension()->name());
229 } else {
230 dictionary->SetString(kTargetTypeField, kTargetTypePage);
231 if (web_contents)
232 dictionary->SetString(kTargetTitleField,
233 UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle())));
234 }
235
236 dictionary->SetBoolean(kTargetAttachedField,
237 !!DevToolsManager::GetInstance()->GetDevToolsClientHostFor(agent_host));
238 if (web_contents) {
Matt Perry 2013/03/18 19:53:44 Can web_contents actually be NULL? You use it in G
Vladislav Kaznacheev 2013/03/19 05:56:23 You are right. This came from inspect_ui.cc where
239 dictionary->SetString(kTargetUrlField, web_contents->GetURL().spec());
240 content::NavigationController& controller = web_contents->GetController();
241 content::NavigationEntry* entry = controller.GetActiveEntry();
242 if (entry != NULL && entry->GetURL().is_valid()) {
243 dictionary->SetString(kTargetFaviconUrlField,
244 entry->GetFavicon().url.spec());
245 }
246 }
247
248 return dictionary;
249 }
250
186 } // namespace 251 } // namespace
187 252
188 static void CopyDebuggee(Debuggee & dst, const Debuggee& src) { 253 static void CopyDebuggee(Debuggee & dst, const Debuggee& src) {
189 if (src.tab_id) 254 if (src.tab_id)
190 dst.tab_id.reset(new int(*src.tab_id)); 255 dst.tab_id.reset(new int(*src.tab_id));
191 if (src.extension_id) 256 if (src.extension_id)
192 dst.extension_id.reset(new std::string(*src.extension_id)); 257 dst.extension_id.reset(new std::string(*src.extension_id));
258 if (src.target_id)
259 dst.target_id.reset(new std::string(*src.target_id));
193 } 260 }
194 261
195 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( 262 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost(
196 WebContents* web_contents, 263 WebContents* web_contents,
197 const std::string& extension_id, 264 const std::string& extension_id,
198 const std::string& extension_name, 265 const std::string& extension_name,
199 const Debuggee& debuggee) 266 const Debuggee& debuggee)
200 : web_contents_(web_contents), 267 : web_contents_(web_contents),
201 extension_id_(extension_id), 268 extension_id_(extension_id),
202 last_request_id_(0), 269 last_request_id_(0),
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 client_host_->MarkAsDismissed(); 487 client_host_->MarkAsDismissed();
421 return true; 488 return true;
422 } 489 }
423 490
424 DebuggerFunction::DebuggerFunction() 491 DebuggerFunction::DebuggerFunction()
425 : contents_(0), 492 : contents_(0),
426 client_host_(0) { 493 client_host_(0) {
427 } 494 }
428 495
429 void DebuggerFunction::FormatErrorMessage(const std::string& format) { 496 void DebuggerFunction::FormatErrorMessage(const std::string& format) {
430 error_ = ErrorUtils::FormatErrorMessage( 497 if (debuggee_.tab_id)
431 format, 498 error_ = ErrorUtils::FormatErrorMessage(
432 debuggee_.tab_id ? 499 format, keys::kTabTargetType, base::IntToString(*debuggee_.tab_id));
433 keys::kTabTargetType : 500 else if (debuggee_.extension_id)
434 keys::kExtensionTargetType, 501 error_ = ErrorUtils::FormatErrorMessage(
435 debuggee_.tab_id ? 502 format, keys::kExtensionTargetType, *debuggee_.extension_id);
436 base::IntToString(*debuggee_.tab_id) : 503 else
437 *debuggee_.extension_id); 504 error_ = ErrorUtils::FormatErrorMessage(
505 format, keys::kOpaqueTargetType, *debuggee_.target_id);
438 } 506 }
439 507
440 bool DebuggerFunction::InitWebContents() { 508 bool DebuggerFunction::InitWebContents() {
441 // Find the WebContents that contains this tab id. 509 // Find the WebContents that contains this tab id.
442 contents_ = NULL; 510 contents_ = NULL;
443 if (debuggee_.tab_id) { 511 if (debuggee_.tab_id) {
444 WebContents* web_contents = NULL; 512 WebContents* web_contents = NULL;
445 bool result = ExtensionTabUtil::GetTabById( 513 bool result = ExtensionTabUtil::GetTabById(
446 *debuggee_.tab_id, profile(), include_incognito(), NULL, NULL, 514 *debuggee_.tab_id, profile(), include_incognito(), NULL, NULL,
447 &web_contents, NULL); 515 &web_contents, NULL);
(...skipping 28 matching lines...) Expand all
476 if (host) { 544 if (host) {
477 contents_ = WebContents::FromRenderViewHost(host->render_view_host()); 545 contents_ = WebContents::FromRenderViewHost(host->render_view_host());
478 if (contents_) 546 if (contents_)
479 return true; 547 return true;
480 } 548 }
481 549
482 FormatErrorMessage(keys::kNoTargetError); 550 FormatErrorMessage(keys::kNoTargetError);
483 return false; 551 return false;
484 } 552 }
485 553
554 if (debuggee_.target_id) {
555 DevToolsAgentHost* agent_host =
556 DevToolsAgentHost::GetForId(*debuggee_.target_id);
557 if (agent_host) {
558 contents_ = WebContents::FromRenderViewHost(
559 agent_host->GetRenderViewHost());
560
561 if (!CommandLine::ForCurrentProcess()->
562 HasSwitch(switches::kSilentDebuggerExtensionAPI)) {
563 // Allow only tabs, reject background pages.
Matt Perry 2013/03/18 19:53:44 Why? And what about popups or extension pages with
Vladislav Kaznacheev 2013/03/19 05:56:23 When an extension attaches to the page using chrom
Matt Perry 2013/03/19 18:02:01 Might this apply to extension popups as well? If n
Vladislav Kaznacheev 2013/03/20 07:20:49 Yes, I missed that. I have refactored the infobar
564 if (GetExtensionBackgroundHost(contents_)) {
565 error_ = ErrorUtils::FormatErrorMessage(
566 keys::kSilentDebuggingRequired,
567 switches::kSilentDebuggerExtensionAPI);
568 return false;
569 }
570 }
571 return true;
572 }
573 FormatErrorMessage(keys::kNoTargetError);
574 return false;
575 }
576
486 error_ = keys::kInvalidTargetError; 577 error_ = keys::kInvalidTargetError;
487 return false; 578 return false;
488 } 579 }
489 580
490 bool DebuggerFunction::InitClientHost() { 581 bool DebuggerFunction::InitClientHost() {
491 if (!InitWebContents()) 582 if (!InitWebContents())
492 return false; 583 return false;
493 584
494 // Don't fetch rvh from the contents since it'll be wrong upon navigation. 585 // Don't fetch rvh from the contents since it'll be wrong upon navigation.
495 client_host_ = AttachedClientHosts::GetInstance()->Lookup(contents_); 586 client_host_ = AttachedClientHosts::GetInstance()->Lookup(contents_);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 } 676 }
586 677
587 DictionaryValue* result_body; 678 DictionaryValue* result_body;
588 SendCommand::Results::Result result; 679 SendCommand::Results::Result result;
589 if (response->GetDictionary("result", &result_body)) 680 if (response->GetDictionary("result", &result_body))
590 result.additional_properties.Swap(result_body); 681 result.additional_properties.Swap(result_body);
591 682
592 results_ = SendCommand::Results::Create(result); 683 results_ = SendCommand::Results::Create(result);
593 SendResponse(true); 684 SendResponse(true);
594 } 685 }
686
687 DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() {}
688
689 DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() {}
690
691 bool DebuggerGetTargetsFunction::RunImpl() {
692 base::ListValue* results_list = new ListValue();
693
694 std::vector<RenderViewHost*> rvh_list =
695 DevToolsAgentHost::GetValidRenderViewHosts();
696 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
697 it != rvh_list.end(); ++it)
698 results_list->Append(SerializePageInfo(*it));
699
700 SetResult(results_list);
701 SendResponse(true);
702 return true;
703 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/debugger/debugger_api.h ('k') | chrome/browser/extensions/api/debugger/debugger_api_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698