OLD | NEW |
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 const char kTargetTypePage[] = "page"; |
| 57 |
| 58 class Target : public content::DevToolsTarget { |
| 59 public: |
| 60 explicit Target(WebContents* web_contents); |
| 61 |
| 62 virtual std::string GetId() const OVERRIDE { return id_; } |
| 63 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; } |
| 64 virtual std::string GetTitle() const OVERRIDE { return title_; } |
| 65 virtual std::string GetDescription() const OVERRIDE { return std::string(); } |
| 66 virtual GURL GetUrl() const OVERRIDE { return url_; } |
| 67 virtual GURL GetFaviconUrl() const OVERRIDE { return favicon_url_; } |
| 68 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE { |
| 69 return last_activity_time_; |
| 70 } |
| 71 virtual bool IsAttached() const OVERRIDE { |
| 72 return agent_host_->IsAttached(); |
| 73 } |
| 74 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { |
| 75 return agent_host_; |
| 76 } |
| 77 virtual bool Activate() const OVERRIDE; |
| 78 virtual bool Close() const OVERRIDE; |
| 79 |
| 80 private: |
| 81 virtual ~Target() {} |
| 82 |
| 83 scoped_refptr<DevToolsAgentHost> agent_host_; |
| 84 std::string id_; |
| 85 std::string title_; |
| 86 GURL url_; |
| 87 GURL favicon_url_; |
| 88 base::TimeTicks last_activity_time_; |
| 89 }; |
| 90 |
| 91 Target::Target(WebContents* web_contents) { |
| 92 agent_host_ = |
| 93 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost()); |
| 94 id_ = agent_host_->GetId(); |
| 95 title_ = UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle())); |
| 96 url_ = web_contents->GetURL(); |
| 97 content::NavigationController& controller = web_contents->GetController(); |
| 98 content::NavigationEntry* entry = controller.GetActiveEntry(); |
| 99 if (entry != NULL && entry->GetURL().is_valid()) |
| 100 favicon_url_ = entry->GetFavicon().url; |
| 101 last_activity_time_ = web_contents->GetLastSelectedTime(); |
| 102 } |
| 103 |
| 104 bool Target::Activate() const { |
| 105 RenderViewHost* rvh = agent_host_->GetRenderViewHost(); |
| 106 if (!rvh) |
| 107 return false; |
| 108 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); |
| 109 if (!web_contents) |
| 110 return false; |
| 111 web_contents->GetDelegate()->ActivateContents(web_contents); |
| 112 return true; |
| 113 } |
| 114 |
| 115 bool Target::Close() const { |
| 116 RenderViewHost* rvh = agent_host_->GetRenderViewHost(); |
| 117 if (!rvh) |
| 118 return false; |
| 119 rvh->ClosePage(); |
| 120 return true; |
| 121 } |
| 122 |
45 // Delegate implementation for the devtools http handler on android. A new | 123 // Delegate implementation for the devtools http handler on android. A new |
46 // instance of this gets created each time devtools is enabled. | 124 // instance of this gets created each time devtools is enabled. |
47 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { | 125 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { |
48 public: | 126 public: |
49 DevToolsServerDelegate() | 127 DevToolsServerDelegate() |
50 : last_tethering_socket_(0) { | 128 : last_tethering_socket_(0) { |
51 } | 129 } |
52 | 130 |
53 virtual std::string GetDiscoveryPageHTML() OVERRIDE { | 131 virtual std::string GetDiscoveryPageHTML() OVERRIDE { |
54 // TopSites updates itself after a delay. Ask TopSites to update itself | 132 // TopSites updates itself after a delay. Ask TopSites to update itself |
(...skipping 20 matching lines...) Expand all Loading... |
75 history::TopSites* top_sites = profile->GetTopSites(); | 153 history::TopSites* top_sites = profile->GetTopSites(); |
76 if (top_sites) { | 154 if (top_sites) { |
77 scoped_refptr<base::RefCountedMemory> data; | 155 scoped_refptr<base::RefCountedMemory> data; |
78 if (top_sites->GetPageThumbnail(url, false, &data)) | 156 if (top_sites->GetPageThumbnail(url, false, &data)) |
79 return std::string(reinterpret_cast<const char*>(data->front()), | 157 return std::string(reinterpret_cast<const char*>(data->front()), |
80 data->size()); | 158 data->size()); |
81 } | 159 } |
82 return ""; | 160 return ""; |
83 } | 161 } |
84 | 162 |
85 virtual content::RenderViewHost* CreateNewTarget() OVERRIDE { | 163 virtual scoped_refptr<content::DevToolsTarget> CreateNewTarget() OVERRIDE { |
86 Profile* profile = | 164 Profile* profile = |
87 g_browser_process->profile_manager()->GetDefaultProfile(); | 165 g_browser_process->profile_manager()->GetDefaultProfile(); |
88 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile); | 166 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile); |
89 if (!tab_model) | 167 if (!tab_model) |
90 return NULL; | 168 return NULL; |
91 content::WebContents* web_contents = | 169 WebContents* web_contents = |
92 tab_model->CreateTabForTesting(GURL(content::kAboutBlankURL)); | 170 tab_model->CreateTabForTesting(GURL(content::kAboutBlankURL)); |
93 if (!web_contents) | 171 if (!web_contents) |
94 return NULL; | 172 return NULL; |
95 return web_contents->GetRenderViewHost(); | 173 return new Target(web_contents); |
96 } | 174 } |
97 | 175 |
98 virtual TargetType GetTargetType(content::RenderViewHost*) OVERRIDE { | 176 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { |
99 return kTargetTypeTab; | 177 TargetList targets; |
100 } | 178 std::vector<RenderViewHost*> rvh_list = |
101 | 179 DevToolsAgentHost::GetValidRenderViewHosts(); |
102 virtual std::string GetViewDescription(content::RenderViewHost*) OVERRIDE { | 180 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin(); |
103 return ""; | 181 it != rvh_list.end(); ++it) { |
| 182 WebContents* web_contents = WebContents::FromRenderViewHost(*it); |
| 183 if (web_contents) |
| 184 targets.push_back(new Target(web_contents)); |
| 185 } |
| 186 callback.Run(targets); |
104 } | 187 } |
105 | 188 |
106 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( | 189 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( |
107 net::StreamListenSocket::Delegate* delegate, | 190 net::StreamListenSocket::Delegate* delegate, |
108 std::string* name) OVERRIDE { | 191 std::string* name) OVERRIDE { |
109 *name = base::StringPrintf( | 192 *name = base::StringPrintf( |
110 kTetheringSocketName, getpid(), ++last_tethering_socket_); | 193 kTetheringSocketName, getpid(), ++last_tethering_socket_); |
111 return net::UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 194 return net::UnixDomainSocket::CreateAndListenWithAbstractNamespace( |
112 *name, | 195 *name, |
113 "", | 196 "", |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 jobject obj, | 296 jobject obj, |
214 jint server, | 297 jint server, |
215 jboolean enabled) { | 298 jboolean enabled) { |
216 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); | 299 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); |
217 if (enabled) { | 300 if (enabled) { |
218 devtools_server->Start(); | 301 devtools_server->Start(); |
219 } else { | 302 } else { |
220 devtools_server->Stop(); | 303 devtools_server->Stop(); |
221 } | 304 } |
222 } | 305 } |
OLD | NEW |