OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "android_webview/native/aw_dev_tools_server.h" | 5 #include "android_webview/native/aw_dev_tools_server.h" |
6 | 6 |
7 #include "android_webview/browser/in_process_view_renderer.h" | 7 #include "android_webview/browser/in_process_view_renderer.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "content/public/browser/android/devtools_auth.h" | 13 #include "content/public/browser/android/devtools_auth.h" |
| 14 #include "content/public/browser/devtools_agent_host.h" |
13 #include "content/public/browser/devtools_http_handler.h" | 15 #include "content/public/browser/devtools_http_handler.h" |
14 #include "content/public/browser/devtools_http_handler_delegate.h" | 16 #include "content/public/browser/devtools_http_handler_delegate.h" |
| 17 #include "content/public/browser/devtools_target.h" |
15 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
16 #include "jni/AwDevToolsServer_jni.h" | 19 #include "jni/AwDevToolsServer_jni.h" |
| 20 #include "net/base/escape.h" |
17 #include "net/socket/unix_domain_socket_posix.h" | 21 #include "net/socket/unix_domain_socket_posix.h" |
18 #include "webkit/common/user_agent/user_agent_util.h" | 22 #include "webkit/common/user_agent/user_agent_util.h" |
19 | 23 |
| 24 using content::DevToolsAgentHost; |
| 25 using content::RenderViewHost; |
| 26 using content::WebContents; |
| 27 |
20 namespace { | 28 namespace { |
21 | 29 |
22 const char kFrontEndURL[] = | 30 const char kFrontEndURL[] = |
23 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html"; | 31 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html"; |
24 const char kSocketNameFormat[] = "webview_devtools_remote_%d"; | 32 const char kSocketNameFormat[] = "webview_devtools_remote_%d"; |
25 | 33 |
| 34 const char kTargetTypePage[] = "page"; |
| 35 |
| 36 std::string GetViewDescription(WebContents* web_contents); |
| 37 |
| 38 class Target : public content::DevToolsTarget { |
| 39 public: |
| 40 explicit Target(WebContents* web_contents); |
| 41 |
| 42 virtual std::string GetId() const OVERRIDE { return id_; } |
| 43 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; } |
| 44 virtual std::string GetTitle() const OVERRIDE { return title_; } |
| 45 virtual std::string GetDescription() const OVERRIDE { return description_; } |
| 46 virtual GURL GetUrl() const OVERRIDE { return url_; } |
| 47 virtual GURL GetFaviconUrl() const OVERRIDE { return GURL(); } |
| 48 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE { |
| 49 return last_activity_time_; |
| 50 } |
| 51 virtual bool IsAttached() const OVERRIDE { |
| 52 return agent_host_->IsAttached(); |
| 53 } |
| 54 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { |
| 55 return agent_host_; |
| 56 } |
| 57 virtual bool Activate() const OVERRIDE { return false; } |
| 58 virtual bool Close() const OVERRIDE { return false; } |
| 59 |
| 60 private: |
| 61 scoped_refptr<DevToolsAgentHost> agent_host_; |
| 62 std::string id_; |
| 63 std::string title_; |
| 64 std::string description_; |
| 65 GURL url_; |
| 66 base::TimeTicks last_activity_time_; |
| 67 }; |
| 68 |
| 69 Target::Target(WebContents* web_contents) { |
| 70 agent_host_ = |
| 71 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost()); |
| 72 id_ = agent_host_->GetId(); |
| 73 description_ = GetViewDescription(web_contents); |
| 74 title_ = UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle())); |
| 75 url_ = web_contents->GetURL(); |
| 76 last_activity_time_ = web_contents->GetLastSelectedTime(); |
| 77 } |
| 78 |
26 // Delegate implementation for the devtools http handler for WebView. A new | 79 // Delegate implementation for the devtools http handler for WebView. A new |
27 // instance of this gets created each time web debugging is enabled. | 80 // instance of this gets created each time web debugging is enabled. |
28 class AwDevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { | 81 class AwDevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { |
29 public: | 82 public: |
30 AwDevToolsServerDelegate() {} | 83 AwDevToolsServerDelegate() {} |
31 virtual ~AwDevToolsServerDelegate() {} | 84 virtual ~AwDevToolsServerDelegate() {} |
32 | 85 |
33 // DevToolsHttpProtocolHandler::Delegate overrides. | 86 // DevToolsHttpProtocolHandler::Delegate overrides. |
34 virtual std::string GetDiscoveryPageHTML() OVERRIDE; | 87 virtual std::string GetDiscoveryPageHTML() OVERRIDE; |
35 | 88 |
36 virtual bool BundlesFrontendResources() OVERRIDE { | 89 virtual bool BundlesFrontendResources() OVERRIDE { |
37 return false; | 90 return false; |
38 } | 91 } |
39 | 92 |
40 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { | 93 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { |
41 return base::FilePath(); | 94 return base::FilePath(); |
42 } | 95 } |
43 | 96 |
44 virtual std::string GetPageThumbnailData(const GURL&) OVERRIDE { | 97 virtual std::string GetPageThumbnailData(const GURL&) OVERRIDE { |
45 return ""; | 98 return ""; |
46 } | 99 } |
47 | 100 |
48 virtual content::RenderViewHost* CreateNewTarget() OVERRIDE { | 101 virtual scoped_ptr<content::DevToolsTarget> CreateNewTarget() OVERRIDE { |
49 return NULL; | 102 return scoped_ptr<content::DevToolsTarget>(); |
50 } | 103 } |
51 | 104 |
52 virtual TargetType GetTargetType(content::RenderViewHost*) OVERRIDE { | 105 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { |
53 return kTargetTypeTab; | 106 TargetList targets; |
| 107 std::vector<RenderViewHost*> rvh_list = |
| 108 DevToolsAgentHost::GetValidRenderViewHosts(); |
| 109 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin(); |
| 110 it != rvh_list.end(); ++it) { |
| 111 WebContents* web_contents = WebContents::FromRenderViewHost(*it); |
| 112 if (web_contents) |
| 113 targets.push_back(new Target(web_contents)); |
| 114 } |
| 115 callback.Run(targets); |
54 } | 116 } |
55 | 117 |
56 virtual std::string GetViewDescription(content::RenderViewHost*) OVERRIDE; | |
57 | |
58 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( | 118 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( |
59 net::StreamListenSocket::Delegate* delegate, | 119 net::StreamListenSocket::Delegate* delegate, |
60 std::string* name) OVERRIDE { | 120 std::string* name) OVERRIDE { |
61 return scoped_ptr<net::StreamListenSocket>(); | 121 return scoped_ptr<net::StreamListenSocket>(); |
62 } | 122 } |
63 | 123 |
64 private: | 124 private: |
65 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate); | 125 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate); |
66 }; | 126 }; |
67 | 127 |
68 | 128 |
69 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() { | 129 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() { |
70 const char html[] = | 130 const char html[] = |
71 "<html>" | 131 "<html>" |
72 "<head><title>WebView remote debugging</title></head>" | 132 "<head><title>WebView remote debugging</title></head>" |
73 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>" | 133 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>" |
74 "</body>" | 134 "</body>" |
75 "</html>"; | 135 "</html>"; |
76 return html; | 136 return html; |
77 } | 137 } |
78 | 138 |
79 std::string AwDevToolsServerDelegate::GetViewDescription( | 139 std::string GetViewDescription(WebContents* web_contents) { |
80 content::RenderViewHost* rvh) { | |
81 content::WebContents* web_contents = | |
82 content::WebContents::FromRenderViewHost(rvh); | |
83 if (!web_contents) return ""; | |
84 | |
85 android_webview::BrowserViewRenderer* bvr | 140 android_webview::BrowserViewRenderer* bvr |
86 = android_webview::InProcessViewRenderer::FromWebContents(web_contents); | 141 = android_webview::InProcessViewRenderer::FromWebContents(web_contents); |
87 if (!bvr) return ""; | 142 if (!bvr) return ""; |
88 base::DictionaryValue description; | 143 base::DictionaryValue description; |
89 description.SetBoolean("attached", bvr->IsAttachedToWindow()); | 144 description.SetBoolean("attached", bvr->IsAttachedToWindow()); |
90 description.SetBoolean("visible", bvr->IsVisible()); | 145 description.SetBoolean("visible", bvr->IsVisible()); |
91 gfx::Rect screen_rect = bvr->GetScreenRect(); | 146 gfx::Rect screen_rect = bvr->GetScreenRect(); |
92 description.SetInteger("screenX", screen_rect.x()); | 147 description.SetInteger("screenX", screen_rect.x()); |
93 description.SetInteger("screenY", screen_rect.y()); | 148 description.SetInteger("screenY", screen_rect.y()); |
94 description.SetBoolean("empty", screen_rect.size().IsEmpty()); | 149 description.SetBoolean("empty", screen_rect.size().IsEmpty()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 AwDevToolsServer* devtools_server = | 216 AwDevToolsServer* devtools_server = |
162 reinterpret_cast<AwDevToolsServer*>(server); | 217 reinterpret_cast<AwDevToolsServer*>(server); |
163 if (enabled) { | 218 if (enabled) { |
164 devtools_server->Start(); | 219 devtools_server->Start(); |
165 } else { | 220 } else { |
166 devtools_server->Stop(); | 221 devtools_server->Stop(); |
167 } | 222 } |
168 } | 223 } |
169 | 224 |
170 } // namespace android_webview | 225 } // namespace android_webview |
OLD | NEW |