Index: chrome/browser/local_discovery/cloud_device_list.cc |
diff --git a/chrome/browser/local_discovery/cloud_device_list.cc b/chrome/browser/local_discovery/cloud_device_list.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6cc4acc68350ea235dd7d8b04f2737113270101d |
--- /dev/null |
+++ b/chrome/browser/local_discovery/cloud_device_list.cc |
@@ -0,0 +1,104 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/local_discovery/cloud_device_list.h" |
+ |
+#include <utility> |
+ |
+#include "base/strings/stringprintf.h" |
+#include "components/cloud_devices/common/cloud_devices_urls.h" |
+ |
+namespace local_discovery { |
+ |
+CloudDeviceList::CloudDeviceList(net::URLRequestContextGetter* request_context, |
+ OAuth2TokenService* token_service, |
+ const std::string& account_id, |
+ Delegate* delegate) |
+ : request_context_(request_context), |
+ delegate_(delegate), |
+ api_flow_(request_context_, |
+ token_service, |
+ account_id, |
+ cloud_devices::GetCloudDevicesRelativeURL("devices"), |
+ this) { |
+} |
+ |
+CloudDeviceList::~CloudDeviceList() { |
+} |
+ |
+void CloudDeviceList::Start() { |
+ api_flow_.Start(); |
+} |
+ |
+const CloudDeviceList::DeviceDetails* CloudDeviceList::GetDetailsFor( |
+ const std::string& id) { |
+ for (iterator i = device_list_.begin(); i != device_list_.end(); ++i) { |
+ if (i->id == id) |
+ return &(*i); |
+ } |
+ return NULL; |
+} |
+ |
+void CloudDeviceList::OnGCDAPIFlowError(GCDBaseApiFlow* flow, |
+ GCDBaseApiFlow::Status status) { |
+ delegate_->OnCloudDeviceListUnavailable(); |
+} |
+ |
+void CloudDeviceList::OnGCDAPIFlowComplete(GCDBaseApiFlow* flow, |
+ const base::DictionaryValue* value) { |
+ const base::ListValue* devices; |
+ |
+ if (!value->GetList("devices", &devices)) { |
+ delegate_->OnCloudDeviceListUnavailable(); |
+ return; |
+ } |
+ |
+ for (base::ListValue::const_iterator i = devices->begin(); |
+ i != devices->end(); i++) { |
+ base::DictionaryValue* device; |
+ DeviceDetails details; |
+ |
+ if (!(*i)->GetAsDictionary(&device)) |
+ continue; |
+ |
+ if (!FillDeviceDetails(device, &details)) |
+ continue; |
+ |
+ device_list_.push_back(details); |
+ } |
+ |
+ delegate_->OnCloudDeviceListReady(); |
+} |
+ |
+bool CloudDeviceList::GCDIsCloudPrint() { |
+ return false; |
+} |
+ |
+bool CloudDeviceList::FillDeviceDetails( |
+ const base::DictionaryValue* device_value, |
+ DeviceDetails* details) { |
+ if (!device_value->GetString("id", &details->id)) |
+ return false; |
+ |
+ if (!device_value->GetString("displayName", &details->display_name) && |
+ !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_
|
+ return false; |
+ } |
+ |
+ if (!device_value->GetString("deviceKind", &details->type)) |
+ return false; |
+ |
+ // Non-essential. |
+ device_value->GetString("description", &details->description); |
+ |
+ return true; |
+} |
+ |
+CloudDeviceList::DeviceDetails::DeviceDetails() { |
+} |
+ |
+CloudDeviceList::DeviceDetails::~DeviceDetails() { |
+} |
+ |
+} // namespace local_discovery |