| 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 "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 explicit RenderViewHostTargetsUIHandler(const Callback& callback); | 89 explicit RenderViewHostTargetsUIHandler(const Callback& callback); |
| 90 virtual ~RenderViewHostTargetsUIHandler(); | 90 virtual ~RenderViewHostTargetsUIHandler(); |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 // content::NotificationObserver overrides. | 93 // content::NotificationObserver overrides. |
| 94 virtual void Observe(int type, | 94 virtual void Observe(int type, |
| 95 const content::NotificationSource& source, | 95 const content::NotificationSource& source, |
| 96 const content::NotificationDetails& details) OVERRIDE; | 96 const content::NotificationDetails& details) OVERRIDE; |
| 97 | 97 |
| 98 void UpdateTargets(); | 98 void UpdateTargets(); |
| 99 void OnTargetsReceived(const DevToolsTargetImpl::List& targets); |
| 99 | 100 |
| 100 content::NotificationRegistrar notification_registrar_; | 101 content::NotificationRegistrar notification_registrar_; |
| 101 scoped_ptr<CancelableTimer> timer_; | 102 scoped_ptr<CancelableTimer> timer_; |
| 103 base::WeakPtrFactory<RenderViewHostTargetsUIHandler> weak_factory_; |
| 102 }; | 104 }; |
| 103 | 105 |
| 104 RenderViewHostTargetsUIHandler::RenderViewHostTargetsUIHandler( | 106 RenderViewHostTargetsUIHandler::RenderViewHostTargetsUIHandler( |
| 105 const Callback& callback) | 107 const Callback& callback) |
| 106 : DevToolsTargetsUIHandler(kTargetSourceRenderer, callback) { | 108 : DevToolsTargetsUIHandler(kTargetSourceRenderer, callback), |
| 109 weak_factory_(this) { |
| 107 notification_registrar_.Add(this, | 110 notification_registrar_.Add(this, |
| 108 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | 111 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, |
| 109 content::NotificationService::AllSources()); | 112 content::NotificationService::AllSources()); |
| 110 notification_registrar_.Add(this, | 113 notification_registrar_.Add(this, |
| 111 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | 114 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 112 content::NotificationService::AllSources()); | 115 content::NotificationService::AllSources()); |
| 113 notification_registrar_.Add(this, | 116 notification_registrar_.Add(this, |
| 114 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 117 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 115 content::NotificationService::AllSources()); | 118 content::NotificationService::AllSources()); |
| 116 UpdateTargets(); | 119 UpdateTargets(); |
| 117 } | 120 } |
| 118 | 121 |
| 119 RenderViewHostTargetsUIHandler::~RenderViewHostTargetsUIHandler() { | 122 RenderViewHostTargetsUIHandler::~RenderViewHostTargetsUIHandler() { |
| 120 notification_registrar_.RemoveAll(); | 123 notification_registrar_.RemoveAll(); |
| 121 } | 124 } |
| 122 | 125 |
| 123 void RenderViewHostTargetsUIHandler::Observe( | 126 void RenderViewHostTargetsUIHandler::Observe( |
| 124 int type, | 127 int type, |
| 125 const content::NotificationSource& source, | 128 const content::NotificationSource& source, |
| 126 const content::NotificationDetails& details) { | 129 const content::NotificationDetails& details) { |
| 127 const int kUpdateDelay = 100; | 130 const int kUpdateDelay = 100; |
| 128 timer_.reset( | 131 timer_.reset( |
| 129 new CancelableTimer( | 132 new CancelableTimer( |
| 130 base::Bind(&RenderViewHostTargetsUIHandler::UpdateTargets, | 133 base::Bind(&RenderViewHostTargetsUIHandler::UpdateTargets, |
| 131 base::Unretained(this)), | 134 base::Unretained(this)), |
| 132 base::TimeDelta::FromMilliseconds(kUpdateDelay))); | 135 base::TimeDelta::FromMilliseconds(kUpdateDelay))); |
| 133 } | 136 } |
| 134 | 137 |
| 135 void RenderViewHostTargetsUIHandler::UpdateTargets() { | 138 void RenderViewHostTargetsUIHandler::UpdateTargets() { |
| 139 DevToolsTargetImpl::EnumerateRenderViewHostTargets( |
| 140 base::Bind(&RenderViewHostTargetsUIHandler::OnTargetsReceived, |
| 141 weak_factory_.GetWeakPtr())); |
| 142 } |
| 143 |
| 144 void RenderViewHostTargetsUIHandler::OnTargetsReceived( |
| 145 const DevToolsTargetImpl::List& targets) { |
| 136 base::ListValue list_value; | 146 base::ListValue list_value; |
| 137 | 147 |
| 138 std::map<std::string, base::DictionaryValue*> id_to_descriptor; | 148 std::map<std::string, base::DictionaryValue*> id_to_descriptor; |
| 139 | 149 |
| 140 DevToolsTargetImpl::List targets = | |
| 141 DevToolsTargetImpl::EnumerateRenderViewHostTargets(); | |
| 142 | |
| 143 STLDeleteValues(&targets_); | 150 STLDeleteValues(&targets_); |
| 144 for (DevToolsTargetImpl::List::iterator it = targets.begin(); | 151 for (DevToolsTargetImpl::List::const_iterator it = targets.begin(); |
| 145 it != targets.end(); ++it) { | 152 it != targets.end(); ++it) { |
| 146 DevToolsTargetImpl* target = *it; | 153 DevToolsTargetImpl* target = *it; |
| 147 targets_[target->GetId()] = target; | 154 targets_[target->GetId()] = target; |
| 148 id_to_descriptor[target->GetId()] = Serialize(*target); | 155 id_to_descriptor[target->GetId()] = Serialize(*target); |
| 149 } | 156 } |
| 150 | 157 |
| 151 for (TargetMap::iterator it(targets_.begin()); it != targets_.end(); ++it) { | 158 for (TargetMap::iterator it(targets_.begin()); it != targets_.end(); ++it) { |
| 152 DevToolsTargetImpl* target = it->second; | 159 DevToolsTargetImpl* target = it->second; |
| 153 base::DictionaryValue* descriptor = id_to_descriptor[target->GetId()]; | 160 base::DictionaryValue* descriptor = id_to_descriptor[target->GetId()]; |
| 154 | 161 |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 base::StringPrintf("%d", it->first), it->second); | 569 base::StringPrintf("%d", it->first), it->second); |
| 563 } | 570 } |
| 564 | 571 |
| 565 std::string device_id = base::StringPrintf( | 572 std::string device_id = base::StringPrintf( |
| 566 kAdbDeviceIdFormat, | 573 kAdbDeviceIdFormat, |
| 567 sit->first.c_str()); | 574 sit->first.c_str()); |
| 568 result.Set(device_id, device_status_dict); | 575 result.Set(device_id, device_status_dict); |
| 569 } | 576 } |
| 570 callback_.Run(result); | 577 callback_.Run(result); |
| 571 } | 578 } |
| OLD | NEW |