| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/device/webrtc/devtools_bridge_instances_reques
t.h" | |
| 6 | |
| 7 #include "base/values.h" | |
| 8 #include "components/cloud_devices/common/cloud_devices_urls.h" | |
| 9 #include "net/base/url_util.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 std::string GetKind(const base::DictionaryValue& value) { | |
| 14 std::string result; | |
| 15 value.GetString("kind", &result); | |
| 16 return result; | |
| 17 } | |
| 18 | |
| 19 bool HasCommand(const base::DictionaryValue& commands_defs_value, | |
| 20 const std::string& command_name) { | |
| 21 const base::DictionaryValue* command_value; | |
| 22 return commands_defs_value.GetDictionary(command_name, &command_value) && | |
| 23 GetKind(*command_value) == "clouddevices#commandDef"; | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 DevToolsBridgeInstancesRequest::Instance::~Instance() { | |
| 29 } | |
| 30 | |
| 31 DevToolsBridgeInstancesRequest::DevToolsBridgeInstancesRequest( | |
| 32 Delegate* delegate) | |
| 33 : delegate_(delegate) { | |
| 34 DCHECK(delegate_); | |
| 35 } | |
| 36 | |
| 37 DevToolsBridgeInstancesRequest::~DevToolsBridgeInstancesRequest() { | |
| 38 } | |
| 39 | |
| 40 void DevToolsBridgeInstancesRequest::OnGCDAPIFlowError( | |
| 41 local_discovery::GCDApiFlow::Status status) { | |
| 42 delegate_->OnDevToolsBridgeInstancesRequestFailed(); | |
| 43 } | |
| 44 | |
| 45 void DevToolsBridgeInstancesRequest::OnGCDAPIFlowComplete( | |
| 46 const base::DictionaryValue& value) { | |
| 47 const base::ListValue* device_list_value = NULL; | |
| 48 if (GetKind(value) == "clouddevices#devicesListResponse" && | |
| 49 value.GetList("devices", &device_list_value)) { | |
| 50 for (const auto& device_value : *device_list_value) { | |
| 51 const base::DictionaryValue* dictionary; | |
| 52 if (device_value->GetAsDictionary(&dictionary)) | |
| 53 TryAddInstance(*dictionary); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 delegate_->OnDevToolsBridgeInstancesRequestSucceeded(result_); | |
| 58 } | |
| 59 | |
| 60 GURL DevToolsBridgeInstancesRequest::GetURL() { | |
| 61 return cloud_devices::GetCloudDevicesRelativeURL("devices"); | |
| 62 } | |
| 63 | |
| 64 void DevToolsBridgeInstancesRequest::TryAddInstance( | |
| 65 const base::DictionaryValue& device_value) { | |
| 66 if (GetKind(device_value) != "clouddevices#device") | |
| 67 return; | |
| 68 | |
| 69 const base::DictionaryValue* commands_defs_value; | |
| 70 if (!device_value.GetDictionary("commandDefs", &commands_defs_value)) | |
| 71 return; | |
| 72 | |
| 73 if (!HasCommand(*commands_defs_value, "base._startSession") || | |
| 74 !HasCommand(*commands_defs_value, "base._iceExchange") || | |
| 75 !HasCommand(*commands_defs_value, "base._renegotiate")) { | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 Instance instance; | |
| 80 if (!device_value.GetString("id", &instance.id)) | |
| 81 return; | |
| 82 if (!device_value.GetString("displayName", &instance.display_name)) | |
| 83 return; | |
| 84 | |
| 85 result_.push_back(instance); | |
| 86 } | |
| OLD | NEW |