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