Chromium Code Reviews| 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/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 | |
| 12 namespace local_discovery { | |
| 13 | |
| 14 CloudDeviceList::CloudDeviceList(net::URLRequestContextGetter* request_context, | |
| 15 OAuth2TokenService* token_service, | |
| 16 const std::string& account_id, | |
| 17 Delegate* delegate) | |
| 18 : request_context_(request_context), | |
| 19 delegate_(delegate), | |
| 20 api_flow_(request_context_, | |
| 21 token_service, | |
| 22 account_id, | |
| 23 cloud_devices::GetCloudDevicesRelativeURL("devices"), | |
| 24 this) { | |
| 25 } | |
| 26 | |
| 27 CloudDeviceList::~CloudDeviceList() { | |
| 28 } | |
| 29 | |
| 30 void CloudDeviceList::Start() { | |
| 31 api_flow_.Start(); | |
| 32 } | |
| 33 | |
| 34 const CloudDeviceList::DeviceDetails* CloudDeviceList::GetDetailsFor( | |
| 35 const std::string& id) { | |
| 36 for (iterator i = device_list_.begin(); i != device_list_.end(); ++i) { | |
| 37 if (i->id == id) | |
| 38 return &(*i); | |
| 39 } | |
| 40 return NULL; | |
| 41 } | |
| 42 | |
| 43 void CloudDeviceList::OnGCDAPIFlowError(GCDBaseApiFlow* flow, | |
| 44 GCDBaseApiFlow::Status status) { | |
| 45 delegate_->OnCloudDeviceListUnavailable(); | |
| 46 } | |
| 47 | |
| 48 void CloudDeviceList::OnGCDAPIFlowComplete(GCDBaseApiFlow* flow, | |
| 49 const base::DictionaryValue* value) { | |
| 50 const base::ListValue* devices; | |
| 51 | |
| 52 if (!value->GetList("devices", &devices)) { | |
| 53 delegate_->OnCloudDeviceListUnavailable(); | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 for (base::ListValue::const_iterator i = devices->begin(); | |
| 58 i != devices->end(); i++) { | |
| 59 base::DictionaryValue* device; | |
| 60 DeviceDetails details; | |
| 61 | |
| 62 if (!(*i)->GetAsDictionary(&device)) | |
| 63 continue; | |
| 64 | |
| 65 if (!FillDeviceDetails(device, &details)) | |
| 66 continue; | |
| 67 | |
| 68 device_list_.push_back(details); | |
| 69 } | |
| 70 | |
| 71 delegate_->OnCloudDeviceListReady(); | |
| 72 } | |
| 73 | |
| 74 bool CloudDeviceList::GCDIsCloudPrint() { | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 bool CloudDeviceList::FillDeviceDetails( | |
| 79 const base::DictionaryValue* device_value, | |
| 80 DeviceDetails* details) { | |
| 81 if (!device_value->GetString("id", &details->id)) | |
| 82 return false; | |
| 83 | |
| 84 if (!device_value->GetString("displayName", &details->display_name) && | |
| 85 !device_value->GetString("systemName", &details->display_name)) { | |
|
Noam Samuel
2014/05/16 21:13:26
Nit: Why are these two chained together while the
Vitaly Buka (NO REVIEWS)
2014/05/17 17:15:37
because it's assignment of the same filed display_
| |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 if (!device_value->GetString("deviceKind", &details->type)) | |
| 90 return false; | |
| 91 | |
| 92 // Non-essential. | |
| 93 device_value->GetString("description", &details->description); | |
| 94 | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 CloudDeviceList::DeviceDetails::DeviceDetails() { | |
| 99 } | |
| 100 | |
| 101 CloudDeviceList::DeviceDetails::~DeviceDetails() { | |
| 102 } | |
| 103 | |
| 104 } // namespace local_discovery | |
| OLD | NEW |