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

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: Extracted DevToolsTarget class 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 "base/strings/utf_string_conversions.h"
18 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/devtools/devtools_adb_bridge.h" 20 #include "chrome/browser/devtools/devtools_adb_bridge.h"
20 #include "chrome/browser/history/top_sites.h" 21 #include "chrome/browser/history/top_sites.h"
21 #include "chrome/browser/profiles/profile_manager.h" 22 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/ui/android/tab_model/tab_model.h" 23 #include "chrome/browser/ui/android/tab_model/tab_model.h"
23 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" 24 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
24 #include "content/public/browser/android/devtools_auth.h" 25 #include "content/public/browser/android/devtools_auth.h"
25 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/devtools_agent_host.h"
26 #include "content/public/browser/devtools_http_handler.h" 28 #include "content/public/browser/devtools_http_handler.h"
27 #include "content/public/browser/devtools_http_handler_delegate.h" 29 #include "content/public/browser/devtools_http_handler_delegate.h"
30 #include "content/public/browser/favicon_status.h"
31 #include "content/public/browser/navigation_entry.h"
32 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
34 #include "content/public/browser/web_contents_delegate.h"
29 #include "content/public/common/content_switches.h" 35 #include "content/public/common/content_switches.h"
30 #include "content/public/common/url_constants.h" 36 #include "content/public/common/url_constants.h"
31 #include "grit/devtools_discovery_page_resources.h" 37 #include "grit/devtools_discovery_page_resources.h"
32 #include "jni/DevToolsServer_jni.h" 38 #include "jni/DevToolsServer_jni.h"
39 #include "net/base/escape.h"
33 #include "net/socket/unix_domain_socket_posix.h" 40 #include "net/socket/unix_domain_socket_posix.h"
34 #include "net/url_request/url_request_context_getter.h" 41 #include "net/url_request/url_request_context_getter.h"
35 #include "ui/base/resource/resource_bundle.h" 42 #include "ui/base/resource/resource_bundle.h"
36 #include "webkit/common/user_agent/user_agent_util.h" 43 #include "webkit/common/user_agent/user_agent_util.h"
37 44
45 using content::DevToolsAgentHost;
46 using content::RenderViewHost;
47 using content::WebContents;
48
38 namespace { 49 namespace {
39 50
40 const char kFrontEndURL[] = 51 const char kFrontEndURL[] =
41 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html"; 52 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
42 const char kDefaultSocketNamePrefix[] = "chrome"; 53 const char kDefaultSocketNamePrefix[] = "chrome";
43 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d"; 54 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d";
44 55
56 class Target : public content::DevToolsTarget {
57 public:
58 explicit Target(WebContents* web_contents);
59
60 virtual std::string GetId() const OVERRIDE { return id_; }
61 virtual std::string GetType() const OVERRIDE { return type_; }
62 virtual std::string GetTitle() const OVERRIDE { return title_; }
63 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
64 virtual GURL GetUrl() const OVERRIDE { return url_; }
65 virtual GURL GetFaviconUrl() const OVERRIDE { return favicon_url_; }
66 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
67 return last_activity_time_;
68 }
69 virtual bool IsAttached() const OVERRIDE {
70 return agent_host_->IsAttached();
71 }
72 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
73 return agent_host_;
74 }
75 virtual bool Activate() const OVERRIDE;
76 virtual bool Close() const OVERRIDE;
77
78 private:
79 scoped_refptr<DevToolsAgentHost> agent_host_;
80 std::string id_;
81 std::string type_;
82 std::string title_;
83 GURL url_;
84 GURL favicon_url_;
85 base::TimeTicks last_activity_time_;
86 };
87
88 Target::Target(WebContents* web_contents) {
89 agent_host_ =
90 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
91 id_ = agent_host_->GetId();
92 type_ = "page";
93 title_ = UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle()));
94 url_ = web_contents->GetURL();
95 content::NavigationController& controller = web_contents->GetController();
96 content::NavigationEntry* entry = controller.GetActiveEntry();
97 if (entry != NULL && entry->GetURL().is_valid())
98 favicon_url_ = entry->GetFavicon().url;
99 last_activity_time_ = web_contents->GetLastSelectedTime();
100 }
101
102 bool Target::Activate() const {
103 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
104 if (!rvh)
105 return false;
106 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
107 if (!web_contents)
108 return false;
109 web_contents->GetDelegate()->ActivateContents(web_contents);
110 return true;
111 }
112
113 bool Target::Close() const {
114 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
115 if (!rvh)
116 return false;
117 rvh->ClosePage();
118 return true;
119 }
120
45 // Delegate implementation for the devtools http handler on android. A new 121 // Delegate implementation for the devtools http handler on android. A new
46 // instance of this gets created each time devtools is enabled. 122 // instance of this gets created each time devtools is enabled.
47 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { 123 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate {
48 public: 124 public:
49 DevToolsServerDelegate() 125 DevToolsServerDelegate()
50 : last_tethering_socket_(0) { 126 : last_tethering_socket_(0) {
51 } 127 }
52 128
53 virtual std::string GetDiscoveryPageHTML() OVERRIDE { 129 virtual std::string GetDiscoveryPageHTML() OVERRIDE {
54 // TopSites updates itself after a delay. Ask TopSites to update itself 130 // TopSites updates itself after a delay. Ask TopSites to update itself
(...skipping 20 matching lines...) Expand all
75 history::TopSites* top_sites = profile->GetTopSites(); 151 history::TopSites* top_sites = profile->GetTopSites();
76 if (top_sites) { 152 if (top_sites) {
77 scoped_refptr<base::RefCountedMemory> data; 153 scoped_refptr<base::RefCountedMemory> data;
78 if (top_sites->GetPageThumbnail(url, false, &data)) 154 if (top_sites->GetPageThumbnail(url, false, &data))
79 return std::string(reinterpret_cast<const char*>(data->front()), 155 return std::string(reinterpret_cast<const char*>(data->front()),
80 data->size()); 156 data->size());
81 } 157 }
82 return ""; 158 return "";
83 } 159 }
84 160
85 virtual content::RenderViewHost* CreateNewTarget() OVERRIDE { 161 virtual scoped_refptr<content::DevToolsTarget> CreateNewTarget() OVERRIDE {
86 Profile* profile = 162 Profile* profile =
87 g_browser_process->profile_manager()->GetDefaultProfile(); 163 g_browser_process->profile_manager()->GetDefaultProfile();
88 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile); 164 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile);
89 if (!tab_model) 165 if (!tab_model)
90 return NULL; 166 return NULL;
91 content::WebContents* web_contents = 167 WebContents* web_contents =
92 tab_model->CreateTabForTesting(GURL(content::kAboutBlankURL)); 168 tab_model->CreateTabForTesting(GURL(content::kAboutBlankURL));
93 if (!web_contents) 169 if (!web_contents)
94 return NULL; 170 return NULL;
95 return web_contents->GetRenderViewHost(); 171 return new Target(web_contents);
96 } 172 }
97 173
98 virtual TargetType GetTargetType(content::RenderViewHost*) OVERRIDE { 174 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE {
99 return kTargetTypeTab; 175 TargetList targets;
100 } 176 std::vector<RenderViewHost*> rvh_list =
101 177 DevToolsAgentHost::GetValidRenderViewHosts();
102 virtual std::string GetViewDescription(content::RenderViewHost*) OVERRIDE { 178 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
103 return ""; 179 it != rvh_list.end(); ++it) {
180 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
181 if (web_contents)
182 targets.push_back(new Target(web_contents));
183 }
184 callback.Run(targets);
104 } 185 }
105 186
106 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( 187 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering(
107 net::StreamListenSocket::Delegate* delegate, 188 net::StreamListenSocket::Delegate* delegate,
108 std::string* name) OVERRIDE { 189 std::string* name) OVERRIDE {
109 *name = base::StringPrintf( 190 *name = base::StringPrintf(
110 kTetheringSocketName, getpid(), ++last_tethering_socket_); 191 kTetheringSocketName, getpid(), ++last_tethering_socket_);
111 return net::UnixDomainSocket::CreateAndListenWithAbstractNamespace( 192 return net::UnixDomainSocket::CreateAndListenWithAbstractNamespace(
112 *name, 193 *name,
113 "", 194 "",
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 jobject obj, 294 jobject obj,
214 jint server, 295 jint server,
215 jboolean enabled) { 296 jboolean enabled) {
216 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); 297 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server);
217 if (enabled) { 298 if (enabled) {
218 devtools_server->Start(); 299 devtools_server->Start();
219 } else { 300 } else {
220 devtools_server->Stop(); 301 devtools_server->Stop();
221 } 302 }
222 } 303 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698