OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/local_discovery/cloud_print_account_manager.h" | |
6 | |
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" | |
11 | |
12 namespace local_discovery { | |
13 | |
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 | |
20 | |
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) { | |
32 } | |
33 | |
34 CloudPrintAccountManager::~CloudPrintAccountManager() { | |
35 } | |
36 | |
37 void CloudPrintAccountManager::Start() { | |
38 flow_.Start(); | |
39 } | |
40 | |
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>(), ""); | |
45 } | |
46 | |
47 void CloudPrintAccountManager::OnCloudPrintAPIFlowError( | |
48 CloudPrintBaseApiFlow* flow, | |
49 CloudPrintBaseApiFlow::Status status) { | |
50 ReportEmptyUserList(); | |
51 } | |
52 | |
53 void CloudPrintAccountManager::OnCloudPrintAPIFlowComplete( | |
54 CloudPrintBaseApiFlow* flow, | |
55 const base::DictionaryValue* value) { | |
56 bool success = false; | |
57 | |
58 std::string xsrf_token; | |
59 const base::ListValue* users = NULL; | |
60 std::vector<std::string> users_vector; | |
61 | |
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; | |
68 } | |
69 | |
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; | |
76 } | |
77 | |
78 users_vector.push_back(user); | |
79 } | |
80 | |
81 callback_.Run(users_vector, xsrf_token); | |
82 } | |
83 | |
84 } // namespace local_discovery | |
OLD | NEW |