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 "chrome/browser/local_discovery/gcd_constants.h" 11#include "components/cloud_devices/common/cloud_devices_urls.h" 12 13namespace local_discovery { 14 15namespace { 16const char kKindDevicesList[] = "clouddevices#devicesListResponse"; 17} 18 19CloudDeviceList::CloudDeviceList(CloudDeviceListDelegate* delegate) 20 : delegate_(delegate) { 21} 22 23CloudDeviceList::~CloudDeviceList() { 24} 25 26void CloudDeviceList::OnGCDAPIFlowError(GCDApiFlow::Status status) { 27 delegate_->OnDeviceListUnavailable(); 28} 29 30void CloudDeviceList::OnGCDAPIFlowComplete(const base::DictionaryValue& value) { 31 std::string kind; 32 value.GetString(kGCDKeyKind, &kind); 33 34 const base::ListValue* devices = NULL; 35 if (kind != kKindDevicesList || !value.GetList("devices", &devices)) { 36 delegate_->OnDeviceListUnavailable(); 37 return; 38 } 39 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; 45 46 if (!(*i)->GetAsDictionary(&device)) 47 continue; 48 49 if (!FillDeviceDetails(*device, &details)) 50 continue; 51 52 result.push_back(details); 53 } 54 55 delegate_->OnDeviceListReady(result); 56} 57 58GURL CloudDeviceList::GetURL() { 59 return cloud_devices::GetCloudDevicesRelativeURL("devices"); 60} 61 62bool CloudDeviceList::FillDeviceDetails( 63 const base::DictionaryValue& device_value, 64 CloudDeviceListDelegate::Device* details) { 65 if (!device_value.GetString("id", &details->id)) 66 return false; 67 68 if (!device_value.GetString("displayName", &details->display_name) && 69 !device_value.GetString("systemName", &details->display_name)) { 70 return false; 71 } 72 73 if (!device_value.GetString("deviceKind", &details->type)) 74 return false; 75 76 // Non-essential. 77 device_value.GetString("description", &details->description); 78 79 return true; 80} 81 82} // namespace local_discovery 83