[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/devtools/devtools_targets_ui.h" |
| 6 | |
dcheng | 71bb845 | 2016-09-17 01:30:15 | [diff] [blame] | 7 | #include <memory> |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 10 | #include "base/bind.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 11 | #include "base/location.h" |
[email protected] | e90c5f9b | 2013-12-11 12:49:44 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 14 | #include "base/stl_util.h" |
ricea | a01edead | 2015-07-01 15:56:50 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 18 | #include "base/values.h" |
[email protected] | 167206cf | 2014-02-17 13:16:40 | [diff] [blame] | 19 | #include "base/version.h" |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 20 | #include "chrome/browser/devtools/device/devtools_android_bridge.h" |
Pavol Marko | 56df027 | 2018-07-04 17:55:04 | [diff] [blame] | 21 | #include "chrome/browser/devtools/devtools_window.h" |
vabr | 6681597 | 2017-04-20 08:27:06 | [diff] [blame] | 22 | #include "chrome/browser/devtools/serialize_host_descriptions.h" |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 23 | #include "content/public/browser/browser_thread.h" |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 24 | #include "content/public/browser/devtools_agent_host.h" |
Dmitry Gozman | 72b5bd9 | 2017-11-21 00:03:18 | [diff] [blame] | 25 | #include "content/public/browser/devtools_agent_host_observer.h" |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 26 | |
| 27 | using content::BrowserThread; |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 28 | using content::DevToolsAgentHost; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
| 32 | const char kTargetSourceField[] = "source"; |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 33 | const char kTargetSourceLocal[] = "local"; |
| 34 | const char kTargetSourceRemote[] = "remote"; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 35 | |
| 36 | const char kTargetIdField[] = "id"; |
| 37 | const char kTargetTypeField[] = "type"; |
| 38 | const char kAttachedField[] = "attached"; |
| 39 | const char kUrlField[] = "url"; |
| 40 | const char kNameField[] = "name"; |
| 41 | const char kFaviconUrlField[] = "faviconUrl"; |
| 42 | const char kDescriptionField[] = "description"; |
| 43 | |
| 44 | const char kGuestList[] = "guests"; |
| 45 | |
| 46 | const char kAdbModelField[] = "adbModel"; |
| 47 | const char kAdbConnectedField[] = "adbConnected"; |
| 48 | const char kAdbSerialField[] = "adbSerial"; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 49 | const char kAdbBrowsersList[] = "browsers"; |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 50 | const char kAdbDeviceIdFormat[] = "device:%s"; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 51 | |
| 52 | const char kAdbBrowserNameField[] = "adbBrowserName"; |
vkuzkokov | 952b347 | 2015-01-15 15:55:26 | [diff] [blame] | 53 | const char kAdbBrowserUserField[] = "adbBrowserUser"; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 54 | const char kAdbBrowserVersionField[] = "adbBrowserVersion"; |
| 55 | const char kAdbBrowserChromeVersionField[] = "adbBrowserChromeVersion"; |
| 56 | const char kAdbPagesList[] = "pages"; |
| 57 | |
| 58 | const char kAdbScreenWidthField[] = "adbScreenWidth"; |
| 59 | const char kAdbScreenHeightField[] = "adbScreenHeight"; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 60 | |
vkuzkokov | 32c0c07 | 2014-10-01 14:10:38 | [diff] [blame] | 61 | const char kPortForwardingPorts[] = "ports"; |
| 62 | const char kPortForwardingBrowserId[] = "browserId"; |
| 63 | |
[email protected] | e90c5f9b | 2013-12-11 12:49:44 | [diff] [blame] | 64 | // CancelableTimer ------------------------------------------------------------ |
| 65 | |
| 66 | class CancelableTimer { |
| 67 | public: |
| 68 | CancelableTimer(base::Closure callback, base::TimeDelta delay) |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 69 | : callback_(callback) { |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 70 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e90c5f9b | 2013-12-11 12:49:44 | [diff] [blame] | 71 | FROM_HERE, |
tzik | 93bf8a7 | 2017-04-24 18:53:30 | [diff] [blame] | 72 | base::BindOnce(&CancelableTimer::Fire, weak_factory_.GetWeakPtr()), |
| 73 | delay); |
[email protected] | f30ed78 | 2014-05-16 17:52:50 | [diff] [blame] | 74 | } |
[email protected] | e90c5f9b | 2013-12-11 12:49:44 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | void Fire() { callback_.Run(); } |
| 78 | |
| 79 | base::Closure callback_; |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 80 | base::WeakPtrFactory<CancelableTimer> weak_factory_{this}; |
[email protected] | e90c5f9b | 2013-12-11 12:49:44 | [diff] [blame] | 81 | }; |
| 82 | |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 83 | // LocalTargetsUIHandler --------------------------------------------- |
| 84 | |
Dmitry Gozman | 72b5bd9 | 2017-11-21 00:03:18 | [diff] [blame] | 85 | class LocalTargetsUIHandler : public DevToolsTargetsUIHandler, |
| 86 | public content::DevToolsAgentHostObserver { |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 87 | public: |
Dmitry Gozman | 9e85208c7 | 2018-03-21 00:47:43 | [diff] [blame] | 88 | LocalTargetsUIHandler(const Callback& callback, Profile* profile); |
dcheng | 03748a4 | 2014-10-21 10:19:13 | [diff] [blame] | 89 | ~LocalTargetsUIHandler() override; |
[email protected] | 8ff7a29 | 2014-07-08 23:20:53 | [diff] [blame] | 90 | |
dgozman | ffe70eca | 2014-09-18 14:28:11 | [diff] [blame] | 91 | // DevToolsTargetsUIHandler overrides. |
dcheng | 03748a4 | 2014-10-21 10:19:13 | [diff] [blame] | 92 | void ForceUpdate() override; |
dgozman | ffe70eca | 2014-09-18 14:28:11 | [diff] [blame] | 93 | |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 94 | private: |
Dmitry Gozman | 72b5bd9 | 2017-11-21 00:03:18 | [diff] [blame] | 95 | // content::DevToolsAgentHostObserver overrides. |
| 96 | bool ShouldForceDevToolsAgentHostCreation() override; |
| 97 | void DevToolsAgentHostCreated(DevToolsAgentHost* agent_host) override; |
| 98 | void DevToolsAgentHostDestroyed(DevToolsAgentHost* agent_host) override; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 99 | |
Dmitry Gozman | 72b5bd9 | 2017-11-21 00:03:18 | [diff] [blame] | 100 | void ScheduleUpdate(); |
| 101 | void UpdateTargets(); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 102 | |
Dmitry Gozman | 9e85208c7 | 2018-03-21 00:47:43 | [diff] [blame] | 103 | Profile* profile_; |
Dmitry Gozman | 72b5bd9 | 2017-11-21 00:03:18 | [diff] [blame] | 104 | std::unique_ptr<CancelableTimer> timer_; |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 105 | base::WeakPtrFactory<LocalTargetsUIHandler> weak_factory_{this}; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 106 | }; |
| 107 | |
Dmitry Gozman | 9e85208c7 | 2018-03-21 00:47:43 | [diff] [blame] | 108 | LocalTargetsUIHandler::LocalTargetsUIHandler(const Callback& callback, |
| 109 | Profile* profile) |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 110 | : DevToolsTargetsUIHandler(kTargetSourceLocal, callback), |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 111 | profile_(profile) { |
Dmitry Gozman | 72b5bd9 | 2017-11-21 00:03:18 | [diff] [blame] | 112 | DevToolsAgentHost::AddObserver(this); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 113 | UpdateTargets(); |
| 114 | } |
| 115 | |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 116 | LocalTargetsUIHandler::~LocalTargetsUIHandler() { |
Dmitry Gozman | 72b5bd9 | 2017-11-21 00:03:18 | [diff] [blame] | 117 | DevToolsAgentHost::RemoveObserver(this); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 118 | } |
| 119 | |
Dmitry Gozman | 72b5bd9 | 2017-11-21 00:03:18 | [diff] [blame] | 120 | bool LocalTargetsUIHandler::ShouldForceDevToolsAgentHostCreation() { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | void LocalTargetsUIHandler::DevToolsAgentHostCreated(DevToolsAgentHost*) { |
| 125 | ScheduleUpdate(); |
| 126 | } |
| 127 | |
| 128 | void LocalTargetsUIHandler::DevToolsAgentHostDestroyed(DevToolsAgentHost*) { |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 129 | ScheduleUpdate(); |
| 130 | } |
| 131 | |
dgozman | ffe70eca | 2014-09-18 14:28:11 | [diff] [blame] | 132 | void LocalTargetsUIHandler::ForceUpdate() { |
| 133 | ScheduleUpdate(); |
| 134 | } |
| 135 | |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 136 | void LocalTargetsUIHandler::ScheduleUpdate() { |
[email protected] | e90c5f9b | 2013-12-11 12:49:44 | [diff] [blame] | 137 | const int kUpdateDelay = 100; |
| 138 | timer_.reset( |
| 139 | new CancelableTimer( |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 140 | base::Bind(&LocalTargetsUIHandler::UpdateTargets, |
[email protected] | e90c5f9b | 2013-12-11 12:49:44 | [diff] [blame] | 141 | base::Unretained(this)), |
| 142 | base::TimeDelta::FromMilliseconds(kUpdateDelay))); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 143 | } |
| 144 | |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 145 | void LocalTargetsUIHandler::UpdateTargets() { |
Dmitry Gozman | 9e85208c7 | 2018-03-21 00:47:43 | [diff] [blame] | 146 | content::DevToolsAgentHost::List targets = |
| 147 | DevToolsAgentHost::GetOrCreateAll(); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 148 | |
vabr | 6681597 | 2017-04-20 08:27:06 | [diff] [blame] | 149 | std::vector<HostDescriptionNode> hosts; |
| 150 | hosts.reserve(targets.size()); |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 151 | targets_.clear(); |
vabr | 6681597 | 2017-04-20 08:27:06 | [diff] [blame] | 152 | for (const scoped_refptr<DevToolsAgentHost>& host : targets) { |
Dmitry Gozman | 9e85208c7 | 2018-03-21 00:47:43 | [diff] [blame] | 153 | if (Profile::FromBrowserContext(host->GetBrowserContext()) != profile_) |
| 154 | continue; |
Pavol Marko | 56df027 | 2018-07-04 17:55:04 | [diff] [blame] | 155 | if (!DevToolsWindow::AllowDevToolsFor(profile_, host->GetWebContents())) |
| 156 | continue; |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 157 | targets_[host->GetId()] = host; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 158 | hosts.push_back({host->GetId(), host->GetParentId(), |
| 159 | std::move(*Serialize(host.get()))}); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 160 | } |
| 161 | |
vabr | 6681597 | 2017-04-20 08:27:06 | [diff] [blame] | 162 | SendSerializedTargets( |
| 163 | SerializeHostDescriptions(std::move(hosts), kGuestList)); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 166 | // AdbTargetsUIHandler -------------------------------------------------------- |
| 167 | |
| 168 | class AdbTargetsUIHandler |
[email protected] | 6defd99c | 2014-04-30 17:22:17 | [diff] [blame] | 169 | : public DevToolsTargetsUIHandler, |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 170 | public DevToolsAndroidBridge::DeviceListListener { |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 171 | public: |
[email protected] | 8ff7a29 | 2014-07-08 23:20:53 | [diff] [blame] | 172 | AdbTargetsUIHandler(const Callback& callback, Profile* profile); |
dcheng | 03748a4 | 2014-10-21 10:19:13 | [diff] [blame] | 173 | ~AdbTargetsUIHandler() override; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 174 | |
vkuzkokov | d49c9e1 | 2015-03-26 16:38:38 | [diff] [blame] | 175 | void Open(const std::string& browser_id, const std::string& url) override; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 176 | |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 177 | scoped_refptr<DevToolsAgentHost> GetBrowserAgentHost( |
mostynb | 01d8b10e | 2014-10-13 09:44:44 | [diff] [blame] | 178 | const std::string& browser_id) override; |
[email protected] | f30ed78 | 2014-05-16 17:52:50 | [diff] [blame] | 179 | |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 180 | private: |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 181 | // DevToolsAndroidBridge::Listener overrides. |
dcheng | 03748a4 | 2014-10-21 10:19:13 | [diff] [blame] | 182 | void DeviceListChanged( |
mostynb | 01d8b10e | 2014-10-13 09:44:44 | [diff] [blame] | 183 | const DevToolsAndroidBridge::RemoteDevices& devices) override; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 184 | |
vkuzkokov | f16940a6 | 2014-10-14 17:49:05 | [diff] [blame] | 185 | DevToolsAndroidBridge* GetAndroidBridge(); |
| 186 | |
serya | 217cac5 | 2014-12-03 15:01:54 | [diff] [blame] | 187 | Profile* const profile_; |
| 188 | DevToolsAndroidBridge* const android_bridge_; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 189 | |
| 190 | typedef std::map<std::string, |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 191 | scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> > RemoteBrowsers; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 192 | RemoteBrowsers remote_browsers_; |
| 193 | }; |
| 194 | |
[email protected] | 8ff7a29 | 2014-07-08 23:20:53 | [diff] [blame] | 195 | AdbTargetsUIHandler::AdbTargetsUIHandler(const Callback& callback, |
| 196 | Profile* profile) |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 197 | : DevToolsTargetsUIHandler(kTargetSourceRemote, callback), |
vkuzkokov | f16940a6 | 2014-10-14 17:49:05 | [diff] [blame] | 198 | profile_(profile), |
| 199 | android_bridge_( |
| 200 | DevToolsAndroidBridge::Factory::GetForProfile(profile_)) { |
dgozman | 50ef6858 | 2015-11-25 03:10:59 | [diff] [blame] | 201 | if (android_bridge_) |
| 202 | android_bridge_->AddDeviceListListener(this); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | AdbTargetsUIHandler::~AdbTargetsUIHandler() { |
dgozman | 50ef6858 | 2015-11-25 03:10:59 | [diff] [blame] | 206 | if (android_bridge_) |
| 207 | android_bridge_->RemoveDeviceListListener(this); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 208 | } |
| 209 | |
vkuzkokov | d49c9e1 | 2015-03-26 16:38:38 | [diff] [blame] | 210 | void AdbTargetsUIHandler::Open(const std::string& browser_id, |
| 211 | const std::string& url) { |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 212 | auto it = remote_browsers_.find(browser_id); |
dgozman | 50ef6858 | 2015-11-25 03:10:59 | [diff] [blame] | 213 | if (it != remote_browsers_.end() && android_bridge_) |
vkuzkokov | 5c77d8d | 2015-03-31 11:23:58 | [diff] [blame] | 214 | android_bridge_->OpenRemotePage(it->second, url); |
[email protected] | 577d2e4ff | 2014-04-15 17:07:03 | [diff] [blame] | 215 | } |
| 216 | |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 217 | scoped_refptr<DevToolsAgentHost> |
[email protected] | f30ed78 | 2014-05-16 17:52:50 | [diff] [blame] | 218 | AdbTargetsUIHandler::GetBrowserAgentHost( |
| 219 | const std::string& browser_id) { |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 220 | auto it = remote_browsers_.find(browser_id); |
dgozman | 50ef6858 | 2015-11-25 03:10:59 | [diff] [blame] | 221 | if (it == remote_browsers_.end() || !android_bridge_) |
| 222 | return nullptr; |
vkuzkokov | f16940a6 | 2014-10-14 17:49:05 | [diff] [blame] | 223 | |
| 224 | return android_bridge_->GetBrowserAgentHost(it->second); |
[email protected] | f30ed78 | 2014-05-16 17:52:50 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | 3bdb22b | 2014-04-15 18:21:28 | [diff] [blame] | 227 | void AdbTargetsUIHandler::DeviceListChanged( |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 228 | const DevToolsAndroidBridge::RemoteDevices& devices) { |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 229 | remote_browsers_.clear(); |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 230 | targets_.clear(); |
dgozman | 50ef6858 | 2015-11-25 03:10:59 | [diff] [blame] | 231 | if (!android_bridge_) |
| 232 | return; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 233 | |
[email protected] | 8ff7a29 | 2014-07-08 23:20:53 | [diff] [blame] | 234 | base::ListValue device_list; |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 235 | for (auto dit = devices.begin(); dit != devices.end(); ++dit) { |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 236 | DevToolsAndroidBridge::RemoteDevice* device = dit->get(); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 237 | std::unique_ptr<base::DictionaryValue> device_data( |
| 238 | new base::DictionaryValue()); |
[email protected] | 4a00a3955e | 2014-04-07 11:03:27 | [diff] [blame] | 239 | device_data->SetString(kAdbModelField, device->model()); |
| 240 | device_data->SetString(kAdbSerialField, device->serial()); |
| 241 | device_data->SetBoolean(kAdbConnectedField, device->is_connected()); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 242 | std::string device_id = base::StringPrintf( |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 243 | kAdbDeviceIdFormat, |
[email protected] | 4a00a3955e | 2014-04-07 11:03:27 | [diff] [blame] | 244 | device->serial().c_str()); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 245 | device_data->SetString(kTargetIdField, device_id); |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 246 | auto browser_list = std::make_unique<base::ListValue>(); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 247 | |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 248 | DevToolsAndroidBridge::RemoteBrowsers& browsers = device->browsers(); |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 249 | for (auto bit = browsers.begin(); bit != browsers.end(); ++bit) { |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 250 | DevToolsAndroidBridge::RemoteBrowser* browser = bit->get(); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 251 | std::unique_ptr<base::DictionaryValue> browser_data( |
| 252 | new base::DictionaryValue()); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 253 | browser_data->SetString(kAdbBrowserNameField, browser->display_name()); |
vkuzkokov | 952b347 | 2015-01-15 15:55:26 | [diff] [blame] | 254 | browser_data->SetString(kAdbBrowserUserField, browser->user()); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 255 | browser_data->SetString(kAdbBrowserVersionField, browser->version()); |
[email protected] | 96fe858 | 2014-04-21 14:56:40 | [diff] [blame] | 256 | DevToolsAndroidBridge::RemoteBrowser::ParsedVersion parsed = |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 257 | browser->GetParsedVersion(); |
| 258 | browser_data->SetInteger( |
| 259 | kAdbBrowserChromeVersionField, |
| 260 | browser->IsChrome() && !parsed.empty() ? parsed[0] : 0); |
vkuzkokov | 46786fd1 | 2015-03-11 14:51:00 | [diff] [blame] | 261 | std::string browser_id = browser->GetId(); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 262 | browser_data->SetString(kTargetIdField, browser_id); |
| 263 | browser_data->SetString(kTargetSourceField, source_id()); |
[email protected] | daa7653 | 2014-03-17 11:32:23 | [diff] [blame] | 264 | |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 265 | auto page_list = std::make_unique<base::ListValue>(); |
[email protected] | 167206cf | 2014-02-17 13:16:40 | [diff] [blame] | 266 | remote_browsers_[browser_id] = browser; |
vkuzkokov | cf47aaf | 2014-10-17 14:11:32 | [diff] [blame] | 267 | for (const auto& page : browser->pages()) { |
pfeldman | e7d2e41 | 2016-09-23 16:41:51 | [diff] [blame] | 268 | scoped_refptr<DevToolsAgentHost> host = page->CreateTarget(); |
vabr | 6681597 | 2017-04-20 08:27:06 | [diff] [blame] | 269 | std::unique_ptr<base::DictionaryValue> target_data = |
| 270 | Serialize(host.get()); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 271 | // Pass the screen size in the target object to make sure that |
| 272 | // the caching logic does not prevent the target item from updating |
| 273 | // when the screen size changes. |
| 274 | gfx::Size screen_size = device->screen_size(); |
| 275 | target_data->SetInteger(kAdbScreenWidthField, screen_size.width()); |
| 276 | target_data->SetInteger(kAdbScreenHeightField, screen_size.height()); |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 277 | targets_[host->GetId()] = host; |
dcheng | 71bb845 | 2016-09-17 01:30:15 | [diff] [blame] | 278 | page_list->Append(std::move(target_data)); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 279 | } |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 280 | browser_data->Set(kAdbPagesList, std::move(page_list)); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 281 | browser_list->Append(std::move(browser_data)); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 282 | } |
| 283 | |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 284 | device_data->Set(kAdbBrowsersList, std::move(browser_list)); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 285 | device_list.Append(std::move(device_data)); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 286 | } |
[email protected] | 8ff7a29 | 2014-07-08 23:20:53 | [diff] [blame] | 287 | SendSerializedTargets(device_list); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | } // namespace |
| 291 | |
| 292 | // DevToolsTargetsUIHandler --------------------------------------------------- |
| 293 | |
| 294 | DevToolsTargetsUIHandler::DevToolsTargetsUIHandler( |
| 295 | const std::string& source_id, |
[email protected] | 8ff7a29 | 2014-07-08 23:20:53 | [diff] [blame] | 296 | const Callback& callback) |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 297 | : source_id_(source_id), |
| 298 | callback_(callback) { |
| 299 | } |
| 300 | |
| 301 | DevToolsTargetsUIHandler::~DevToolsTargetsUIHandler() { |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // static |
dcheng | b8eac3a | 2016-04-13 02:03:23 | [diff] [blame] | 305 | std::unique_ptr<DevToolsTargetsUIHandler> |
dgozman | bfb96fb | 2014-09-12 13:33:29 | [diff] [blame] | 306 | DevToolsTargetsUIHandler::CreateForLocal( |
Dmitry Gozman | 9e85208c7 | 2018-03-21 00:47:43 | [diff] [blame] | 307 | const DevToolsTargetsUIHandler::Callback& callback, |
| 308 | Profile* profile) { |
dcheng | b8eac3a | 2016-04-13 02:03:23 | [diff] [blame] | 309 | return std::unique_ptr<DevToolsTargetsUIHandler>( |
Dmitry Gozman | 9e85208c7 | 2018-03-21 00:47:43 | [diff] [blame] | 310 | new LocalTargetsUIHandler(callback, profile)); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | 6defd99c | 2014-04-30 17:22:17 | [diff] [blame] | 313 | // static |
dcheng | b8eac3a | 2016-04-13 02:03:23 | [diff] [blame] | 314 | std::unique_ptr<DevToolsTargetsUIHandler> |
[email protected] | 6defd99c | 2014-04-30 17:22:17 | [diff] [blame] | 315 | DevToolsTargetsUIHandler::CreateForAdb( |
dcheng | b8eac3a | 2016-04-13 02:03:23 | [diff] [blame] | 316 | const DevToolsTargetsUIHandler::Callback& callback, |
| 317 | Profile* profile) { |
| 318 | return std::unique_ptr<DevToolsTargetsUIHandler>( |
[email protected] | 6defd99c | 2014-04-30 17:22:17 | [diff] [blame] | 319 | new AdbTargetsUIHandler(callback, profile)); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 320 | } |
| 321 | |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 322 | scoped_refptr<DevToolsAgentHost> DevToolsTargetsUIHandler::GetTarget( |
[email protected] | 6defd99c | 2014-04-30 17:22:17 | [diff] [blame] | 323 | const std::string& target_id) { |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 324 | auto it = targets_.find(target_id); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 325 | if (it != targets_.end()) |
[email protected] | 6defd99c | 2014-04-30 17:22:17 | [diff] [blame] | 326 | return it->second; |
| 327 | return NULL; |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 6defd99c | 2014-04-30 17:22:17 | [diff] [blame] | 330 | void DevToolsTargetsUIHandler::Open(const std::string& browser_id, |
vkuzkokov | d49c9e1 | 2015-03-26 16:38:38 | [diff] [blame] | 331 | const std::string& url) { |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 332 | } |
| 333 | |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 334 | scoped_refptr<DevToolsAgentHost> |
[email protected] | f30ed78 | 2014-05-16 17:52:50 | [diff] [blame] | 335 | DevToolsTargetsUIHandler::GetBrowserAgentHost(const std::string& browser_id) { |
| 336 | return NULL; |
| 337 | } |
| 338 | |
dcheng | 71bb845 | 2016-09-17 01:30:15 | [diff] [blame] | 339 | std::unique_ptr<base::DictionaryValue> DevToolsTargetsUIHandler::Serialize( |
vabr | 6681597 | 2017-04-20 08:27:06 | [diff] [blame] | 340 | DevToolsAgentHost* host) { |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 341 | auto target_data = std::make_unique<base::DictionaryValue>(); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 342 | target_data->SetString(kTargetSourceField, source_id_); |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 343 | target_data->SetString(kTargetIdField, host->GetId()); |
| 344 | target_data->SetString(kTargetTypeField, host->GetType()); |
| 345 | target_data->SetBoolean(kAttachedField, host->IsAttached()); |
| 346 | target_data->SetString(kUrlField, host->GetURL().spec()); |
| 347 | target_data->SetString(kNameField, host->GetTitle()); |
| 348 | target_data->SetString(kFaviconUrlField, host->GetFaviconURL().spec()); |
| 349 | target_data->SetString(kDescriptionField, host->GetDescription()); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 350 | return target_data; |
| 351 | } |
| 352 | |
| 353 | void DevToolsTargetsUIHandler::SendSerializedTargets( |
[email protected] | 8ff7a29 | 2014-07-08 23:20:53 | [diff] [blame] | 354 | const base::ListValue& list) { |
| 355 | callback_.Run(source_id_, list); |
[email protected] | ad224623 | 2013-11-13 18:42:36 | [diff] [blame] | 356 | } |
| 357 | |
dgozman | ffe70eca | 2014-09-18 14:28:11 | [diff] [blame] | 358 | void DevToolsTargetsUIHandler::ForceUpdate() { |
| 359 | } |
| 360 | |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 361 | // PortForwardingStatusSerializer --------------------------------------------- |
| 362 | |
| 363 | PortForwardingStatusSerializer::PortForwardingStatusSerializer( |
| 364 | const Callback& callback, Profile* profile) |
| 365 | : callback_(callback), |
| 366 | profile_(profile) { |
vkuzkokov | 4e7c181 | 2014-09-03 16:00:00 | [diff] [blame] | 367 | DevToolsAndroidBridge* android_bridge = |
| 368 | DevToolsAndroidBridge::Factory::GetForProfile(profile_); |
| 369 | if (android_bridge) |
| 370 | android_bridge->AddPortForwardingListener(this); |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | PortForwardingStatusSerializer::~PortForwardingStatusSerializer() { |
vkuzkokov | 4e7c181 | 2014-09-03 16:00:00 | [diff] [blame] | 374 | DevToolsAndroidBridge* android_bridge = |
| 375 | DevToolsAndroidBridge::Factory::GetForProfile(profile_); |
| 376 | if (android_bridge) |
| 377 | android_bridge->RemovePortForwardingListener(this); |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void PortForwardingStatusSerializer::PortStatusChanged( |
vkuzkokov | 32c0c07 | 2014-10-01 14:10:38 | [diff] [blame] | 381 | const ForwardingStatus& status) { |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 382 | base::DictionaryValue result; |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 383 | for (auto sit = status.begin(); sit != status.end(); ++sit) { |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 384 | auto port_status_dict = std::make_unique<base::DictionaryValue>(); |
vkuzkokov | 32c0c07 | 2014-10-01 14:10:38 | [diff] [blame] | 385 | const PortStatusMap& port_status_map = sit->second; |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 386 | for (auto it = port_status_map.begin(); it != port_status_map.end(); ++it) { |
Raul Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 387 | port_status_dict->SetInteger(base::NumberToString(it->first), it->second); |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 388 | } |
| 389 | |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 390 | auto device_status_dict = std::make_unique<base::DictionaryValue>(); |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 391 | device_status_dict->Set(kPortForwardingPorts, std::move(port_status_dict)); |
vkuzkokov | 32c0c07 | 2014-10-01 14:10:38 | [diff] [blame] | 392 | device_status_dict->SetString(kPortForwardingBrowserId, |
vkuzkokov | 46786fd1 | 2015-03-11 14:51:00 | [diff] [blame] | 393 | sit->first->GetId()); |
vkuzkokov | 32c0c07 | 2014-10-01 14:10:38 | [diff] [blame] | 394 | |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 395 | std::string device_id = base::StringPrintf( |
| 396 | kAdbDeviceIdFormat, |
vkuzkokov | 32c0c07 | 2014-10-01 14:10:38 | [diff] [blame] | 397 | sit->first->serial().c_str()); |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 398 | result.Set(device_id, std::move(device_status_dict)); |
[email protected] | def2b68 | 2014-04-15 12:59:10 | [diff] [blame] | 399 | } |
| 400 | callback_.Run(result); |
| 401 | } |