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 "chrome/browser/devtools/devtools_targets_ui.h" | 5 #include "chrome/browser/devtools/devtools_targets_ui.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "base/version.h" | 19 #include "base/version.h" |
20 #include "chrome/browser/devtools/device/devtools_android_bridge.h" | 20 #include "chrome/browser/devtools/device/devtools_android_bridge.h" |
21 #include "chrome/browser/devtools/serialize_dictionary_forest.h" | |
21 #include "content/public/browser/browser_child_process_observer.h" | 22 #include "content/public/browser/browser_child_process_observer.h" |
22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/child_process_data.h" | 24 #include "content/public/browser/child_process_data.h" |
24 #include "content/public/browser/devtools_agent_host.h" | 25 #include "content/public/browser/devtools_agent_host.h" |
25 #include "content/public/browser/notification_observer.h" | 26 #include "content/public/browser/notification_observer.h" |
26 #include "content/public/browser/notification_registrar.h" | 27 #include "content/public/browser/notification_registrar.h" |
27 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
28 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
29 #include "content/public/browser/notification_types.h" | 30 #include "content/public/browser/notification_types.h" |
30 #include "content/public/browser/worker_service.h" | 31 #include "content/public/browser/worker_service.h" |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 base::Unretained(this)), | 227 base::Unretained(this)), |
227 base::TimeDelta::FromMilliseconds(kUpdateDelay))); | 228 base::TimeDelta::FromMilliseconds(kUpdateDelay))); |
228 } | 229 } |
229 | 230 |
230 void LocalTargetsUIHandler::UpdateTargets() { | 231 void LocalTargetsUIHandler::UpdateTargets() { |
231 SendTargets(DevToolsAgentHost::GetOrCreateAll()); | 232 SendTargets(DevToolsAgentHost::GetOrCreateAll()); |
232 } | 233 } |
233 | 234 |
234 void LocalTargetsUIHandler::SendTargets( | 235 void LocalTargetsUIHandler::SendTargets( |
235 const content::DevToolsAgentHost::List& targets) { | 236 const content::DevToolsAgentHost::List& targets) { |
236 base::ListValue list_value; | 237 std::vector<DictionaryForestNode> forest; |
237 std::map<std::string, base::DictionaryValue*> id_to_descriptor; | 238 forest.reserve(targets.size()); |
238 | 239 |
239 targets_.clear(); | 240 { |
240 for (scoped_refptr<DevToolsAgentHost> host : targets) { | 241 std::map<std::string, DictionaryForestNode*> id_to_node; |
jdoerrie
2017/04/18 09:57:44
Small suggestion:
Given that you care about perfor
vabr (Chromium)
2017/04/19 11:03:17
Thanks!
What I'm reading in the description of ba
jdoerrie
2017/04/19 11:45:51
No, you are right, the main advantages of flat con
| |
241 targets_[host->GetId()] = host; | |
242 id_to_descriptor[host->GetId()] = Serialize(host).release(); | |
243 } | |
244 | 242 |
245 for (auto& it : targets_) { | 243 // Reset |targets_| and fill the forest with nodes without edges. |
246 scoped_refptr<DevToolsAgentHost> host = it.second; | 244 targets_.clear(); |
247 base::DictionaryValue* descriptor = id_to_descriptor[host->GetId()]; | 245 for (const scoped_refptr<DevToolsAgentHost>& host : targets) { |
248 DCHECK(descriptor); | 246 targets_[host->GetId()] = host; |
249 std::string parent_id = host->GetParentId(); | 247 forest.push_back({nullptr, *Serialize(host.get())}); |
250 if (parent_id.empty() || id_to_descriptor.count(parent_id) == 0) { | 248 id_to_node[host->GetId()] = &forest.back(); |
251 list_value.Append(base::WrapUnique(descriptor)); | 249 } |
252 } else { | 250 |
253 base::DictionaryValue* parent = id_to_descriptor[parent_id]; | 251 // Now add the edges. |
254 base::ListValue* guests_weak = NULL; | 252 for (const scoped_refptr<DevToolsAgentHost>& host : targets) { |
255 if (!parent->GetList(kGuestList, &guests_weak)) { | 253 std::string parent_id = host->GetParentId(); |
256 auto guests = base::MakeUnique<base::ListValue>(); | 254 if (parent_id.empty()) |
257 guests_weak = guests.get(); | 255 continue; |
258 parent->Set(kGuestList, std::move(guests)); | 256 auto node_it = id_to_node.find(parent_id); |
259 } | 257 if (node_it == id_to_node.end()) |
260 guests_weak->Append(base::WrapUnique(descriptor)); | 258 continue; |
259 id_to_node[host->GetId()]->parent = node_it->second; | |
261 } | 260 } |
262 } | 261 } |
263 | 262 |
264 SendSerializedTargets(list_value); | 263 SendSerializedTargets( |
264 SerializeDictionaryForest(std::move(forest), kGuestList)); | |
dgozman
2017/04/17 21:22:43
Since there could be no more than one parent, I ha
vabr (Chromium)
2017/04/18 08:52:49
Thanks for the suggestion.
One thing which is les
| |
265 } | 265 } |
266 | 266 |
267 // AdbTargetsUIHandler -------------------------------------------------------- | 267 // AdbTargetsUIHandler -------------------------------------------------------- |
268 | 268 |
269 class AdbTargetsUIHandler | 269 class AdbTargetsUIHandler |
270 : public DevToolsTargetsUIHandler, | 270 : public DevToolsTargetsUIHandler, |
271 public DevToolsAndroidBridge::DeviceListListener { | 271 public DevToolsAndroidBridge::DeviceListListener { |
272 public: | 272 public: |
273 AdbTargetsUIHandler(const Callback& callback, Profile* profile); | 273 AdbTargetsUIHandler(const Callback& callback, Profile* profile); |
274 ~AdbTargetsUIHandler() override; | 274 ~AdbTargetsUIHandler() override; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
362 kAdbBrowserChromeVersionField, | 362 kAdbBrowserChromeVersionField, |
363 browser->IsChrome() && !parsed.empty() ? parsed[0] : 0); | 363 browser->IsChrome() && !parsed.empty() ? parsed[0] : 0); |
364 std::string browser_id = browser->GetId(); | 364 std::string browser_id = browser->GetId(); |
365 browser_data->SetString(kTargetIdField, browser_id); | 365 browser_data->SetString(kTargetIdField, browser_id); |
366 browser_data->SetString(kTargetSourceField, source_id()); | 366 browser_data->SetString(kTargetSourceField, source_id()); |
367 | 367 |
368 auto page_list = base::MakeUnique<base::ListValue>(); | 368 auto page_list = base::MakeUnique<base::ListValue>(); |
369 remote_browsers_[browser_id] = browser; | 369 remote_browsers_[browser_id] = browser; |
370 for (const auto& page : browser->pages()) { | 370 for (const auto& page : browser->pages()) { |
371 scoped_refptr<DevToolsAgentHost> host = page->CreateTarget(); | 371 scoped_refptr<DevToolsAgentHost> host = page->CreateTarget(); |
372 std::unique_ptr<base::DictionaryValue> target_data = Serialize(host); | 372 std::unique_ptr<base::DictionaryValue> target_data = |
373 Serialize(host.get()); | |
373 // Pass the screen size in the target object to make sure that | 374 // Pass the screen size in the target object to make sure that |
374 // the caching logic does not prevent the target item from updating | 375 // the caching logic does not prevent the target item from updating |
375 // when the screen size changes. | 376 // when the screen size changes. |
376 gfx::Size screen_size = device->screen_size(); | 377 gfx::Size screen_size = device->screen_size(); |
377 target_data->SetInteger(kAdbScreenWidthField, screen_size.width()); | 378 target_data->SetInteger(kAdbScreenWidthField, screen_size.width()); |
378 target_data->SetInteger(kAdbScreenHeightField, screen_size.height()); | 379 target_data->SetInteger(kAdbScreenHeightField, screen_size.height()); |
379 targets_[host->GetId()] = host; | 380 targets_[host->GetId()] = host; |
380 page_list->Append(std::move(target_data)); | 381 page_list->Append(std::move(target_data)); |
381 } | 382 } |
382 browser_data->Set(kAdbPagesList, std::move(page_list)); | 383 browser_data->Set(kAdbPagesList, std::move(page_list)); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 void DevToolsTargetsUIHandler::Open(const std::string& browser_id, | 432 void DevToolsTargetsUIHandler::Open(const std::string& browser_id, |
432 const std::string& url) { | 433 const std::string& url) { |
433 } | 434 } |
434 | 435 |
435 scoped_refptr<DevToolsAgentHost> | 436 scoped_refptr<DevToolsAgentHost> |
436 DevToolsTargetsUIHandler::GetBrowserAgentHost(const std::string& browser_id) { | 437 DevToolsTargetsUIHandler::GetBrowserAgentHost(const std::string& browser_id) { |
437 return NULL; | 438 return NULL; |
438 } | 439 } |
439 | 440 |
440 std::unique_ptr<base::DictionaryValue> DevToolsTargetsUIHandler::Serialize( | 441 std::unique_ptr<base::DictionaryValue> DevToolsTargetsUIHandler::Serialize( |
441 scoped_refptr<DevToolsAgentHost> host) { | 442 DevToolsAgentHost* host) { |
442 auto target_data = base::MakeUnique<base::DictionaryValue>(); | 443 auto target_data = base::MakeUnique<base::DictionaryValue>(); |
443 target_data->SetString(kTargetSourceField, source_id_); | 444 target_data->SetString(kTargetSourceField, source_id_); |
444 target_data->SetString(kTargetIdField, host->GetId()); | 445 target_data->SetString(kTargetIdField, host->GetId()); |
445 target_data->SetString(kTargetTypeField, host->GetType()); | 446 target_data->SetString(kTargetTypeField, host->GetType()); |
446 target_data->SetBoolean(kAttachedField, host->IsAttached()); | 447 target_data->SetBoolean(kAttachedField, host->IsAttached()); |
447 target_data->SetString(kUrlField, host->GetURL().spec()); | 448 target_data->SetString(kUrlField, host->GetURL().spec()); |
448 target_data->SetString(kNameField, host->GetTitle()); | 449 target_data->SetString(kNameField, host->GetTitle()); |
449 target_data->SetString(kFaviconUrlField, host->GetFaviconURL().spec()); | 450 target_data->SetString(kFaviconUrlField, host->GetFaviconURL().spec()); |
450 target_data->SetString(kDescriptionField, host->GetDescription()); | 451 target_data->SetString(kDescriptionField, host->GetDescription()); |
451 return target_data; | 452 return target_data; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
495 device_status_dict->SetString(kPortForwardingBrowserId, | 496 device_status_dict->SetString(kPortForwardingBrowserId, |
496 sit->first->GetId()); | 497 sit->first->GetId()); |
497 | 498 |
498 std::string device_id = base::StringPrintf( | 499 std::string device_id = base::StringPrintf( |
499 kAdbDeviceIdFormat, | 500 kAdbDeviceIdFormat, |
500 sit->first->serial().c_str()); | 501 sit->first->serial().c_str()); |
501 result.Set(device_id, std::move(device_status_dict)); | 502 result.Set(device_id, std::move(device_status_dict)); |
502 } | 503 } |
503 callback_.Run(result); | 504 callback_.Run(result); |
504 } | 505 } |
OLD | NEW |