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

Side by Side Diff: chrome/browser/android/dev_tools_server.cc

Issue 24995003: DevTools: Extract target discovery and manipulation from DevToolsHttpHandlerImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added Android implementations Created 7 years, 2 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 | Annotate | Revision Log
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 #include "chrome/browser/android/dev_tools_server.h" 5 #include "chrome/browser/android/dev_tools_server.h"
6 6
7 #include <pwd.h> 7 #include <pwd.h>
8 #include <cstring> 8 #include <cstring>
9 9
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "chrome/browser/browser_process.h" 18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/devtools/devtools_adb_bridge.h" 19 #include "chrome/browser/devtools/devtools_adb_bridge.h"
20 #include "chrome/browser/history/top_sites.h" 20 #include "chrome/browser/history/top_sites.h"
21 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/ui/android/tab_model/tab_model.h" 22 #include "chrome/browser/ui/android/tab_model/tab_model.h"
23 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" 23 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
24 #include "content/public/browser/android/devtools_auth.h" 24 #include "content/public/browser/android/devtools_auth.h"
25 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/devtools_agent_host.h"
26 #include "content/public/browser/devtools_http_handler.h" 27 #include "content/public/browser/devtools_http_handler.h"
27 #include "content/public/browser/devtools_http_handler_delegate.h" 28 #include "content/public/browser/devtools_http_handler_delegate.h"
29 #include "content/public/browser/devtools_target_descriptor.h"
30 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/web_contents.h" 31 #include "content/public/browser/web_contents.h"
32 #include "content/public/browser/web_contents_delegate.h"
29 #include "content/public/common/content_switches.h" 33 #include "content/public/common/content_switches.h"
30 #include "content/public/common/url_constants.h" 34 #include "content/public/common/url_constants.h"
31 #include "grit/devtools_discovery_page_resources.h" 35 #include "grit/devtools_discovery_page_resources.h"
32 #include "jni/DevToolsServer_jni.h" 36 #include "jni/DevToolsServer_jni.h"
33 #include "net/socket/unix_domain_socket_posix.h" 37 #include "net/socket/unix_domain_socket_posix.h"
34 #include "net/url_request/url_request_context_getter.h" 38 #include "net/url_request/url_request_context_getter.h"
35 #include "ui/base/resource/resource_bundle.h" 39 #include "ui/base/resource/resource_bundle.h"
36 #include "webkit/common/user_agent/user_agent_util.h" 40 #include "webkit/common/user_agent/user_agent_util.h"
37 41
42 using content::DevToolsAgentHost;
43 using content::RenderViewHost;
44 using content::WebContents;
45
38 namespace { 46 namespace {
39 47
40 const char kFrontEndURL[] = 48 const char kFrontEndURL[] =
41 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html"; 49 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
42 const char kDefaultSocketNamePrefix[] = "chrome"; 50 const char kDefaultSocketNamePrefix[] = "chrome";
43 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d"; 51 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d";
44 52
53 RenderViewHost* GetRenderViewHost(const std::string& id) {
54 DevToolsAgentHost* agent_host = DevToolsAgentHost::GetForId(id);
55 return agent_host ? agent_host->GetRenderViewHost() : NULL;
56 }
57
58 WebContents* GetWebContents(const std::string& id) {
59 RenderViewHost* rvh = GetRenderViewHost(id);
60 return rvh ? WebContents::FromRenderViewHost(rvh) : NULL;
61 }
62
45 // Delegate implementation for the devtools http handler on android. A new 63 // Delegate implementation for the devtools http handler on android. A new
46 // instance of this gets created each time devtools is enabled. 64 // instance of this gets created each time devtools is enabled.
47 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { 65 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate {
48 public: 66 public:
49 DevToolsServerDelegate() 67 DevToolsServerDelegate()
50 : last_tethering_socket_(0) { 68 : last_tethering_socket_(0) {
51 } 69 }
52 70
53 virtual std::string GetDiscoveryPageHTML() OVERRIDE { 71 virtual std::string GetDiscoveryPageHTML() OVERRIDE {
54 // TopSites updates itself after a delay. Ask TopSites to update itself 72 // TopSites updates itself after a delay. Ask TopSites to update itself
55 // when we're about to show the remote debugging landing page. 73 // when we're about to show the remote debugging landing page.
56 content::BrowserThread::PostTask( 74 content::BrowserThread::PostTask(
57 content::BrowserThread::UI, 75 content::BrowserThread::UI,
58 FROM_HERE, 76 FROM_HERE,
59 base::Bind(&DevToolsServerDelegate::PopulatePageThumbnails)); 77 base::Bind(&DevToolsServerDelegate::PopulatePageThumbnails));
60 return ResourceBundle::GetSharedInstance().GetRawDataResource( 78 return ResourceBundle::GetSharedInstance().GetRawDataResource(
61 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string(); 79 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string();
62 } 80 }
63 81
64 virtual bool BundlesFrontendResources() OVERRIDE { 82 virtual bool BundlesFrontendResources() OVERRIDE {
65 return false; 83 return false;
66 } 84 }
67 85
68 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { 86 virtual base::FilePath GetDebugFrontendDir() OVERRIDE {
69 return base::FilePath(); 87 return base::FilePath();
70 } 88 }
71 89
72 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { 90 virtual bool SupportsPageThumbnails() OVERRIDE {
91 return true;
92 }
93
94 virtual std::string GetPageThumbnailData(const std::string& id) OVERRIDE {
95 WebContents* web_contents = GetWebContents(id);
96 if (!web_contents)
97 return "";
98
99 GURL url = web_contents->GetURL();
73 Profile* profile = 100 Profile* profile =
74 ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); 101 ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
75 history::TopSites* top_sites = profile->GetTopSites(); 102 history::TopSites* top_sites = profile->GetTopSites();
76 if (top_sites) { 103 if (top_sites) {
77 scoped_refptr<base::RefCountedMemory> data; 104 scoped_refptr<base::RefCountedMemory> data;
78 if (top_sites->GetPageThumbnail(url, false, &data)) 105 if (top_sites->GetPageThumbnail(url, false, &data))
79 return std::string(reinterpret_cast<const char*>(data->front()), 106 return std::string(reinterpret_cast<const char*>(data->front()),
80 data->size()); 107 data->size());
81 } 108 }
82 return ""; 109 return "";
83 } 110 }
84 111
85 virtual content::RenderViewHost* CreateNewTarget() OVERRIDE { 112 virtual Target* CreateNewTarget() OVERRIDE {
86 Profile* profile = 113 Profile* profile =
87 g_browser_process->profile_manager()->GetDefaultProfile(); 114 g_browser_process->profile_manager()->GetDefaultProfile();
88 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile); 115 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile);
89 if (!tab_model) 116 if (!tab_model)
90 return NULL; 117 return NULL;
91 content::WebContents* web_contents = 118 WebContents* web_contents =
92 tab_model->CreateTabForTesting(GURL(content::kAboutBlankURL)); 119 tab_model->CreateTabForTesting(GURL(content::kAboutBlankURL));
93 if (!web_contents) 120 if (!web_contents)
94 return NULL; 121 return NULL;
95 return web_contents->GetRenderViewHost(); 122 return Target::FromWebContents(web_contents);
96 } 123 }
97 124
98 virtual TargetType GetTargetType(content::RenderViewHost*) OVERRIDE { 125 virtual bool ActivateTarget(const std::string& id) OVERRIDE {
99 return kTargetTypeTab; 126 WebContents* web_contents = GetWebContents(id);
127 if (!web_contents)
128 return false;
129 web_contents->GetDelegate()->ActivateContents(web_contents);
130 return true;
100 } 131 }
101 132
102 virtual std::string GetViewDescription(content::RenderViewHost*) OVERRIDE { 133 virtual bool CloseTarget(const std::string& id) OVERRIDE {
103 return ""; 134 RenderViewHost* rvh = GetRenderViewHost(id);
135 if (!rvh)
136 return false;
137 rvh->ClosePage();
138 return true;
139 }
140
141 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost(
142 const std::string& id) OVERRIDE {
143 return DevToolsAgentHost::GetForId(id);
144 }
145
146 virtual void RequestTargets(TargetCallback callback) OVERRIDE {
147 TargetList targets;
148 std::vector<RenderViewHost*> rvh_list =
149 DevToolsAgentHost::GetValidRenderViewHosts();
150 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
151 it != rvh_list.end(); ++it) {
152 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
153 if (web_contents)
154 targets.push_back(Target::FromWebContents(web_contents));
155 }
156 callback.Run(targets);
104 } 157 }
105 158
106 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( 159 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering(
107 net::StreamListenSocket::Delegate* delegate, 160 net::StreamListenSocket::Delegate* delegate,
108 std::string* name) OVERRIDE { 161 std::string* name) OVERRIDE {
109 *name = base::StringPrintf( 162 *name = base::StringPrintf(
110 kTetheringSocketName, getpid(), ++last_tethering_socket_); 163 kTetheringSocketName, getpid(), ++last_tethering_socket_);
111 return net::UnixDomainSocket::CreateAndListenWithAbstractNamespace( 164 return net::UnixDomainSocket::CreateAndListenWithAbstractNamespace(
112 *name, 165 *name,
113 "", 166 "",
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 jobject obj, 266 jobject obj,
214 jint server, 267 jint server,
215 jboolean enabled) { 268 jboolean enabled) {
216 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); 269 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server);
217 if (enabled) { 270 if (enabled) {
218 devtools_server->Start(); 271 devtools_server->Start();
219 } else { 272 } else {
220 devtools_server->Stop(); 273 devtools_server->Stop();
221 } 274 }
222 } 275 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698