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.
5 #include "chrome/browser/devtools/device/webrtc/devtools_bridge_instances_request.h"
7 #include "base/values.h"
8 #include "components/cloud_devices/common/cloud_devices_urls.h"
9 #include "net/base/url_util.h"
13 std::string
GetKind(const base::DictionaryValue
& value
) {
15 value
.GetString("kind", &result
);
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";
28 DevToolsBridgeInstancesRequest::Instance::~Instance() {
31 DevToolsBridgeInstancesRequest::DevToolsBridgeInstancesRequest(
33 : delegate_(delegate
) {
37 DevToolsBridgeInstancesRequest::~DevToolsBridgeInstancesRequest() {
40 void DevToolsBridgeInstancesRequest::OnGCDAPIFlowError(
41 local_discovery::GCDApiFlow::Status status
) {
42 delegate_
->OnDevToolsBridgeInstancesRequestFailed();
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
);
57 delegate_
->OnDevToolsBridgeInstancesRequestSucceeded(result_
);
60 GURL
DevToolsBridgeInstancesRequest::GetURL() {
61 return cloud_devices::GetCloudDevicesRelativeURL("devices");
64 void DevToolsBridgeInstancesRequest::TryAddInstance(
65 const base::DictionaryValue
& device_value
) {
66 if (GetKind(device_value
) != "clouddevices#device")
69 const base::DictionaryValue
* commands_defs_value
;
70 if (!device_value
.GetDictionary("commandDefs", &commands_defs_value
))
73 if (!HasCommand(*commands_defs_value
, "base._startSession") ||
74 !HasCommand(*commands_defs_value
, "base._iceExchange") ||
75 !HasCommand(*commands_defs_value
, "base._renegotiate")) {
80 if (!device_value
.GetString("id", &instance
.id
))
82 if (!device_value
.GetString("displayName", &instance
.display_name
))
85 result_
.push_back(instance
);