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 "content/public/browser/browser_thread.h"
16 #include "content/public/browser/favicon_status.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/render_frame_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "extensions/browser/extension_host.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/guest_view/guest_view_base.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::RenderViewHost
;
30 using content::WebContents
;
31 using content::WorkerService
;
33 const char DevToolsTargetImpl::kTargetTypeApp
[] = "app";
34 const char DevToolsTargetImpl::kTargetTypeBackgroundPage
[] = "background_page";
35 const char DevToolsTargetImpl::kTargetTypePage
[] = "page";
36 const char DevToolsTargetImpl::kTargetTypeWorker
[] = "worker";
37 const char DevToolsTargetImpl::kTargetTypeWebView
[] = "webview";
38 const char DevToolsTargetImpl::kTargetTypeIFrame
[] = "iframe";
39 const char DevToolsTargetImpl::kTargetTypeOther
[] = "other";
40 const char DevToolsTargetImpl::kTargetTypeServiceWorker
[] = "service_worker";
44 // WebContentsTarget --------------------------------------------------------
46 class WebContentsTarget
: public DevToolsTargetImpl
{
48 WebContentsTarget(WebContents
* web_contents
, bool is_tab
);
50 // DevToolsTargetImpl overrides:
51 WebContents
* GetWebContents() const override
;
52 int GetTabId() const override
;
53 std::string
GetExtensionId() const override
;
54 void Inspect(Profile
* profile
) const override
;
58 std::string extension_id_
;
61 WebContentsTarget::WebContentsTarget(WebContents
* web_contents
, bool is_tab
)
62 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents
)),
64 set_type(kTargetTypeOther
);
66 content::RenderFrameHost
* rfh
=
67 web_contents
->GetRenderViewHost()->GetMainFrame();
68 if (rfh
->IsCrossProcessSubframe()) {
69 set_url(rfh
->GetLastCommittedURL());
70 set_type(kTargetTypeIFrame
);
71 // TODO(pfeldman) Update for out of process iframes.
72 RenderViewHost
* parent_rvh
= rfh
->GetParent()->GetRenderViewHost();
73 set_parent_id(DevToolsAgentHost::GetOrCreateFor(
74 WebContents::FromRenderViewHost(parent_rvh
))->GetId());
78 content::NavigationController
& controller
= web_contents
->GetController();
79 content::NavigationEntry
* entry
= controller
.GetActiveEntry();
80 if (entry
!= NULL
&& entry
->GetURL().is_valid())
81 set_favicon_url(entry
->GetFavicon().url
);
82 set_last_activity_time(web_contents
->GetLastActiveTime());
84 extensions::GuestViewBase
* guest
=
85 extensions::GuestViewBase::FromWebContents(web_contents
);
86 WebContents
* guest_contents
= guest
? guest
->embedder_web_contents() : NULL
;
88 set_type(kTargetTypeWebView
);
89 set_parent_id(DevToolsAgentHost::GetOrCreateFor(guest_contents
)->GetId());
94 set_type(kTargetTypePage
);
95 tab_id_
= extensions::ExtensionTabUtil::GetTabId(web_contents
);
99 const extensions::Extension
* extension
= extensions::ExtensionRegistry::Get(
100 web_contents
->GetBrowserContext())->enabled_extensions().GetByID(
106 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
109 set_title(extension
->name());
110 extensions::ExtensionHost
* extension_host
=
111 extensions::ProcessManager::Get(profile
)
112 ->GetBackgroundHostForExtension(extension
->id());
113 if (extension_host
&&
114 extension_host
->host_contents() == web_contents
) {
115 set_type(kTargetTypeBackgroundPage
);
116 extension_id_
= extension
->id();
117 } else if (extension
->is_hosted_app()
118 || extension
->is_legacy_packaged_app()
119 || extension
->is_platform_app()) {
120 set_type(kTargetTypeApp
);
122 set_favicon_url(extensions::ExtensionIconSource::GetIconURL(
123 extension
, extension_misc::EXTENSION_ICON_SMALLISH
,
124 ExtensionIconSet::MATCH_BIGGER
, false, NULL
));
127 WebContents
* WebContentsTarget::GetWebContents() const {
128 return GetAgentHost()->GetWebContents();
131 int WebContentsTarget::GetTabId() const {
135 std::string
WebContentsTarget::GetExtensionId() const {
136 return extension_id_
;
139 void WebContentsTarget::Inspect(Profile
* profile
) const {
140 WebContents
* web_contents
= GetWebContents();
143 DevToolsWindow::OpenDevToolsWindow(web_contents
);
146 // WorkerTarget ----------------------------------------------------------------
148 class WorkerTarget
: public DevToolsTargetImpl
{
150 explicit WorkerTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
);
152 // DevToolsTargetImpl overrides:
153 void Inspect(Profile
* profile
) const override
;
156 WorkerTarget::WorkerTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
)
157 : DevToolsTargetImpl(agent_host
) {
158 switch (agent_host
->GetType()) {
159 case DevToolsAgentHost::TYPE_SHARED_WORKER
:
160 set_type(kTargetTypeWorker
);
162 case DevToolsAgentHost::TYPE_SERVICE_WORKER
:
163 set_type(kTargetTypeServiceWorker
);
170 void WorkerTarget::Inspect(Profile
* profile
) const {
171 DevToolsWindow::OpenDevToolsWindowForWorker(profile
, GetAgentHost());
176 // DevToolsTargetImpl ----------------------------------------------------------
178 DevToolsTargetImpl::~DevToolsTargetImpl() {
181 DevToolsTargetImpl::DevToolsTargetImpl(
182 scoped_refptr
<DevToolsAgentHost
> agent_host
)
183 : agent_host_(agent_host
),
184 title_(agent_host
->GetTitle()),
185 url_(agent_host
->GetURL()) {
188 std::string
DevToolsTargetImpl::GetParentId() const {
192 std::string
DevToolsTargetImpl::GetId() const {
193 return agent_host_
->GetId();
196 std::string
DevToolsTargetImpl::GetType() const {
200 std::string
DevToolsTargetImpl::GetTitle() const {
204 std::string
DevToolsTargetImpl::GetDescription() const {
208 GURL
DevToolsTargetImpl::GetURL() const {
212 GURL
DevToolsTargetImpl::GetFaviconURL() const {
216 base::TimeTicks
DevToolsTargetImpl::GetLastActivityTime() const {
217 return last_activity_time_
;
220 scoped_refptr
<content::DevToolsAgentHost
>
221 DevToolsTargetImpl::GetAgentHost() const {
225 bool DevToolsTargetImpl::IsAttached() const {
226 return agent_host_
->IsAttached();
229 bool DevToolsTargetImpl::Activate() const {
230 return agent_host_
->Activate();
233 bool DevToolsTargetImpl::Close() const {
234 return agent_host_
->Close();
237 int DevToolsTargetImpl::GetTabId() const {
241 WebContents
* DevToolsTargetImpl::GetWebContents() const {
245 std::string
DevToolsTargetImpl::GetExtensionId() const {
246 return std::string();
249 void DevToolsTargetImpl::Inspect(Profile
* /*profile*/) const {
252 void DevToolsTargetImpl::Reload() const {
256 scoped_ptr
<DevToolsTargetImpl
> DevToolsTargetImpl::CreateForWebContents(
257 content::WebContents
* web_contents
,
259 return scoped_ptr
<DevToolsTargetImpl
>(
260 new WebContentsTarget(web_contents
, is_tab
));
264 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback
) {
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
267 std::set
<WebContents
*> tab_web_contents
;
268 for (TabContentsIterator it
; !it
.done(); it
.Next())
269 tab_web_contents
.insert(*it
);
271 DevToolsTargetImpl::List result
;
272 DevToolsAgentHost::List agents
= DevToolsAgentHost::GetOrCreateAll();
273 for (DevToolsAgentHost::List::iterator it
= agents
.begin();
274 it
!= agents
.end(); ++it
) {
275 DevToolsAgentHost
* agent_host
= (*it
).get();
276 switch (agent_host
->GetType()) {
277 case DevToolsAgentHost::TYPE_WEB_CONTENTS
:
278 if (WebContents
* web_contents
= agent_host
->GetWebContents()) {
280 tab_web_contents
.find(web_contents
) != tab_web_contents
.end();
281 result
.push_back(new WebContentsTarget(web_contents
, is_tab
));
284 case DevToolsAgentHost::TYPE_SHARED_WORKER
:
285 result
.push_back(new WorkerTarget(agent_host
));
287 case DevToolsAgentHost::TYPE_SERVICE_WORKER
:
288 result
.push_back(new WorkerTarget(agent_host
));
295 callback
.Run(result
);