cloud_device_list.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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/local_discovery/cloud_device_list.h"
6
7#include <utility>
8
9#include "base/strings/stringprintf.h"
10#include "components/cloud_devices/common/cloud_devices_urls.h"
11
12namespace local_discovery {
13
14CloudDeviceList::CloudDeviceList(CloudDeviceListDelegate* delegate)
15    : delegate_(delegate) {
16}
17
18CloudDeviceList::~CloudDeviceList() {
19}
20
21void CloudDeviceList::OnGCDAPIFlowError(GCDApiFlow::Status status) {
22  delegate_->OnDeviceListUnavailable();
23}
24
25void CloudDeviceList::OnGCDAPIFlowComplete(const base::DictionaryValue& value) {
26  const base::ListValue* devices;
27
28  if (!value.GetList("devices", &devices)) {
29    delegate_->OnDeviceListUnavailable();
30    return;
31  }
32
33  std::vector<CloudDeviceListDelegate::Device> result;
34  for (base::ListValue::const_iterator i = devices->begin();
35       i != devices->end(); i++) {
36    base::DictionaryValue* device;
37    CloudDeviceListDelegate::Device details;
38
39    if (!(*i)->GetAsDictionary(&device))
40      continue;
41
42    if (!FillDeviceDetails(*device, &details))
43      continue;
44
45    result.push_back(details);
46  }
47
48  delegate_->OnDeviceListReady(result);
49}
50
51GURL CloudDeviceList::GetURL() {
52  return cloud_devices::GetCloudDevicesRelativeURL("devices");
53}
54
55bool CloudDeviceList::FillDeviceDetails(
56    const base::DictionaryValue& device_value,
57    CloudDeviceListDelegate::Device* details) {
58  if (!device_value.GetString("id", &details->id))
59    return false;
60
61  if (!device_value.GetString("displayName", &details->display_name) &&
62      !device_value.GetString("systemName", &details->display_name)) {
63    return false;
64  }
65
66  if (!device_value.GetString("deviceKind", &details->type))
67    return false;
68
69  // Non-essential.
70  device_value.GetString("description", &details->description);
71
72  return true;
73}
74
75}  // namespace local_discovery
76