Chromium Code Reviews| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "android_webview/common/aw_content_client.h" | 9 #include "android_webview/common/aw_content_client.h" |
| 10 #include "android_webview/native/aw_contents.h" | 10 #include "android_webview/native/aw_contents.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "components/devtools_http_handler/devtools_http_handler.h" | |
| 18 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" | |
| 19 #include "content/public/browser/android/devtools_auth.h" | 17 #include "content/public/browser/android/devtools_auth.h" |
| 20 #include "content/public/browser/devtools_agent_host.h" | 18 #include "content/public/browser/devtools_agent_host.h" |
| 19 #include "content/public/browser/devtools_manager_delegate.h" | |
| 21 #include "content/public/browser/devtools_socket_factory.h" | 20 #include "content/public/browser/devtools_socket_factory.h" |
| 22 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/common/user_agent.h" | 22 #include "content/public/common/user_agent.h" |
| 24 #include "jni/AwDevToolsServer_jni.h" | 23 #include "jni/AwDevToolsServer_jni.h" |
| 25 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 26 #include "net/socket/unix_domain_server_socket_posix.h" | 25 #include "net/socket/unix_domain_server_socket_posix.h" |
| 27 | 26 |
| 28 using base::android::JavaParamRef; | 27 using base::android::JavaParamRef; |
| 29 using content::DevToolsAgentHost; | 28 using content::DevToolsAgentHost; |
| 29 using content::RenderFrameHost; | |
| 30 using content::RenderViewHost; | 30 using content::RenderViewHost; |
| 31 using content::WebContents; | 31 using content::WebContents; |
| 32 using devtools_http_handler::DevToolsHttpHandler; | |
| 33 | 32 |
| 34 namespace { | 33 namespace { |
| 35 | 34 |
| 36 const char kFrontEndURL[] = | 35 const char kFrontEndURL[] = |
| 37 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html"; | 36 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html"; |
| 38 const char kSocketNameFormat[] = "webview_devtools_remote_%d"; | 37 const char kSocketNameFormat[] = "webview_devtools_remote_%d"; |
| 39 const char kTetheringSocketName[] = "webview_devtools_tethering_%d_%d"; | 38 const char kTetheringSocketName[] = "webview_devtools_tethering_%d_%d"; |
| 40 | 39 |
| 41 const int kBackLog = 10; | 40 const int kBackLog = 10; |
| 42 | 41 |
| 43 // Delegate implementation for the devtools http handler for WebView. A new | 42 // Delegate implementation for the devtools http handler for WebView. A new |
| 44 // instance of this gets created each time web debugging is enabled. | 43 // instance of this gets created each time web debugging is enabled. |
| 45 class AwDevToolsServerDelegate : | 44 class AwDevToolsServerDelegate : public content::DevToolsManagerDelegate { |
| 46 public devtools_http_handler::DevToolsHttpHandlerDelegate { | |
| 47 public: | 45 public: |
| 48 AwDevToolsServerDelegate() { | 46 AwDevToolsServerDelegate() { |
| 49 } | 47 } |
| 50 | 48 |
| 51 ~AwDevToolsServerDelegate() override {} | 49 ~AwDevToolsServerDelegate() override {} |
| 52 | 50 |
| 53 // devtools_http_handler::DevToolsHttpHandlerDelegate implementation. | 51 // content::DevToolsManagerDelegate implementation. |
| 52 void Inspect(DevToolsAgentHost* agent_host) override; | |
| 53 void DevToolsAgentStateChanged(DevToolsAgentHost* agent_host, | |
| 54 bool attached) override; | |
| 55 std::string GetTargetType(RenderFrameHost* host) override; | |
|
dgozman
2016/09/02 19:33:07
Let's turn SetDescription into GetTargetDescriptio
| |
| 56 std::string GetTargetTitle(RenderFrameHost* host) override; | |
| 57 scoped_refptr<DevToolsAgentHost> CreateNewTarget(const GURL& url) override; | |
| 58 base::DictionaryValue* HandleCommand( | |
| 59 DevToolsAgentHost* agent_host, | |
| 60 base::DictionaryValue* command) override; | |
| 54 std::string GetDiscoveryPageHTML() override; | 61 std::string GetDiscoveryPageHTML() override; |
| 55 std::string GetFrontendResource(const std::string& path) override; | 62 std::string GetFrontendResource(const std::string& path) override; |
| 56 | 63 |
| 57 private: | 64 private: |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate); | 65 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate); |
| 60 }; | 66 }; |
| 61 | 67 |
| 68 void AwDevToolsServerDelegate::Inspect(DevToolsAgentHost* agent_host) { | |
| 69 } | |
| 70 | |
| 71 void AwDevToolsServerDelegate::DevToolsAgentStateChanged( | |
| 72 DevToolsAgentHost* agent_host, bool attached) { | |
| 73 } | |
| 74 | |
| 75 std::string AwDevToolsServerDelegate::GetTargetType(RenderFrameHost* host) { | |
| 76 return "webview"; | |
| 77 } | |
| 78 | |
| 79 std::string AwDevToolsServerDelegate::GetTargetTitle(RenderFrameHost* host) { | |
| 80 return std::string(); | |
| 81 } | |
| 82 | |
| 83 scoped_refptr<DevToolsAgentHost> AwDevToolsServerDelegate::CreateNewTarget( | |
| 84 const GURL& url) { | |
| 85 return nullptr; | |
| 86 } | |
| 87 | |
| 88 base::DictionaryValue* AwDevToolsServerDelegate::HandleCommand( | |
| 89 DevToolsAgentHost* agent_host, | |
| 90 base::DictionaryValue* command) { | |
| 91 return nullptr; | |
| 92 } | |
| 62 | 93 |
| 63 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() { | 94 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() { |
| 64 const char html[] = | 95 const char html[] = |
| 65 "<html>" | 96 "<html>" |
| 66 "<head><title>WebView remote debugging</title></head>" | 97 "<head><title>WebView remote debugging</title></head>" |
| 67 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>" | 98 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>" |
| 68 "</body>" | 99 "</body>" |
| 69 "</html>"; | 100 "</html>"; |
| 70 return html; | 101 return html; |
| 71 } | 102 } |
| 72 | 103 |
| 73 std::string AwDevToolsServerDelegate::GetFrontendResource( | 104 std::string AwDevToolsServerDelegate::GetFrontendResource( |
| 74 const std::string& path) { | 105 const std::string& path) { |
| 75 return std::string(); | 106 return std::string(); |
| 76 } | 107 } |
| 77 | 108 |
| 78 // Factory for UnixDomainServerSocket. | 109 // Factory for UnixDomainServerSocket. |
| 79 class UnixDomainServerSocketFactory : public content::DevToolsSocketFactory { | 110 class UnixDomainServerSocketFactory : public content::DevToolsSocketFactory { |
| 80 public: | 111 public: |
| 81 explicit UnixDomainServerSocketFactory(const std::string& socket_name) | 112 explicit UnixDomainServerSocketFactory(const std::string& socket_name) |
| 82 : socket_name_(socket_name), | 113 : socket_name_(socket_name), |
| 83 last_tethering_socket_(0) { | 114 last_tethering_socket_(0) { |
| 84 } | 115 } |
| 85 | 116 |
| 86 private: | 117 private: |
| 87 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory. | 118 // content::DevToolsAgentHost::ServerSocketFactory. |
| 88 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { | 119 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 89 std::unique_ptr<net::UnixDomainServerSocket> socket( | 120 std::unique_ptr<net::UnixDomainServerSocket> socket( |
| 90 new net::UnixDomainServerSocket( | 121 new net::UnixDomainServerSocket( |
| 91 base::Bind(&content::CanUserConnectToDevTools), | 122 base::Bind(&content::CanUserConnectToDevTools), |
| 92 true /* use_abstract_namespace */)); | 123 true /* use_abstract_namespace */)); |
| 93 if (socket->BindAndListen(socket_name_, kBackLog) != net::OK) | 124 if (socket->BindAndListen(socket_name_, kBackLog) != net::OK) |
| 94 return std::unique_ptr<net::ServerSocket>(); | 125 return std::unique_ptr<net::ServerSocket>(); |
| 95 | 126 |
| 96 return std::move(socket); | 127 return std::move(socket); |
| 97 } | 128 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 113 std::string socket_name_; | 144 std::string socket_name_; |
| 114 int last_tethering_socket_; | 145 int last_tethering_socket_; |
| 115 | 146 |
| 116 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); | 147 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); |
| 117 }; | 148 }; |
| 118 | 149 |
| 119 } // namespace | 150 } // namespace |
| 120 | 151 |
| 121 namespace android_webview { | 152 namespace android_webview { |
| 122 | 153 |
| 123 AwDevToolsServer::AwDevToolsServer() { | 154 AwDevToolsServer::AwDevToolsServer() : is_started_(false) { |
| 124 } | 155 } |
| 125 | 156 |
| 126 AwDevToolsServer::~AwDevToolsServer() { | 157 AwDevToolsServer::~AwDevToolsServer() { |
| 127 Stop(); | 158 Stop(); |
| 128 } | 159 } |
| 129 | 160 |
| 130 void AwDevToolsServer::Start() { | 161 void AwDevToolsServer::Start() { |
| 131 if (devtools_http_handler_) | 162 if (is_started_) |
| 132 return; | 163 return; |
| 164 is_started_ = true; | |
| 133 | 165 |
| 134 std::unique_ptr<content::DevToolsSocketFactory> factory( | 166 std::unique_ptr<content::DevToolsSocketFactory> factory( |
| 135 new UnixDomainServerSocketFactory( | 167 new UnixDomainServerSocketFactory( |
| 136 base::StringPrintf(kSocketNameFormat, getpid()))); | 168 base::StringPrintf(kSocketNameFormat, getpid()))); |
| 137 devtools_http_handler_.reset(new DevToolsHttpHandler( | 169 DevToolsAgentHost::StartRemoteDebuggingServer( |
| 138 std::move(factory), | 170 std::move(factory), |
| 139 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), | 171 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), |
| 140 new AwDevToolsServerDelegate(), base::FilePath(), base::FilePath(), | 172 base::FilePath(), base::FilePath(), |
| 141 GetProduct(), GetUserAgent())); | 173 GetProduct(), GetUserAgent()); |
| 142 } | 174 } |
| 143 | 175 |
| 144 void AwDevToolsServer::Stop() { | 176 void AwDevToolsServer::Stop() { |
| 145 devtools_http_handler_.reset(); | 177 DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 178 is_started_ = false; | |
| 146 } | 179 } |
| 147 | 180 |
| 148 bool AwDevToolsServer::IsStarted() const { | 181 bool AwDevToolsServer::IsStarted() const { |
| 149 return !!devtools_http_handler_; | 182 return is_started_; |
| 150 } | 183 } |
| 151 | 184 |
| 152 bool RegisterAwDevToolsServer(JNIEnv* env) { | 185 bool RegisterAwDevToolsServer(JNIEnv* env) { |
| 153 return RegisterNativesImpl(env); | 186 return RegisterNativesImpl(env); |
| 154 } | 187 } |
| 155 | 188 |
| 156 static jlong InitRemoteDebugging(JNIEnv* env, | 189 static jlong InitRemoteDebugging(JNIEnv* env, |
| 157 const JavaParamRef<jobject>& obj) { | 190 const JavaParamRef<jobject>& obj) { |
| 158 AwDevToolsServer* server = new AwDevToolsServer(); | 191 AwDevToolsServer* server = new AwDevToolsServer(); |
| 159 return reinterpret_cast<intptr_t>(server); | 192 return reinterpret_cast<intptr_t>(server); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 172 AwDevToolsServer* devtools_server = | 205 AwDevToolsServer* devtools_server = |
| 173 reinterpret_cast<AwDevToolsServer*>(server); | 206 reinterpret_cast<AwDevToolsServer*>(server); |
| 174 if (enabled) { | 207 if (enabled) { |
| 175 devtools_server->Start(); | 208 devtools_server->Start(); |
| 176 } else { | 209 } else { |
| 177 devtools_server->Stop(); | 210 devtools_server->Stop(); |
| 178 } | 211 } |
| 179 } | 212 } |
| 180 | 213 |
| 181 } // namespace android_webview | 214 } // namespace android_webview |
| OLD | NEW |