Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / local_discovery / cloud_print_account_manager.cc
blob590bd86d129e4b488fff0675c58027e415e23203
1 // Copyright 2013 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_print_account_manager.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/values.h"
9 #include "chrome/common/cloud_print/cloud_print_constants.h"
10 #include "url/gurl.h"
12 namespace local_discovery {
14 namespace {
15 // URL relative to cloud print root
16 const char kCloudPrintRequestURLFormat[] = "%s/list?proxy=none";
17 const char kCloudPrintKeyUsers[] = "request.users";
18 const char kCloudPrintKeyXsrfToken[] = "xsrf_token";
19 } // namespace
21 CloudPrintAccountManager::CloudPrintAccountManager(
22 net::URLRequestContextGetter* request_context,
23 const std::string& cloud_print_url,
24 int token_user_index,
25 const AccountsCallback& callback)
26 : flow_(request_context,
27 token_user_index,
28 GURL(base::StringPrintf(kCloudPrintRequestURLFormat,
29 cloud_print_url.c_str())),
30 this),
31 callback_(callback) {
34 CloudPrintAccountManager::~CloudPrintAccountManager() {
37 void CloudPrintAccountManager::Start() {
38 flow_.Start();
41 // If an error occurs or the user is not logged in, return an empty user list to
42 // signify cookie-based accounts should not be used.
43 void CloudPrintAccountManager::ReportEmptyUserList() {
44 callback_.Run(std::vector<std::string>(), "");
47 void CloudPrintAccountManager::OnCloudPrintAPIFlowError(
48 CloudPrintBaseApiFlow* flow,
49 CloudPrintBaseApiFlow::Status status) {
50 ReportEmptyUserList();
53 void CloudPrintAccountManager::OnCloudPrintAPIFlowComplete(
54 CloudPrintBaseApiFlow* flow,
55 const base::DictionaryValue* value) {
56 bool success = false;
58 std::string xsrf_token;
59 const base::ListValue* users = NULL;
60 std::vector<std::string> users_vector;
62 if (!value->GetBoolean(cloud_print::kSuccessValue, &success) ||
63 !value->GetList(kCloudPrintKeyUsers, &users) ||
64 !value->GetString(kCloudPrintKeyXsrfToken, &xsrf_token) ||
65 !success) {
66 ReportEmptyUserList();
67 return;
70 for (size_t i = 0; i < users->GetSize(); i++) {
71 std::string user;
72 if (!users->GetString(i, &user)) {
73 // If we can't read a user from the list, send the users we do recognize
74 // and the XSRF token from the server.
75 break;
78 users_vector.push_back(user);
81 callback_.Run(users_vector, xsrf_token);
84 } // namespace local_discovery