1 // Copyright 2013 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/devtools/devtools_target_impl.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/devtools/devtools_window.h"
10 #include "chrome/browser/extensions/extension_tab_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
13 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
14 #include "chrome/common/extensions/extension_constants.h"
15 #include "components/guest_view/browser/guest_view_base.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/devtools_agent_host.h"
18 #include "content/public/browser/favicon_status.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "extensions/browser/extension_host.h"
23 #include "extensions/browser/extension_registry.h"
24 #include "extensions/browser/process_manager.h"
25 #include "extensions/common/constants.h"
27 using content::BrowserThread
;
28 using content::DevToolsAgentHost
;
29 using content::WebContents
;
31 const char DevToolsTargetImpl::kTargetTypeApp
[] = "app";
32 const char DevToolsTargetImpl::kTargetTypeBackgroundPage
[] = "background_page";
33 const char DevToolsTargetImpl::kTargetTypePage
[] = "page";
34 const char DevToolsTargetImpl::kTargetTypeWorker
[] = "worker";
35 const char DevToolsTargetImpl::kTargetTypeWebView
[] = "webview";
36 const char DevToolsTargetImpl::kTargetTypeIFrame
[] = "iframe";
37 const char DevToolsTargetImpl::kTargetTypeOther
[] = "other";
38 const char DevToolsTargetImpl::kTargetTypeServiceWorker
[] = "service_worker";
42 // WebContentsTarget --------------------------------------------------------
44 class WebContentsTarget
: public DevToolsTargetImpl
{
46 WebContentsTarget(WebContents
* web_contents
, bool is_tab
);
48 // DevToolsTargetImpl overrides.
49 int GetTabId() const override
;
50 std::string
GetExtensionId() const override
;
51 void Inspect(Profile
* profile
) const override
;
55 std::string extension_id_
;
58 WebContentsTarget::WebContentsTarget(WebContents
* web_contents
, bool is_tab
)
59 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents
)),
61 set_type(kTargetTypeOther
);
63 guest_view::GuestViewBase
* guest
=
64 guest_view::GuestViewBase::FromWebContents(web_contents
);
65 WebContents
* guest_contents
= guest
? guest
->embedder_web_contents() : NULL
;
67 set_type(kTargetTypeWebView
);
68 set_parent_id(DevToolsAgentHost::GetOrCreateFor(guest_contents
)->GetId());
73 set_type(kTargetTypePage
);
74 tab_id_
= extensions::ExtensionTabUtil::GetTabId(web_contents
);
78 const extensions::Extension
* extension
= extensions::ExtensionRegistry::Get(
79 web_contents
->GetBrowserContext())->enabled_extensions().GetByID(
85 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
88 set_title(extension
->name());
89 extensions::ExtensionHost
* extension_host
=
90 extensions::ProcessManager::Get(profile
)
91 ->GetBackgroundHostForExtension(extension
->id());
93 extension_host
->host_contents() == web_contents
) {
94 set_type(kTargetTypeBackgroundPage
);
95 extension_id_
= extension
->id();
96 } else if (extension
->is_hosted_app()
97 || extension
->is_legacy_packaged_app()
98 || extension
->is_platform_app()) {
99 set_type(kTargetTypeApp
);
101 set_favicon_url(extensions::ExtensionIconSource::GetIconURL(
102 extension
, extension_misc::EXTENSION_ICON_SMALLISH
,
103 ExtensionIconSet::MATCH_BIGGER
, false, NULL
));
106 int WebContentsTarget::GetTabId() const {
110 std::string
WebContentsTarget::GetExtensionId() const {
111 return extension_id_
;
114 void WebContentsTarget::Inspect(Profile
* profile
) const {
115 WebContents
* web_contents
= GetWebContents();
118 DevToolsWindow::OpenDevToolsWindow(web_contents
);
121 // FrameTarget ----------------------------------------------------------------
123 class FrameTarget
: public DevToolsTargetImpl
{
125 explicit FrameTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
);
127 // DevToolsTargetImpl overrides:
128 void Inspect(Profile
* profile
) const override
;
131 FrameTarget::FrameTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
)
132 : DevToolsTargetImpl(agent_host
) {
133 set_type(kTargetTypeIFrame
);
134 WebContents
* wc
= agent_host
->GetWebContents();
135 DCHECK(DevToolsAgentHost::GetOrCreateFor(wc
).get() != agent_host
.get());
136 set_parent_id(DevToolsAgentHost::GetOrCreateFor(wc
)->GetId());
139 void FrameTarget::Inspect(Profile
* profile
) const {
140 DevToolsWindow::OpenDevToolsWindow(profile
, GetAgentHost());
143 // WorkerTarget ----------------------------------------------------------------
145 class WorkerTarget
: public DevToolsTargetImpl
{
147 explicit WorkerTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
);
149 // DevToolsTargetImpl overrides:
150 void Inspect(Profile
* profile
) const override
;
153 WorkerTarget::WorkerTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
)
154 : DevToolsTargetImpl(agent_host
) {
155 switch (agent_host
->GetType()) {
156 case DevToolsAgentHost::TYPE_SHARED_WORKER
:
157 set_type(kTargetTypeWorker
);
159 case DevToolsAgentHost::TYPE_SERVICE_WORKER
:
160 set_type(kTargetTypeServiceWorker
);
167 void WorkerTarget::Inspect(Profile
* profile
) const {
168 DevToolsWindow::OpenDevToolsWindowForWorker(profile
, GetAgentHost());
173 // DevToolsTargetImpl ----------------------------------------------------------
175 DevToolsTargetImpl::~DevToolsTargetImpl() {
178 DevToolsTargetImpl::DevToolsTargetImpl(
179 scoped_refptr
<DevToolsAgentHost
> agent_host
)
180 : devtools_discovery::BasicTargetDescriptor(agent_host
) {
183 int DevToolsTargetImpl::GetTabId() const {
187 WebContents
* DevToolsTargetImpl::GetWebContents() const {
188 return GetAgentHost()->GetWebContents();
191 std::string
DevToolsTargetImpl::GetExtensionId() const {
192 return std::string();
195 void DevToolsTargetImpl::Inspect(Profile
* /*profile*/) const {
198 void DevToolsTargetImpl::Reload() const {
202 scoped_ptr
<DevToolsTargetImpl
> DevToolsTargetImpl::CreateForTab(
203 content::WebContents
* web_contents
) {
204 // TODO(dgozman): these checks should not be necessary. See
205 // http://crbug.com/489664.
208 if (!DevToolsAgentHost::GetOrCreateFor(web_contents
))
210 return scoped_ptr
<DevToolsTargetImpl
>(
211 new WebContentsTarget(web_contents
, true));
215 std::vector
<DevToolsTargetImpl
*> DevToolsTargetImpl::EnumerateAll() {
216 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
218 std::set
<WebContents
*> tab_web_contents
;
219 for (TabContentsIterator it
; !it
.done(); it
.Next())
220 tab_web_contents
.insert(*it
);
222 std::vector
<DevToolsTargetImpl
*> result
;
223 DevToolsAgentHost::List agents
= DevToolsAgentHost::GetOrCreateAll();
224 for (DevToolsAgentHost::List::iterator it
= agents
.begin();
225 it
!= agents
.end(); ++it
) {
226 DevToolsAgentHost
* agent_host
= (*it
).get();
227 switch (agent_host
->GetType()) {
228 case DevToolsAgentHost::TYPE_WEB_CONTENTS
:
229 if (WebContents
* web_contents
= agent_host
->GetWebContents()) {
231 tab_web_contents
.find(web_contents
) != tab_web_contents
.end();
232 result
.push_back(new WebContentsTarget(web_contents
, is_tab
));
235 case DevToolsAgentHost::TYPE_FRAME
:
236 result
.push_back(new FrameTarget(agent_host
));
238 case DevToolsAgentHost::TYPE_SHARED_WORKER
:
239 result
.push_back(new WorkerTarget(agent_host
));
241 case DevToolsAgentHost::TYPE_SERVICE_WORKER
:
242 result
.push_back(new WorkerTarget(agent_host
));