| 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/ui/webui/inspect_ui.h" | 5 #include "chrome/browser/ui/webui/inspect_ui.h" |
| 6 | 6 |
| 7 #include <set> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "base/memory/ref_counted_memory.h" | |
| 13 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 14 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 9 #include "chrome/browser/devtools/devtools_adb_bridge.h" |
| 16 #include "base/strings/string_util.h" | 10 #include "chrome/browser/devtools/devtools_targets_ui.h" |
| 17 #include "base/strings/stringprintf.h" | |
| 18 #include "base/strings/utf_string_conversions.h" | |
| 19 #include "base/values.h" | |
| 20 #include "chrome/browser/devtools/devtools_target_impl.h" | |
| 21 #include "chrome/browser/devtools/port_forwarding_controller.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/ui/browser_navigator.h" | 12 #include "chrome/browser/ui/browser_navigator.h" |
| 24 #include "chrome/browser/ui/singleton_tabs.h" | 13 #include "chrome/browser/ui/singleton_tabs.h" |
| 25 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | |
| 26 #include "chrome/browser/ui/webui/theme_source.h" | 14 #include "chrome/browser/ui/webui/theme_source.h" |
| 27 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 29 #include "content/public/browser/browser_child_process_observer.h" | |
| 30 #include "content/public/browser/browser_thread.h" | |
| 31 #include "content/public/browser/child_process_data.h" | |
| 32 #include "content/public/browser/devtools_agent_host.h" | |
| 33 #include "content/public/browser/devtools_client_host.h" | |
| 34 #include "content/public/browser/devtools_manager.h" | |
| 35 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 36 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 37 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
| 38 #include "content/public/browser/render_process_host.h" | |
| 39 #include "content/public/browser/render_view_host.h" | |
| 40 #include "content/public/browser/user_metrics.h" | 20 #include "content/public/browser/user_metrics.h" |
| 41 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 42 #include "content/public/browser/web_ui.h" | 22 #include "content/public/browser/web_ui.h" |
| 43 #include "content/public/browser/web_ui_data_source.h" | 23 #include "content/public/browser/web_ui_data_source.h" |
| 44 #include "content/public/browser/web_ui_message_handler.h" | 24 #include "content/public/browser/web_ui_message_handler.h" |
| 45 #include "content/public/browser/worker_service.h" | |
| 46 #include "content/public/browser/worker_service_observer.h" | |
| 47 #include "content/public/common/process_type.h" | |
| 48 #include "grit/browser_resources.h" | 25 #include "grit/browser_resources.h" |
| 49 #include "grit/generated_resources.h" | |
| 50 #include "net/base/escape.h" | |
| 51 #include "net/base/net_errors.h" | |
| 52 #include "ui/base/resource/resource_bundle.h" | |
| 53 | 26 |
| 54 using content::BrowserThread; | |
| 55 using content::ChildProcessData; | |
| 56 using content::DevToolsAgentHost; | |
| 57 using content::DevToolsClientHost; | |
| 58 using content::DevToolsManager; | |
| 59 using content::RenderProcessHost; | |
| 60 using content::RenderViewHost; | |
| 61 using content::RenderViewHostDelegate; | |
| 62 using content::RenderWidgetHost; | |
| 63 using content::WebContents; | 27 using content::WebContents; |
| 64 using content::WebUIMessageHandler; | 28 using content::WebUIMessageHandler; |
| 65 using content::WorkerService; | |
| 66 using content::WorkerServiceObserver; | |
| 67 | 29 |
| 68 namespace { | 30 namespace { |
| 69 | 31 |
| 70 const char kWorkerTargetType[] = "worker"; | |
| 71 const char kAdbTargetType[] = "adb_page"; | |
| 72 | |
| 73 const char kInitUICommand[] = "init-ui"; | 32 const char kInitUICommand[] = "init-ui"; |
| 74 const char kInspectCommand[] = "inspect"; | 33 const char kInspectCommand[] = "inspect"; |
| 75 const char kActivateCommand[] = "activate"; | 34 const char kActivateCommand[] = "activate"; |
| 76 const char kCloseCommand[] = "close"; | 35 const char kCloseCommand[] = "close"; |
| 77 const char kReloadCommand[] = "reload"; | 36 const char kReloadCommand[] = "reload"; |
| 78 const char kOpenCommand[] = "open"; | 37 const char kOpenCommand[] = "open"; |
| 79 | 38 |
| 80 const char kDiscoverUsbDevicesEnabledCommand[] = | 39 const char kDiscoverUsbDevicesEnabledCommand[] = |
| 81 "set-discover-usb-devices-enabled"; | 40 "set-discover-usb-devices-enabled"; |
| 82 const char kPortForwardingEnabledCommand[] = | 41 const char kPortForwardingEnabledCommand[] = |
| 83 "set-port-forwarding-enabled"; | 42 "set-port-forwarding-enabled"; |
| 84 const char kPortForwardingConfigCommand[] = "set-port-forwarding-config"; | 43 const char kPortForwardingConfigCommand[] = "set-port-forwarding-config"; |
| 85 | 44 |
| 86 const char kPortForwardingDefaultPort[] = "8080"; | 45 const char kPortForwardingDefaultPort[] = "8080"; |
| 87 const char kPortForwardingDefaultLocation[] = "localhost:8080"; | 46 const char kPortForwardingDefaultLocation[] = "localhost:8080"; |
| 88 | 47 |
| 89 const char kTargetIdField[] = "id"; | |
| 90 const char kTargetTypeField[] = "type"; | |
| 91 const char kAttachedField[] = "attached"; | |
| 92 const char kUrlField[] = "url"; | |
| 93 const char kNameField[] = "name"; | |
| 94 const char kFaviconUrlField[] = "faviconUrl"; | |
| 95 const char kDescription[] = "description"; | |
| 96 const char kAdbConnectedField[] = "adbConnected"; | |
| 97 const char kAdbModelField[] = "adbModel"; | |
| 98 const char kAdbSerialField[] = "adbSerial"; | |
| 99 const char kAdbBrowserNameField[] = "adbBrowserName"; | |
| 100 const char kAdbBrowserVersionField[] = "adbBrowserVersion"; | |
| 101 const char kAdbBrowserChromeVersionField[] = "adbBrowserChromeVersion"; | |
| 102 const char kAdbGlobalIdField[] = "adbGlobalId"; | |
| 103 const char kAdbBrowsersField[] = "browsers"; | |
| 104 const char kAdbPagesField[] = "pages"; | |
| 105 const char kAdbPortStatus[] = "adbPortStatus"; | |
| 106 const char kAdbScreenWidthField[] = "adbScreenWidth"; | |
| 107 const char kAdbScreenHeightField[] = "adbScreenHeight"; | |
| 108 const char kAdbAttachedForeignField[] = "adbAttachedForeign"; | |
| 109 const char kGuestList[] = "guests"; | |
| 110 | |
| 111 DictionaryValue* BuildTargetDescriptor( | |
| 112 const DevToolsTargetImpl& target) { | |
| 113 DictionaryValue* target_data = new DictionaryValue(); | |
| 114 target_data->SetString(kTargetIdField, target.GetId()); | |
| 115 target_data->SetString(kTargetTypeField, target.GetType()); | |
| 116 target_data->SetBoolean(kAttachedField, target.IsAttached()); | |
| 117 target_data->SetString(kUrlField, target.GetUrl().spec()); | |
| 118 target_data->SetString(kNameField, net::EscapeForHTML(target.GetTitle())); | |
| 119 target_data->SetString(kFaviconUrlField, target.GetFaviconUrl().spec()); | |
| 120 target_data->SetString(kDescription, target.GetDescription()); | |
| 121 | |
| 122 return target_data; | |
| 123 } | |
| 124 | |
| 125 class InspectMessageHandler : public WebUIMessageHandler { | 48 class InspectMessageHandler : public WebUIMessageHandler { |
| 126 public: | 49 public: |
| 127 explicit InspectMessageHandler(InspectUI* inspect_ui) | 50 explicit InspectMessageHandler(InspectUI* inspect_ui) |
| 128 : inspect_ui_(inspect_ui) {} | 51 : inspect_ui_(inspect_ui) {} |
| 129 virtual ~InspectMessageHandler() {} | 52 virtual ~InspectMessageHandler() {} |
| 130 | 53 |
| 131 private: | 54 private: |
| 132 // WebUIMessageHandler implementation. | 55 // WebUIMessageHandler implementation. |
| 133 virtual void RegisterMessages() OVERRIDE; | 56 virtual void RegisterMessages() OVERRIDE; |
| 134 | 57 |
| 135 void HandleInitUICommand(const ListValue* args); | 58 void HandleInitUICommand(const ListValue* args); |
| 136 void HandleInspectCommand(const ListValue* args); | 59 void HandleInspectCommand(const ListValue* args); |
| 137 void HandleActivateCommand(const ListValue* args); | 60 void HandleActivateCommand(const ListValue* args); |
| 138 void HandleCloseCommand(const ListValue* args); | 61 void HandleCloseCommand(const ListValue* args); |
| 139 void HandleReloadCommand(const ListValue* args); | 62 void HandleReloadCommand(const ListValue* args); |
| 140 void HandleOpenCommand(const ListValue* args); | 63 void HandleOpenCommand(const ListValue* args); |
| 141 void HandleBooleanPrefChanged(const char* pref_name, | 64 void HandleBooleanPrefChanged(const char* pref_name, |
| 142 const ListValue* args); | 65 const ListValue* args); |
| 143 void HandlePortForwardingConfigCommand(const ListValue* args); | 66 void HandlePortForwardingConfigCommand(const ListValue* args); |
| 144 | 67 |
| 145 DevToolsTargetImpl* FindTarget(const ListValue* args); | |
| 146 | |
| 147 InspectUI* inspect_ui_; | 68 InspectUI* inspect_ui_; |
| 148 | 69 |
| 149 DISALLOW_COPY_AND_ASSIGN(InspectMessageHandler); | 70 DISALLOW_COPY_AND_ASSIGN(InspectMessageHandler); |
| 150 }; | 71 }; |
| 151 | 72 |
| 152 void InspectMessageHandler::RegisterMessages() { | 73 void InspectMessageHandler::RegisterMessages() { |
| 153 web_ui()->RegisterMessageCallback(kInitUICommand, | 74 web_ui()->RegisterMessageCallback(kInitUICommand, |
| 154 base::Bind(&InspectMessageHandler::HandleInitUICommand, | 75 base::Bind(&InspectMessageHandler::HandleInitUICommand, |
| 155 base::Unretained(this))); | 76 base::Unretained(this))); |
| 156 web_ui()->RegisterMessageCallback(kInspectCommand, | 77 web_ui()->RegisterMessageCallback(kInspectCommand, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 178 base::Unretained(this))); | 99 base::Unretained(this))); |
| 179 web_ui()->RegisterMessageCallback(kOpenCommand, | 100 web_ui()->RegisterMessageCallback(kOpenCommand, |
| 180 base::Bind(&InspectMessageHandler::HandleOpenCommand, | 101 base::Bind(&InspectMessageHandler::HandleOpenCommand, |
| 181 base::Unretained(this))); | 102 base::Unretained(this))); |
| 182 } | 103 } |
| 183 | 104 |
| 184 void InspectMessageHandler::HandleInitUICommand(const ListValue*) { | 105 void InspectMessageHandler::HandleInitUICommand(const ListValue*) { |
| 185 inspect_ui_->InitUI(); | 106 inspect_ui_->InitUI(); |
| 186 } | 107 } |
| 187 | 108 |
| 109 static bool ParseStringArgs(const ListValue* args, |
| 110 std::string* arg0, |
| 111 std::string* arg1, |
| 112 std::string* arg2 = 0) { |
| 113 int arg_size = args->GetSize(); |
| 114 return (!arg0 || (arg_size > 0 && args->GetString(0, arg0))) && |
| 115 (!arg1 || (arg_size > 1 && args->GetString(1, arg1))) && |
| 116 (!arg2 || (arg_size > 2 && args->GetString(2, arg2))); |
| 117 } |
| 118 |
| 188 void InspectMessageHandler::HandleInspectCommand(const ListValue* args) { | 119 void InspectMessageHandler::HandleInspectCommand(const ListValue* args) { |
| 189 Profile* profile = Profile::FromWebUI(web_ui()); | 120 std::string source; |
| 190 if (!profile) | 121 std::string id; |
| 191 return; | 122 if (ParseStringArgs(args, &source, &id)) |
| 192 DevToolsTargetImpl* target = FindTarget(args); | 123 inspect_ui_->Inspect(source, id); |
| 193 if (target) | |
| 194 target->Inspect(profile); | |
| 195 } | 124 } |
| 196 | 125 |
| 197 void InspectMessageHandler::HandleActivateCommand(const ListValue* args) { | 126 void InspectMessageHandler::HandleActivateCommand(const ListValue* args) { |
| 198 DevToolsTargetImpl* target = FindTarget(args); | 127 std::string source; |
| 199 if (target) | 128 std::string id; |
| 200 target->Activate(); | 129 if (ParseStringArgs(args, &source, &id)) |
| 130 inspect_ui_->Activate(source, id); |
| 201 } | 131 } |
| 202 | 132 |
| 203 void InspectMessageHandler::HandleCloseCommand(const ListValue* args) { | 133 void InspectMessageHandler::HandleCloseCommand(const ListValue* args) { |
| 204 DevToolsTargetImpl* target = FindTarget(args); | 134 std::string source; |
| 205 if (target) | 135 std::string id; |
| 206 target->Close(); | 136 if (ParseStringArgs(args, &source, &id)) |
| 137 inspect_ui_->Close(source, id); |
| 207 } | 138 } |
| 208 | 139 |
| 209 void InspectMessageHandler::HandleReloadCommand(const ListValue* args) { | 140 void InspectMessageHandler::HandleReloadCommand(const ListValue* args) { |
| 210 DevToolsTargetImpl* target = FindTarget(args); | 141 std::string source; |
| 211 if (target) | 142 std::string id; |
| 212 target->Reload(); | 143 if (ParseStringArgs(args, &source, &id)) |
| 144 inspect_ui_->Reload(source, id); |
| 213 } | 145 } |
| 214 | 146 |
| 215 void InspectMessageHandler::HandleOpenCommand(const ListValue* args) { | 147 void InspectMessageHandler::HandleOpenCommand(const ListValue* args) { |
| 216 if (args->GetSize() != 2) | 148 std::string source_id; |
| 217 return; | |
| 218 std::string browser_id; | 149 std::string browser_id; |
| 219 if (!args->GetString(0, &browser_id)) | |
| 220 return; | |
| 221 scoped_refptr<DevToolsAdbBridge::RemoteBrowser> remote_browser = | |
| 222 inspect_ui_->FindRemoteBrowser(browser_id); | |
| 223 if (!remote_browser) | |
| 224 return; | |
| 225 std::string url; | 150 std::string url; |
| 226 if (!args->GetString(1, &url)) | 151 if (ParseStringArgs(args, &source_id, &browser_id, &url)) |
| 227 return; | 152 inspect_ui_->Open(source_id, browser_id, url); |
| 228 GURL gurl(url); | |
| 229 if (!gurl.is_valid()) { | |
| 230 gurl = GURL("http://" + url); | |
| 231 if (!gurl.is_valid()) | |
| 232 return; | |
| 233 } | |
| 234 remote_browser->Open(gurl.spec()); | |
| 235 } | |
| 236 | |
| 237 DevToolsTargetImpl* InspectMessageHandler::FindTarget(const ListValue* args) { | |
| 238 const DictionaryValue* data; | |
| 239 std::string type; | |
| 240 std::string id; | |
| 241 if (args->GetSize() == 1 && args->GetDictionary(0, &data) && | |
| 242 data->GetString(kTargetTypeField, &type) && | |
| 243 data->GetString(kTargetIdField, &id)) { | |
| 244 return inspect_ui_->FindTarget(type, id); | |
| 245 } | |
| 246 return NULL; | |
| 247 } | 153 } |
| 248 | 154 |
| 249 void InspectMessageHandler::HandleBooleanPrefChanged( | 155 void InspectMessageHandler::HandleBooleanPrefChanged( |
| 250 const char* pref_name, | 156 const char* pref_name, |
| 251 const ListValue* args) { | 157 const ListValue* args) { |
| 252 Profile* profile = Profile::FromWebUI(web_ui()); | 158 Profile* profile = Profile::FromWebUI(web_ui()); |
| 253 if (!profile) | 159 if (!profile) |
| 254 return; | 160 return; |
| 255 | 161 |
| 256 bool enabled; | 162 bool enabled; |
| 257 if (args->GetSize() == 1 && args->GetBoolean(0, &enabled)) | 163 if (args->GetSize() == 1 && args->GetBoolean(0, &enabled)) |
| 258 profile->GetPrefs()->SetBoolean(pref_name, enabled); | 164 profile->GetPrefs()->SetBoolean(pref_name, enabled); |
| 259 } | 165 } |
| 260 | 166 |
| 261 void InspectMessageHandler::HandlePortForwardingConfigCommand( | 167 void InspectMessageHandler::HandlePortForwardingConfigCommand( |
| 262 const ListValue* args) { | 168 const ListValue* args) { |
| 263 Profile* profile = Profile::FromWebUI(web_ui()); | 169 Profile* profile = Profile::FromWebUI(web_ui()); |
| 264 if (!profile) | 170 if (!profile) |
| 265 return; | 171 return; |
| 266 | 172 |
| 267 const DictionaryValue* dict_src; | 173 const DictionaryValue* dict_src; |
| 268 if (args->GetSize() == 1 && args->GetDictionary(0, &dict_src)) | 174 if (args->GetSize() == 1 && args->GetDictionary(0, &dict_src)) |
| 269 profile->GetPrefs()->Set(prefs::kDevToolsPortForwardingConfig, *dict_src); | 175 profile->GetPrefs()->Set(prefs::kDevToolsPortForwardingConfig, *dict_src); |
| 270 } | 176 } |
| 271 | 177 |
| 272 } // namespace | 178 } // namespace |
| 273 | 179 |
| 274 class InspectUI::WorkerCreationDestructionListener | |
| 275 : public WorkerServiceObserver, | |
| 276 public content::BrowserChildProcessObserver, | |
| 277 public base::RefCountedThreadSafe<WorkerCreationDestructionListener> { | |
| 278 public: | |
| 279 WorkerCreationDestructionListener() | |
| 280 : discovery_ui_(NULL) {} | |
| 281 | |
| 282 void Init(InspectUI* workers_ui) { | |
| 283 DCHECK(workers_ui); | |
| 284 DCHECK(!discovery_ui_); | |
| 285 discovery_ui_ = workers_ui; | |
| 286 BrowserChildProcessObserver::Add(this); | |
| 287 BrowserThread::PostTask( | |
| 288 BrowserThread::IO, FROM_HERE, | |
| 289 base::Bind(&WorkerCreationDestructionListener::RegisterObserver, | |
| 290 this)); | |
| 291 } | |
| 292 | |
| 293 void InspectUIDestroyed() { | |
| 294 DCHECK(discovery_ui_); | |
| 295 discovery_ui_ = NULL; | |
| 296 BrowserChildProcessObserver::Remove(this); | |
| 297 BrowserThread::PostTask( | |
| 298 BrowserThread::IO, FROM_HERE, | |
| 299 base::Bind(&WorkerCreationDestructionListener::UnregisterObserver, | |
| 300 this)); | |
| 301 } | |
| 302 | |
| 303 void UpdateUI() { | |
| 304 BrowserThread::PostTask( | |
| 305 BrowserThread::IO, FROM_HERE, | |
| 306 base::Bind(&WorkerCreationDestructionListener::CollectWorkersData, | |
| 307 this)); | |
| 308 } | |
| 309 | |
| 310 private: | |
| 311 friend class base::RefCountedThreadSafe<WorkerCreationDestructionListener>; | |
| 312 virtual ~WorkerCreationDestructionListener() {} | |
| 313 | |
| 314 virtual void WorkerCreated( | |
| 315 const GURL& url, | |
| 316 const string16& name, | |
| 317 int process_id, | |
| 318 int route_id) OVERRIDE { | |
| 319 CollectWorkersData(); | |
| 320 } | |
| 321 | |
| 322 virtual void WorkerDestroyed(int process_id, int route_id) OVERRIDE { | |
| 323 CollectWorkersData(); | |
| 324 } | |
| 325 | |
| 326 virtual void BrowserChildProcessHostConnected( | |
| 327 const content::ChildProcessData& data) OVERRIDE { | |
| 328 if (data.process_type == content::PROCESS_TYPE_WORKER) | |
| 329 UpdateUI(); | |
| 330 } | |
| 331 | |
| 332 virtual void BrowserChildProcessHostDisconnected( | |
| 333 const content::ChildProcessData& data) OVERRIDE { | |
| 334 if (data.process_type == content::PROCESS_TYPE_WORKER) | |
| 335 UpdateUI(); | |
| 336 } | |
| 337 | |
| 338 void CollectWorkersData() { | |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 340 DevToolsTargetImpl::EnumerateWorkerTargets( | |
| 341 base::Bind( | |
| 342 &WorkerCreationDestructionListener::PopulateWorkersList, this)); | |
| 343 } | |
| 344 | |
| 345 void RegisterObserver() { | |
| 346 WorkerService::GetInstance()->AddObserver(this); | |
| 347 } | |
| 348 | |
| 349 void UnregisterObserver() { | |
| 350 WorkerService::GetInstance()->RemoveObserver(this); | |
| 351 } | |
| 352 | |
| 353 void PopulateWorkersList(const DevToolsTargetImpl::List& targets) { | |
| 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 355 if (discovery_ui_) | |
| 356 discovery_ui_->PopulateWorkerTargets(targets); | |
| 357 } | |
| 358 | |
| 359 InspectUI* discovery_ui_; | |
| 360 }; | |
| 361 | |
| 362 InspectUI::InspectUI(content::WebUI* web_ui) | 180 InspectUI::InspectUI(content::WebUI* web_ui) |
| 363 : WebUIController(web_ui) { | 181 : WebUIController(web_ui) { |
| 364 web_ui->AddMessageHandler(new InspectMessageHandler(this)); | 182 web_ui->AddMessageHandler(new InspectMessageHandler(this)); |
| 365 Profile* profile = Profile::FromWebUI(web_ui); | 183 Profile* profile = Profile::FromWebUI(web_ui); |
| 366 content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource()); | 184 content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource()); |
| 367 | 185 |
| 368 // Set up the chrome://theme/ source. | 186 // Set up the chrome://theme/ source. |
| 369 ThemeSource* theme = new ThemeSource(profile); | 187 ThemeSource* theme = new ThemeSource(profile); |
| 370 content::URLDataSource::Add(profile, theme); | 188 content::URLDataSource::Add(profile, theme); |
| 371 } | 189 } |
| 372 | 190 |
| 373 InspectUI::~InspectUI() { | 191 InspectUI::~InspectUI() { |
| 374 StopListeningNotifications(); | 192 StopListeningNotifications(); |
| 375 STLDeleteValues(&render_view_host_targets_); | |
| 376 } | 193 } |
| 377 | 194 |
| 378 void InspectUI::InitUI() { | 195 void InspectUI::InitUI() { |
| 379 SetPortForwardingDefaults(); | 196 SetPortForwardingDefaults(); |
| 380 StartListeningNotifications(); | 197 StartListeningNotifications(); |
| 381 PopulateWebContentsTargets(); | |
| 382 UpdateDiscoverUsbDevicesEnabled(); | 198 UpdateDiscoverUsbDevicesEnabled(); |
| 383 UpdatePortForwardingEnabled(); | 199 UpdatePortForwardingEnabled(); |
| 384 UpdatePortForwardingConfig(); | 200 UpdatePortForwardingConfig(); |
| 385 observer_->UpdateUI(); | |
| 386 } | 201 } |
| 387 | 202 |
| 388 DevToolsTargetImpl* InspectUI::FindTarget(const std::string& type, | 203 void InspectUI::Inspect(const std::string& source_id, |
| 389 const std::string& id) { | 204 const std::string& target_id) { |
| 390 if (type == kWorkerTargetType) { | 205 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id); |
| 391 TargetMap::iterator it = worker_targets_.find(id); | 206 if (handler) |
| 392 return it == worker_targets_.end() ? NULL : it->second; | 207 handler->Inspect(target_id, Profile::FromWebUI(web_ui())); |
| 393 } else if (type == kAdbTargetType) { | |
| 394 TargetMap::iterator it = remote_targets_.find(id); | |
| 395 return it == remote_targets_.end() ? NULL : it->second; | |
| 396 } else { | |
| 397 TargetMap::iterator it = render_view_host_targets_.find(id); | |
| 398 return it == render_view_host_targets_.end() ? NULL : it->second; | |
| 399 } | |
| 400 } | 208 } |
| 401 | 209 |
| 402 scoped_refptr<DevToolsAdbBridge::RemoteBrowser> | 210 void InspectUI::Activate(const std::string& source_id, |
| 403 InspectUI::FindRemoteBrowser(const std::string& id) { | 211 const std::string& target_id) { |
| 404 RemoteBrowsers::iterator it = remote_browsers_.find(id); | 212 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id); |
| 405 return it == remote_browsers_.end() ? NULL : it->second; | 213 if (handler) |
| 214 handler->Activate(target_id); |
| 215 } |
| 216 |
| 217 void InspectUI::Close(const std::string& source_id, |
| 218 const std::string& target_id) { |
| 219 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id); |
| 220 if (handler) |
| 221 handler->Close(target_id); |
| 222 } |
| 223 |
| 224 void InspectUI::Reload(const std::string& source_id, |
| 225 const std::string& target_id) { |
| 226 DevToolsTargetsUIHandler* handler = FindTargetHandler(source_id); |
| 227 if (handler) |
| 228 handler->Reload(target_id); |
| 229 } |
| 230 |
| 231 void InspectUI::Open(const std::string& source_id, |
| 232 const std::string& browser_id, |
| 233 const std::string& url) { |
| 234 DevToolsRemoteTargetsUIHandler* handler = FindRemoteTargetHandler(source_id); |
| 235 if (handler) |
| 236 handler->Open(browser_id, url); |
| 406 } | 237 } |
| 407 | 238 |
| 408 void InspectUI::InspectDevices(Browser* browser) { | 239 void InspectUI::InspectDevices(Browser* browser) { |
| 409 content::RecordAction(content::UserMetricsAction("InspectDevices")); | 240 content::RecordAction(content::UserMetricsAction("InspectDevices")); |
| 410 chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams( | 241 chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams( |
| 411 browser, GURL(chrome::kChromeUIInspectURL))); | 242 browser, GURL(chrome::kChromeUIInspectURL))); |
| 412 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE; | 243 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE; |
| 413 ShowSingletonTabOverwritingNTP(browser, params); | 244 ShowSingletonTabOverwritingNTP(browser, params); |
| 414 } | 245 } |
| 415 | 246 |
| 416 void InspectUI::PopulateWebContentsTargets() { | |
| 417 ListValue list_value; | |
| 418 | |
| 419 std::map<WebContents*, DictionaryValue*> web_contents_to_descriptor_; | |
| 420 std::vector<DevToolsTargetImpl*> guest_targets; | |
| 421 | |
| 422 DevToolsTargetImpl::List targets = | |
| 423 DevToolsTargetImpl::EnumerateRenderViewHostTargets(); | |
| 424 | |
| 425 STLDeleteValues(&render_view_host_targets_); | |
| 426 for (DevToolsTargetImpl::List::iterator it = targets.begin(); | |
| 427 it != targets.end(); ++it) { | |
| 428 scoped_ptr<DevToolsTargetImpl> target(*it); | |
| 429 RenderViewHost* rvh = target->GetRenderViewHost(); | |
| 430 if (!rvh) | |
| 431 continue; | |
| 432 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); | |
| 433 if (!web_contents) | |
| 434 continue; | |
| 435 | |
| 436 DevToolsTargetImpl* target_ptr = target.get(); | |
| 437 render_view_host_targets_[target_ptr->GetId()] = target.release(); | |
| 438 if (rvh->GetProcess()->IsGuest()) { | |
| 439 guest_targets.push_back(target_ptr); | |
| 440 } else { | |
| 441 DictionaryValue* descriptor = BuildTargetDescriptor(*target_ptr); | |
| 442 list_value.Append(descriptor); | |
| 443 web_contents_to_descriptor_[web_contents] = descriptor; | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 // Add the list of guest-views to each of its embedders. | |
| 448 for (std::vector<DevToolsTargetImpl*>::iterator it(guest_targets.begin()); | |
| 449 it != guest_targets.end(); ++it) { | |
| 450 DevToolsTargetImpl* guest = (*it); | |
| 451 WebContents* guest_web_contents = | |
| 452 WebContents::FromRenderViewHost(guest->GetRenderViewHost()); | |
| 453 WebContents* embedder = guest_web_contents->GetEmbedderWebContents(); | |
| 454 if (embedder && web_contents_to_descriptor_.count(embedder) > 0) { | |
| 455 DictionaryValue* parent = web_contents_to_descriptor_[embedder]; | |
| 456 ListValue* guests = NULL; | |
| 457 if (!parent->GetList(kGuestList, &guests)) { | |
| 458 guests = new ListValue(); | |
| 459 parent->Set(kGuestList, guests); | |
| 460 } | |
| 461 guests->Append(BuildTargetDescriptor(*guest)); | |
| 462 } | |
| 463 } | |
| 464 | |
| 465 web_ui()->CallJavascriptFunction("populateWebContentsTargets", list_value); | |
| 466 } | |
| 467 | |
| 468 void InspectUI::PopulateWorkerTargets(const DevToolsTargetImpl::List& targets) { | |
| 469 ListValue list_value; | |
| 470 | |
| 471 STLDeleteValues(&worker_targets_); | |
| 472 for (DevToolsTargetImpl::List::const_iterator it = targets.begin(); | |
| 473 it != targets.end(); ++it) { | |
| 474 DevToolsTargetImpl* target = *it; | |
| 475 list_value.Append(BuildTargetDescriptor(*target)); | |
| 476 worker_targets_[target->GetId()] = target; | |
| 477 } | |
| 478 | |
| 479 web_ui()->CallJavascriptFunction("populateWorkerTargets", list_value); | |
| 480 } | |
| 481 | |
| 482 void InspectUI::Observe(int type, | 247 void InspectUI::Observe(int type, |
| 483 const content::NotificationSource& source, | 248 const content::NotificationSource& source, |
| 484 const content::NotificationDetails& details) { | 249 const content::NotificationDetails& details) { |
| 485 if (source != content::Source<WebContents>(web_ui()->GetWebContents())) | 250 if (source == content::Source<WebContents>(web_ui()->GetWebContents())) |
| 486 PopulateWebContentsTargets(); | |
| 487 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED) | |
| 488 StopListeningNotifications(); | 251 StopListeningNotifications(); |
| 489 } | 252 } |
| 490 | 253 |
| 491 void InspectUI::StartListeningNotifications() { | 254 void InspectUI::StartListeningNotifications() { |
| 492 if (observer_) | 255 if (!target_handlers_.empty()) |
| 493 return; | 256 return; |
| 494 | 257 |
| 495 observer_ = new WorkerCreationDestructionListener(); | 258 Profile* profile = Profile::FromWebUI(web_ui()); |
| 496 observer_->Init(this); | |
| 497 | 259 |
| 498 Profile* profile = Profile::FromWebUI(web_ui()); | 260 DevToolsTargetsUIHandler::Callback callback = |
| 499 DevToolsAdbBridge* adb_bridge = | 261 base::Bind(&InspectUI::PopulateTargets, base::Unretained(this)); |
| 500 DevToolsAdbBridge::Factory::GetForProfile(profile); | 262 |
| 501 if (adb_bridge) | 263 AddTargetUIHandler( |
| 502 adb_bridge->AddListener(this); | 264 DevToolsTargetsUIHandler::CreateForRenderers(callback)); |
| 265 AddTargetUIHandler( |
| 266 DevToolsTargetsUIHandler::CreateForWorkers(callback)); |
| 267 AddRemoteTargetUIHandler( |
| 268 DevToolsRemoteTargetsUIHandler::CreateForAdb(callback, profile)); |
| 503 | 269 |
| 504 notification_registrar_.Add(this, | 270 notification_registrar_.Add(this, |
| 505 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | |
| 506 content::NotificationService::AllSources()); | |
| 507 notification_registrar_.Add(this, | |
| 508 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | 271 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 509 content::NotificationService::AllSources()); | 272 content::NotificationService::AllSources()); |
| 510 notification_registrar_.Add(this, | |
| 511 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
| 512 content::NotificationService::AllSources()); | |
| 513 | 273 |
| 514 pref_change_registrar_.Init(profile->GetPrefs()); | 274 pref_change_registrar_.Init(profile->GetPrefs()); |
| 515 pref_change_registrar_.Add(prefs::kDevToolsDiscoverUsbDevicesEnabled, | 275 pref_change_registrar_.Add(prefs::kDevToolsDiscoverUsbDevicesEnabled, |
| 516 base::Bind(&InspectUI::UpdateDiscoverUsbDevicesEnabled, | 276 base::Bind(&InspectUI::UpdateDiscoverUsbDevicesEnabled, |
| 517 base::Unretained(this))); | 277 base::Unretained(this))); |
| 518 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled, | 278 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingEnabled, |
| 519 base::Bind(&InspectUI::UpdatePortForwardingEnabled, | 279 base::Bind(&InspectUI::UpdatePortForwardingEnabled, |
| 520 base::Unretained(this))); | 280 base::Unretained(this))); |
| 521 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig, | 281 pref_change_registrar_.Add(prefs::kDevToolsPortForwardingConfig, |
| 522 base::Bind(&InspectUI::UpdatePortForwardingConfig, | 282 base::Bind(&InspectUI::UpdatePortForwardingConfig, |
| 523 base::Unretained(this))); | 283 base::Unretained(this))); |
| 524 } | 284 } |
| 525 | 285 |
| 526 void InspectUI::StopListeningNotifications() | 286 void InspectUI::StopListeningNotifications() { |
| 527 { | 287 if (target_handlers_.empty()) |
| 528 if (!observer_.get()) | |
| 529 return; | 288 return; |
| 530 Profile* profile = Profile::FromWebUI(web_ui()); | 289 |
| 531 DevToolsAdbBridge* adb_bridge = | 290 STLDeleteValues(&target_handlers_); |
| 532 DevToolsAdbBridge::Factory::GetForProfile(profile); | 291 STLDeleteValues(&remote_target_handlers_); |
| 533 if (adb_bridge) | 292 |
| 534 adb_bridge->RemoveListener(this); | |
| 535 observer_->InspectUIDestroyed(); | |
| 536 observer_ = NULL; | |
| 537 notification_registrar_.RemoveAll(); | 293 notification_registrar_.RemoveAll(); |
| 538 pref_change_registrar_.RemoveAll(); | 294 pref_change_registrar_.RemoveAll(); |
| 539 } | 295 } |
| 540 | 296 |
| 541 content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() { | 297 content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() { |
| 542 content::WebUIDataSource* source = | 298 content::WebUIDataSource* source = |
| 543 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost); | 299 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost); |
| 544 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS); | 300 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS); |
| 545 source->AddResourcePath("inspect.js", IDR_INSPECT_JS); | 301 source->AddResourcePath("inspect.js", IDR_INSPECT_JS); |
| 546 source->SetDefaultResource(IDR_INSPECT_HTML); | 302 source->SetDefaultResource(IDR_INSPECT_HTML); |
| 547 return source; | 303 return source; |
| 548 } | 304 } |
| 549 | 305 |
| 550 void InspectUI::RemoteDevicesChanged( | |
| 551 DevToolsAdbBridge::RemoteDevices* devices) { | |
| 552 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 553 PortForwardingController* port_forwarding_controller = | |
| 554 PortForwardingController::Factory::GetForProfile(profile); | |
| 555 PortForwardingController::DevicesStatus port_forwarding_status; | |
| 556 if (port_forwarding_controller) | |
| 557 port_forwarding_status = | |
| 558 port_forwarding_controller->UpdateDeviceList(*devices); | |
| 559 | |
| 560 remote_browsers_.clear(); | |
| 561 STLDeleteValues(&remote_targets_); | |
| 562 ListValue device_list; | |
| 563 for (DevToolsAdbBridge::RemoteDevices::iterator dit = devices->begin(); | |
| 564 dit != devices->end(); ++dit) { | |
| 565 DevToolsAdbBridge::RemoteDevice* device = dit->get(); | |
| 566 DictionaryValue* device_data = new DictionaryValue(); | |
| 567 device_data->SetString(kAdbModelField, device->GetModel()); | |
| 568 device_data->SetString(kAdbSerialField, device->GetSerial()); | |
| 569 device_data->SetBoolean(kAdbConnectedField, device->IsConnected()); | |
| 570 std::string device_id = base::StringPrintf( | |
| 571 "device:%s", | |
| 572 device->GetSerial().c_str()); | |
| 573 device_data->SetString(kAdbGlobalIdField, device_id); | |
| 574 ListValue* browser_list = new ListValue(); | |
| 575 device_data->Set(kAdbBrowsersField, browser_list); | |
| 576 | |
| 577 DevToolsAdbBridge::RemoteBrowsers& browsers = device->browsers(); | |
| 578 for (DevToolsAdbBridge::RemoteBrowsers::iterator bit = | |
| 579 browsers.begin(); bit != browsers.end(); ++bit) { | |
| 580 DevToolsAdbBridge::RemoteBrowser* browser = bit->get(); | |
| 581 DictionaryValue* browser_data = new DictionaryValue(); | |
| 582 browser_data->SetString(kAdbBrowserNameField, browser->display_name()); | |
| 583 browser_data->SetString(kAdbBrowserVersionField, browser->version()); | |
| 584 DevToolsAdbBridge::RemoteBrowser::ParsedVersion parsed = | |
| 585 browser->GetParsedVersion(); | |
| 586 browser_data->SetInteger( | |
| 587 kAdbBrowserChromeVersionField, | |
| 588 browser->IsChrome() && !parsed.empty() ? parsed[0] : 0); | |
| 589 std::string browser_id = base::StringPrintf( | |
| 590 "browser:%s:%s:%s:%s", | |
| 591 device->GetSerial().c_str(), // Ensure uniqueness across devices. | |
| 592 browser->display_name().c_str(), // Sort by display name. | |
| 593 browser->version().c_str(), // Then by version. | |
| 594 browser->socket().c_str()); // Ensure uniqueness on the device. | |
| 595 browser_data->SetString(kAdbGlobalIdField, browser_id); | |
| 596 remote_browsers_[browser_id] = browser; | |
| 597 ListValue* page_list = new ListValue(); | |
| 598 browser_data->Set(kAdbPagesField, page_list); | |
| 599 | |
| 600 DevToolsTargetImpl::List pages = browser->CreatePageTargets(); | |
| 601 for (DevToolsTargetImpl::List::iterator it = | |
| 602 pages.begin(); it != pages.end(); ++it) { | |
| 603 DevToolsTargetImpl* page = *it; | |
| 604 DictionaryValue* page_data = BuildTargetDescriptor(*page); | |
| 605 page_data->SetBoolean( | |
| 606 kAdbAttachedForeignField, | |
| 607 page->IsAttached() && | |
| 608 !DevToolsAdbBridge::HasDevToolsWindow(page->GetId())); | |
| 609 // Pass the screen size in the page object to make sure that | |
| 610 // the caching logic does not prevent the page item from updating | |
| 611 // when the screen size changes. | |
| 612 gfx::Size screen_size = device->screen_size(); | |
| 613 page_data->SetInteger(kAdbScreenWidthField, screen_size.width()); | |
| 614 page_data->SetInteger(kAdbScreenHeightField, screen_size.height()); | |
| 615 remote_targets_[page->GetId()] = page; | |
| 616 page_list->Append(page_data); | |
| 617 } | |
| 618 browser_list->Append(browser_data); | |
| 619 } | |
| 620 | |
| 621 if (port_forwarding_controller) { | |
| 622 PortForwardingController::DevicesStatus::iterator sit = | |
| 623 port_forwarding_status.find(device->GetSerial()); | |
| 624 if (sit != port_forwarding_status.end()) { | |
| 625 DictionaryValue* port_status_dict = new DictionaryValue(); | |
| 626 typedef PortForwardingController::PortStatusMap StatusMap; | |
| 627 const StatusMap& port_status = sit->second; | |
| 628 for (StatusMap::const_iterator it = port_status.begin(); | |
| 629 it != port_status.end(); ++it) { | |
| 630 port_status_dict->SetInteger( | |
| 631 base::StringPrintf("%d", it->first), it->second); | |
| 632 } | |
| 633 device_data->Set(kAdbPortStatus, port_status_dict); | |
| 634 } | |
| 635 } | |
| 636 | |
| 637 device_list.Append(device_data); | |
| 638 } | |
| 639 web_ui()->CallJavascriptFunction("populateRemoteTargets", device_list); | |
| 640 } | |
| 641 | |
| 642 void InspectUI::UpdateDiscoverUsbDevicesEnabled() { | 306 void InspectUI::UpdateDiscoverUsbDevicesEnabled() { |
| 643 const Value* value = GetPrefValue(prefs::kDevToolsDiscoverUsbDevicesEnabled); | 307 const Value* value = GetPrefValue(prefs::kDevToolsDiscoverUsbDevicesEnabled); |
| 644 web_ui()->CallJavascriptFunction("updateDiscoverUsbDevicesEnabled", *value); | 308 web_ui()->CallJavascriptFunction("updateDiscoverUsbDevicesEnabled", *value); |
| 645 | 309 |
| 646 // Configure adb bridge. | 310 // Configure adb bridge. |
| 647 Profile* profile = Profile::FromWebUI(web_ui()); | 311 Profile* profile = Profile::FromWebUI(web_ui()); |
| 648 DevToolsAdbBridge* adb_bridge = | 312 DevToolsAdbBridge* adb_bridge = |
| 649 DevToolsAdbBridge::Factory::GetForProfile(profile); | 313 DevToolsAdbBridge::Factory::GetForProfile(profile); |
| 650 if (adb_bridge) { | 314 if (adb_bridge) { |
| 651 bool enabled = false; | 315 bool enabled = false; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 base::DictionaryValue default_config; | 368 base::DictionaryValue default_config; |
| 705 default_config.SetString( | 369 default_config.SetString( |
| 706 kPortForwardingDefaultPort, kPortForwardingDefaultLocation); | 370 kPortForwardingDefaultPort, kPortForwardingDefaultLocation); |
| 707 prefs->Set(prefs::kDevToolsPortForwardingConfig, default_config); | 371 prefs->Set(prefs::kDevToolsPortForwardingConfig, default_config); |
| 708 } | 372 } |
| 709 | 373 |
| 710 const base::Value* InspectUI::GetPrefValue(const char* name) { | 374 const base::Value* InspectUI::GetPrefValue(const char* name) { |
| 711 Profile* profile = Profile::FromWebUI(web_ui()); | 375 Profile* profile = Profile::FromWebUI(web_ui()); |
| 712 return profile->GetPrefs()->FindPreference(name)->GetValue(); | 376 return profile->GetPrefs()->FindPreference(name)->GetValue(); |
| 713 } | 377 } |
| 378 |
| 379 void InspectUI::AddTargetUIHandler( |
| 380 scoped_ptr<DevToolsTargetsUIHandler> handler) { |
| 381 DevToolsTargetsUIHandler* handler_ptr = handler.release(); |
| 382 target_handlers_[handler_ptr->source_id()] = handler_ptr; |
| 383 } |
| 384 |
| 385 void InspectUI::AddRemoteTargetUIHandler( |
| 386 scoped_ptr<DevToolsRemoteTargetsUIHandler> handler) { |
| 387 DevToolsRemoteTargetsUIHandler* handler_ptr = handler.release(); |
| 388 remote_target_handlers_[handler_ptr->source_id()] = handler_ptr; |
| 389 } |
| 390 |
| 391 DevToolsTargetsUIHandler* InspectUI::FindTargetHandler( |
| 392 const std::string& source_id) { |
| 393 TargetHandlerMap::iterator it = target_handlers_.find(source_id); |
| 394 return it != target_handlers_.end() ? |
| 395 it->second : |
| 396 FindRemoteTargetHandler(source_id); |
| 397 } |
| 398 |
| 399 DevToolsRemoteTargetsUIHandler* InspectUI::FindRemoteTargetHandler( |
| 400 const std::string& source_id) { |
| 401 RemoteTargetHandlerMap::iterator it = remote_target_handlers_.find(source_id); |
| 402 return it != remote_target_handlers_.end() ? it->second : NULL; |
| 403 } |
| 404 |
| 405 void InspectUI::PopulateTargets(const std::string& source, |
| 406 scoped_ptr<base::ListValue> targets) { |
| 407 scoped_ptr<base::Value> source_value(base::Value::CreateStringValue(source)); |
| 408 web_ui()->CallJavascriptFunction( |
| 409 "populateTargets", |
| 410 *source_value.get(), |
| 411 *targets.get()); |
| 412 } |
| OLD | NEW |