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"
12 namespace local_discovery
{
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";
21 CloudPrintAccountManager::CloudPrintAccountManager(
22 net::URLRequestContextGetter
* request_context
,
23 const std::string
& cloud_print_url
,
25 const AccountsCallback
& callback
)
26 : flow_(request_context
,
28 GURL(base::StringPrintf(kCloudPrintRequestURLFormat
,
29 cloud_print_url
.c_str())),
34 CloudPrintAccountManager::~CloudPrintAccountManager() {
37 void CloudPrintAccountManager::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
) {
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
) ||
66 ReportEmptyUserList();
70 for (size_t i
= 0; i
< users
->GetSize(); i
++) {
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.
78 users_vector
.push_back(user
);
81 callback_
.Run(users_vector
, xsrf_token
);
84 } // namespace local_discovery