| 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" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 // corresponding string on the client side. Since debugging an older version of | 58 // corresponding string on the client side. Since debugging an older version of |
| 59 // Chrome for Android from a newer version of desktop Chrome is a very common | 59 // Chrome for Android from a newer version of desktop Chrome is a very common |
| 60 // scenario, the client code will have to be modified to recognize both the old | 60 // scenario, the client code will have to be modified to recognize both the old |
| 61 // and the new format. | 61 // and the new format. |
| 62 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote"; | 62 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote"; |
| 63 | 63 |
| 64 const char kFrontEndURL[] = | 64 const char kFrontEndURL[] = |
| 65 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html"; | 65 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html"; |
| 66 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d"; | 66 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d"; |
| 67 | 67 |
| 68 const char kTargetTypePage[] = "page"; | |
| 69 const char kTargetTypeOther[] = "other"; | |
| 70 | |
| 71 static GURL GetFaviconURLForContents(WebContents* web_contents) { | 68 static GURL GetFaviconURLForContents(WebContents* web_contents) { |
| 72 content::NavigationController& controller = web_contents->GetController(); | 69 content::NavigationController& controller = web_contents->GetController(); |
| 73 content::NavigationEntry* entry = controller.GetActiveEntry(); | 70 content::NavigationEntry* entry = controller.GetActiveEntry(); |
| 74 if (entry != NULL && entry->GetURL().is_valid()) | 71 if (entry != NULL && entry->GetURL().is_valid()) |
| 75 return entry->GetFavicon().url; | 72 return entry->GetFavicon().url; |
| 76 return GURL(); | 73 return GURL(); |
| 77 } | 74 } |
| 78 | 75 |
| 76 static GURL GetFaviconURLForAgentHost( |
| 77 scoped_refptr<DevToolsAgentHost> agent_host) { |
| 78 if (WebContents* web_contents = agent_host->GetWebContents()) |
| 79 return GetFaviconURLForContents(web_contents); |
| 80 return GURL(); |
| 81 } |
| 82 |
| 83 static base::TimeTicks GetLastActiveTimeForAgentHost( |
| 84 scoped_refptr<DevToolsAgentHost> agent_host) { |
| 85 if (WebContents* web_contents = agent_host->GetWebContents()) |
| 86 return web_contents->GetLastActiveTime(); |
| 87 return base::TimeTicks(); |
| 88 } |
| 89 |
| 79 bool AuthorizeSocketAccessWithDebugPermission( | 90 bool AuthorizeSocketAccessWithDebugPermission( |
| 80 const net::UnixDomainServerSocket::Credentials& credentials) { | 91 const net::UnixDomainServerSocket::Credentials& credentials) { |
| 81 JNIEnv* env = base::android::AttachCurrentThread(); | 92 JNIEnv* env = base::android::AttachCurrentThread(); |
| 82 return Java_DevToolsServer_checkDebugPermission( | 93 return Java_DevToolsServer_checkDebugPermission( |
| 83 env, base::android::GetApplicationContext(), | 94 env, base::android::GetApplicationContext(), |
| 84 credentials.process_id, credentials.user_id) || | 95 credentials.process_id, credentials.user_id) || |
| 85 content::CanUserConnectToDevTools(credentials); | 96 content::CanUserConnectToDevTools(credentials); |
| 86 } | 97 } |
| 87 | 98 |
| 88 class TargetBase : public content::DevToolsTarget { | 99 class TargetBase : public content::DevToolsTarget { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 103 } | 114 } |
| 104 | 115 |
| 105 protected: | 116 protected: |
| 106 explicit TargetBase(WebContents* web_contents) | 117 explicit TargetBase(WebContents* web_contents) |
| 107 : title_(base::UTF16ToUTF8(web_contents->GetTitle())), | 118 : title_(base::UTF16ToUTF8(web_contents->GetTitle())), |
| 108 url_(web_contents->GetURL()), | 119 url_(web_contents->GetURL()), |
| 109 favicon_url_(GetFaviconURLForContents(web_contents)), | 120 favicon_url_(GetFaviconURLForContents(web_contents)), |
| 110 last_activity_time_(web_contents->GetLastActiveTime()) { | 121 last_activity_time_(web_contents->GetLastActiveTime()) { |
| 111 } | 122 } |
| 112 | 123 |
| 113 TargetBase(const base::string16& title, const GURL& url) | 124 explicit TargetBase(scoped_refptr<DevToolsAgentHost> agent_host) |
| 114 : title_(base::UTF16ToUTF8(title)), | 125 : title_(agent_host->GetTitle()), |
| 115 url_(url) | 126 url_(agent_host->GetURL()), |
| 116 {} | 127 favicon_url_(GetFaviconURLForAgentHost(agent_host)), |
| 128 last_activity_time_(GetLastActiveTimeForAgentHost(agent_host)) { |
| 129 } |
| 130 |
| 131 TargetBase(const std::string& title, const GURL& url) |
| 132 : title_(title), |
| 133 url_(url) { |
| 134 } |
| 117 | 135 |
| 118 private: | 136 private: |
| 119 const std::string title_; | 137 const std::string title_; |
| 120 const GURL url_; | 138 const GURL url_; |
| 121 const GURL favicon_url_; | 139 const GURL favicon_url_; |
| 122 const base::TimeTicks last_activity_time_; | 140 const base::TimeTicks last_activity_time_; |
| 123 }; | 141 }; |
| 124 | 142 |
| 125 class TabTarget : public TargetBase { | 143 class TabTarget : public TargetBase { |
| 126 public: | 144 public: |
| 127 static TabTarget* CreateForWebContents(int tab_id, | 145 static TabTarget* CreateForWebContents(int tab_id, |
| 128 WebContents* web_contents) { | 146 WebContents* web_contents) { |
| 129 return new TabTarget(tab_id, web_contents); | 147 return new TabTarget(tab_id, web_contents); |
| 130 } | 148 } |
| 131 | 149 |
| 132 static TabTarget* CreateForUnloadedTab(int tab_id, | 150 static TabTarget* CreateForUnloadedTab(int tab_id, |
| 133 const base::string16& title, | 151 const base::string16& title, |
| 134 const GURL& url) { | 152 const GURL& url) { |
| 135 return new TabTarget(tab_id, title, url); | 153 return new TabTarget(tab_id, title, url); |
| 136 } | 154 } |
| 137 | 155 |
| 138 // content::DevToolsTarget implementation: | 156 // content::DevToolsTarget implementation: |
| 139 virtual std::string GetId() const OVERRIDE { | 157 virtual std::string GetId() const OVERRIDE { |
| 140 return base::IntToString(tab_id_); | 158 return base::IntToString(tab_id_); |
| 141 } | 159 } |
| 142 | 160 |
| 143 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; } | 161 virtual std::string GetType() const OVERRIDE { |
| 162 return content::kAgentHostTypePage; |
| 163 } |
| 144 | 164 |
| 145 virtual bool IsAttached() const OVERRIDE { | 165 virtual bool IsAttached() const OVERRIDE { |
| 146 TabModel* model; | 166 TabModel* model; |
| 147 int index; | 167 int index; |
| 148 if (!FindTab(&model, &index)) | 168 if (!FindTab(&model, &index)) |
| 149 return false; | 169 return false; |
| 150 WebContents* web_contents = model->GetWebContentsAt(index); | 170 WebContents* web_contents = model->GetWebContentsAt(index); |
| 151 if (!web_contents) | 171 if (!web_contents) |
| 152 return false; | 172 return false; |
| 153 return DevToolsAgentHost::IsDebuggerAttached(web_contents); | 173 return DevToolsAgentHost::IsDebuggerAttached(web_contents); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 return true; | 208 return true; |
| 189 } | 209 } |
| 190 | 210 |
| 191 private: | 211 private: |
| 192 TabTarget(int tab_id, WebContents* web_contents) | 212 TabTarget(int tab_id, WebContents* web_contents) |
| 193 : TargetBase(web_contents), | 213 : TargetBase(web_contents), |
| 194 tab_id_(tab_id) { | 214 tab_id_(tab_id) { |
| 195 } | 215 } |
| 196 | 216 |
| 197 TabTarget(int tab_id, const base::string16& title, const GURL& url) | 217 TabTarget(int tab_id, const base::string16& title, const GURL& url) |
| 198 : TargetBase(title, url), | 218 : TargetBase(base::UTF16ToUTF8(title), url), |
| 199 tab_id_(tab_id) { | 219 tab_id_(tab_id) { |
| 200 } | 220 } |
| 201 | 221 |
| 202 bool FindTab(TabModel** model_result, int* index_result) const { | 222 bool FindTab(TabModel** model_result, int* index_result) const { |
| 203 for (TabModelList::const_iterator iter = TabModelList::begin(); | 223 for (TabModelList::const_iterator iter = TabModelList::begin(); |
| 204 iter != TabModelList::end(); ++iter) { | 224 iter != TabModelList::end(); ++iter) { |
| 205 TabModel* model = *iter; | 225 TabModel* model = *iter; |
| 206 for (int i = 0; i < model->GetTabCount(); ++i) { | 226 for (int i = 0; i < model->GetTabCount(); ++i) { |
| 207 TabAndroid* tab = model->GetTabAt(i); | 227 TabAndroid* tab = model->GetTabAt(i); |
| 208 if (tab->GetAndroidId() == tab_id_) { | 228 if (tab->GetAndroidId() == tab_id_) { |
| 209 *model_result = model; | 229 *model_result = model; |
| 210 *index_result = i; | 230 *index_result = i; |
| 211 return true; | 231 return true; |
| 212 } | 232 } |
| 213 } | 233 } |
| 214 } | 234 } |
| 215 return false; | 235 return false; |
| 216 } | 236 } |
| 217 | 237 |
| 218 const int tab_id_; | 238 const int tab_id_; |
| 219 }; | 239 }; |
| 220 | 240 |
| 221 class NonTabTarget : public TargetBase { | 241 class NonTabTarget : public TargetBase { |
| 222 public: | 242 public: |
| 223 explicit NonTabTarget(WebContents* web_contents) | 243 explicit NonTabTarget(scoped_refptr<DevToolsAgentHost> agent_host) |
| 224 : TargetBase(web_contents), | 244 : TargetBase(agent_host), |
| 225 agent_host_(DevToolsAgentHost::GetOrCreateFor(web_contents)) { | 245 agent_host_(agent_host) { |
| 226 } | 246 } |
| 227 | 247 |
| 228 // content::DevToolsTarget implementation: | 248 // content::DevToolsTarget implementation: |
| 229 virtual std::string GetId() const OVERRIDE { | 249 virtual std::string GetId() const OVERRIDE { |
| 230 return agent_host_->GetId(); | 250 return agent_host_->GetId(); |
| 231 } | 251 } |
| 232 | 252 |
| 233 virtual std::string GetType() const OVERRIDE { | 253 virtual std::string GetType() const OVERRIDE { |
| 234 if (TabModelList::begin() == TabModelList::end()) { | 254 return agent_host_->GetType(); |
| 235 // If there are no tab models we must be running in ChromeShell. | |
| 236 // Return the 'page' target type for backwards compatibility. | |
| 237 return kTargetTypePage; | |
| 238 } | |
| 239 return kTargetTypeOther; | |
| 240 } | 255 } |
| 241 | 256 |
| 242 virtual bool IsAttached() const OVERRIDE { | 257 virtual bool IsAttached() const OVERRIDE { |
| 243 return agent_host_->IsAttached(); | 258 return agent_host_->IsAttached(); |
| 244 } | 259 } |
| 245 | 260 |
| 246 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { | 261 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { |
| 247 return agent_host_; | 262 return agent_host_; |
| 248 } | 263 } |
| 249 | 264 |
| 250 virtual bool Activate() const OVERRIDE { | 265 virtual bool Activate() const OVERRIDE { |
| 251 WebContents* web_contents = agent_host_->GetWebContents(); | 266 return agent_host_->Activate(); |
| 252 if (!web_contents) | |
| 253 return false; | |
| 254 web_contents->GetDelegate()->ActivateContents(web_contents); | |
| 255 return true; | |
| 256 } | 267 } |
| 257 | 268 |
| 258 virtual bool Close() const OVERRIDE { | 269 virtual bool Close() const OVERRIDE { |
| 259 WebContents* web_contents = agent_host_->GetWebContents(); | 270 return agent_host_->Close(); |
| 260 if (!web_contents) | |
| 261 return false; | |
| 262 web_contents->GetRenderViewHost()->ClosePage(); | |
| 263 return true; | |
| 264 } | 271 } |
| 265 | 272 |
| 266 private: | 273 private: |
| 267 scoped_refptr<DevToolsAgentHost> agent_host_; | 274 scoped_refptr<DevToolsAgentHost> agent_host_; |
| 268 }; | 275 }; |
| 269 | 276 |
| 270 // Delegate implementation for the devtools http handler on android. A new | 277 // Delegate implementation for the devtools http handler on android. A new |
| 271 // instance of this gets created each time devtools is enabled. | 278 // instance of this gets created each time devtools is enabled. |
| 272 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { | 279 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { |
| 273 public: | 280 public: |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 web_contents)); | 351 web_contents)); |
| 345 } else { | 352 } else { |
| 346 targets.push_back(TabTarget::CreateForUnloadedTab(tab->GetAndroidId(), | 353 targets.push_back(TabTarget::CreateForUnloadedTab(tab->GetAndroidId(), |
| 347 tab->GetTitle(), | 354 tab->GetTitle(), |
| 348 tab->GetURL())); | 355 tab->GetURL())); |
| 349 } | 356 } |
| 350 } | 357 } |
| 351 } | 358 } |
| 352 | 359 |
| 353 // Add targets for WebContents not associated with any tabs. | 360 // Add targets for WebContents not associated with any tabs. |
| 354 std::vector<WebContents*> wc_list = | 361 DevToolsAgentHost::List agents = |
| 355 DevToolsAgentHost::GetInspectableWebContents(); | 362 DevToolsAgentHost::GetOrCreateAll(); |
| 356 for (std::vector<WebContents*>::iterator it = wc_list.begin(); | 363 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
| 357 it != wc_list.end(); | 364 it != agents.end(); ++it) { |
| 358 ++it) { | 365 if (WebContents* web_contents = (*it)->GetWebContents()) { |
| 359 if (tab_web_contents.find(*it) != tab_web_contents.end()) | 366 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) |
| 360 continue; | 367 continue; |
| 368 } |
| 361 targets.push_back(new NonTabTarget(*it)); | 369 targets.push_back(new NonTabTarget(*it)); |
| 362 } | 370 } |
| 363 | 371 |
| 364 callback.Run(targets); | 372 callback.Run(targets); |
| 365 } | 373 } |
| 366 | 374 |
| 367 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( | 375 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( |
| 368 net::StreamListenSocket::Delegate* delegate, | 376 net::StreamListenSocket::Delegate* delegate, |
| 369 std::string* name) OVERRIDE { | 377 std::string* name) OVERRIDE { |
| 370 *name = base::StringPrintf( | 378 *name = base::StringPrintf( |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 jlong server, | 478 jlong server, |
| 471 jboolean enabled, | 479 jboolean enabled, |
| 472 jboolean allow_debug_permission) { | 480 jboolean allow_debug_permission) { |
| 473 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); | 481 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); |
| 474 if (enabled) { | 482 if (enabled) { |
| 475 devtools_server->Start(allow_debug_permission); | 483 devtools_server->Start(allow_debug_permission); |
| 476 } else { | 484 } else { |
| 477 devtools_server->Stop(); | 485 devtools_server->Stop(); |
| 478 } | 486 } |
| 479 } | 487 } |
| OLD | NEW |