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::NavigationController
& controller
= web_contents
->GetController();
67 content::NavigationEntry
* entry
= controller
.GetActiveEntry();
68 if (entry
!= NULL
&& entry
->GetURL().is_valid())
69 set_favicon_url(entry
->GetFavicon().url
);
70 set_last_activity_time(web_contents
->GetLastActiveTime());
72 extensions::GuestViewBase
* guest
=
73 extensions::GuestViewBase::FromWebContents(web_contents
);
74 WebContents
* guest_contents
= guest
? guest
->embedder_web_contents() : NULL
;
76 set_type(kTargetTypeWebView
);
77 set_parent_id(DevToolsAgentHost::GetOrCreateFor(guest_contents
)->GetId());
82 set_type(kTargetTypePage
);
83 tab_id_
= extensions::ExtensionTabUtil::GetTabId(web_contents
);
87 const extensions::Extension
* extension
= extensions::ExtensionRegistry::Get(
88 web_contents
->GetBrowserContext())->enabled_extensions().GetByID(
94 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
97 set_title(extension
->name());
98 extensions::ExtensionHost
* extension_host
=
99 extensions::ProcessManager::Get(profile
)
100 ->GetBackgroundHostForExtension(extension
->id());
101 if (extension_host
&&
102 extension_host
->host_contents() == web_contents
) {
103 set_type(kTargetTypeBackgroundPage
);
104 extension_id_
= extension
->id();
105 } else if (extension
->is_hosted_app()
106 || extension
->is_legacy_packaged_app()
107 || extension
->is_platform_app()) {
108 set_type(kTargetTypeApp
);
110 set_favicon_url(extensions::ExtensionIconSource::GetIconURL(
111 extension
, extension_misc::EXTENSION_ICON_SMALLISH
,
112 ExtensionIconSet::MATCH_BIGGER
, false, NULL
));
115 WebContents
* WebContentsTarget::GetWebContents() const {
116 return GetAgentHost()->GetWebContents();
119 int WebContentsTarget::GetTabId() const {
123 std::string
WebContentsTarget::GetExtensionId() const {
124 return extension_id_
;
127 void WebContentsTarget::Inspect(Profile
* profile
) const {
128 WebContents
* web_contents
= GetWebContents();
131 DevToolsWindow::OpenDevToolsWindow(web_contents
);
134 // FrameTarget ----------------------------------------------------------------
136 class FrameTarget
: public DevToolsTargetImpl
{
138 explicit FrameTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
);
140 // DevToolsTargetImpl overrides:
141 void Inspect(Profile
* profile
) const override
;
144 FrameTarget::FrameTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
)
145 : DevToolsTargetImpl(agent_host
) {
146 set_type(kTargetTypePage
);
147 WebContents
* wc
= agent_host
->GetWebContents();
148 DCHECK(DevToolsAgentHost::GetOrCreateFor(wc
).get() != agent_host
.get());
149 set_parent_id(DevToolsAgentHost::GetOrCreateFor(wc
)->GetId());
152 void FrameTarget::Inspect(Profile
* profile
) const {
153 DevToolsWindow::OpenDevToolsWindow(profile
, GetAgentHost());
156 // WorkerTarget ----------------------------------------------------------------
158 class WorkerTarget
: public DevToolsTargetImpl
{
160 explicit WorkerTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
);
162 // DevToolsTargetImpl overrides:
163 void Inspect(Profile
* profile
) const override
;
166 WorkerTarget::WorkerTarget(scoped_refptr
<DevToolsAgentHost
> agent_host
)
167 : DevToolsTargetImpl(agent_host
) {
168 switch (agent_host
->GetType()) {
169 case DevToolsAgentHost::TYPE_SHARED_WORKER
:
170 set_type(kTargetTypeWorker
);
172 case DevToolsAgentHost::TYPE_SERVICE_WORKER
:
173 set_type(kTargetTypeServiceWorker
);
180 void WorkerTarget::Inspect(Profile
* profile
) const {
181 DevToolsWindow::OpenDevToolsWindowForWorker(profile
, GetAgentHost());
186 // DevToolsTargetImpl ----------------------------------------------------------
188 DevToolsTargetImpl::~DevToolsTargetImpl() {
191 DevToolsTargetImpl::DevToolsTargetImpl(
192 scoped_refptr
<DevToolsAgentHost
> agent_host
)
193 : agent_host_(agent_host
),
194 title_(agent_host
->GetTitle()),
195 url_(agent_host
->GetURL()) {
198 std::string
DevToolsTargetImpl::GetParentId() const {
202 std::string
DevToolsTargetImpl::GetId() const {
203 return agent_host_
->GetId();
206 std::string
DevToolsTargetImpl::GetType() const {
210 std::string
DevToolsTargetImpl::GetTitle() const {
214 std::string
DevToolsTargetImpl::GetDescription() const {
218 GURL
DevToolsTargetImpl::GetURL() const {
222 GURL
DevToolsTargetImpl::GetFaviconURL() const {
226 base::TimeTicks
DevToolsTargetImpl::GetLastActivityTime() const {
227 return last_activity_time_
;
230 scoped_refptr
<content::DevToolsAgentHost
>
231 DevToolsTargetImpl::GetAgentHost() const {
235 bool DevToolsTargetImpl::IsAttached() const {
236 return agent_host_
->IsAttached();
239 bool DevToolsTargetImpl::Activate() const {
240 return agent_host_
->Activate();
243 bool DevToolsTargetImpl::Close() const {
244 return agent_host_
->Close();
247 int DevToolsTargetImpl::GetTabId() const {
251 WebContents
* DevToolsTargetImpl::GetWebContents() const {
255 std::string
DevToolsTargetImpl::GetExtensionId() const {
256 return std::string();
259 void DevToolsTargetImpl::Inspect(Profile
* /*profile*/) const {
262 void DevToolsTargetImpl::Reload() const {
266 scoped_ptr
<DevToolsTargetImpl
> DevToolsTargetImpl::CreateForWebContents(
267 content::WebContents
* web_contents
,
269 return scoped_ptr
<DevToolsTargetImpl
>(
270 new WebContentsTarget(web_contents
, is_tab
));
274 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback
) {
275 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
277 std::set
<WebContents
*> tab_web_contents
;
278 for (TabContentsIterator it
; !it
.done(); it
.Next())
279 tab_web_contents
.insert(*it
);
281 DevToolsTargetImpl::List result
;
282 DevToolsAgentHost::List agents
= DevToolsAgentHost::GetOrCreateAll();
283 for (DevToolsAgentHost::List::iterator it
= agents
.begin();
284 it
!= agents
.end(); ++it
) {
285 DevToolsAgentHost
* agent_host
= (*it
).get();
286 switch (agent_host
->GetType()) {
287 case DevToolsAgentHost::TYPE_WEB_CONTENTS
:
288 if (WebContents
* web_contents
= agent_host
->GetWebContents()) {
290 tab_web_contents
.find(web_contents
) != tab_web_contents
.end();
291 result
.push_back(new WebContentsTarget(web_contents
, is_tab
));
294 case DevToolsAgentHost::TYPE_FRAME
:
295 result
.push_back(new FrameTarget(agent_host
));
297 case DevToolsAgentHost::TYPE_SHARED_WORKER
:
298 result
.push_back(new WorkerTarget(agent_host
));
300 case DevToolsAgentHost::TYPE_SERVICE_WORKER
:
301 result
.push_back(new WorkerTarget(agent_host
));
308 callback
.Run(result
);