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/local_discovery/cloud_device_list.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/local_discovery/gcd_constants.h"
11 #include "components/cloud_devices/common/cloud_devices_urls.h"
13 namespace local_discovery
{
16 const char kKindDevicesList
[] = "clouddevices#devicesListResponse";
19 CloudDeviceList::CloudDeviceList(CloudDeviceListDelegate
* delegate
)
20 : delegate_(delegate
) {
23 CloudDeviceList::~CloudDeviceList() {
26 void CloudDeviceList::OnGCDAPIFlowError(GCDApiFlow::Status status
) {
27 delegate_
->OnDeviceListUnavailable();
30 void CloudDeviceList::OnGCDAPIFlowComplete(const base::DictionaryValue
& value
) {
32 value
.GetString(kGCDKeyKind
, &kind
);
34 const base::ListValue
* devices
= NULL
;
35 if (kind
!= kKindDevicesList
|| !value
.GetList("devices", &devices
)) {
36 delegate_
->OnDeviceListUnavailable();
40 std::vector
<CloudDeviceListDelegate::Device
> result
;
41 for (base::ListValue::const_iterator i
= devices
->begin();
42 i
!= devices
->end(); i
++) {
43 base::DictionaryValue
* device
;
44 CloudDeviceListDelegate::Device details
;
46 if (!(*i
)->GetAsDictionary(&device
))
49 if (!FillDeviceDetails(*device
, &details
))
52 result
.push_back(details
);
55 delegate_
->OnDeviceListReady(result
);
58 GURL
CloudDeviceList::GetURL() {
59 return cloud_devices::GetCloudDevicesRelativeURL("devices");
62 bool CloudDeviceList::FillDeviceDetails(
63 const base::DictionaryValue
& device_value
,
64 CloudDeviceListDelegate::Device
* details
) {
65 if (!device_value
.GetString("id", &details
->id
))
68 if (!device_value
.GetString("displayName", &details
->display_name
) &&
69 !device_value
.GetString("systemName", &details
->display_name
)) {
73 if (!device_value
.GetString("deviceKind", &details
->type
))
77 device_value
.GetString("description", &details
->description
);
82 } // namespace local_discovery