Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: chrome/browser/local_discovery/cloud_print_account_manager.cc

Issue 23530007: Move API flows for cloud print into common class CloudPrintBaseApiFlow (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: AppendQueryParameter Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/local_discovery/cloud_print_account_manager.h" 5 #include "chrome/browser/local_discovery/cloud_print_account_manager.h"
6 6
7 #include "base/json/json_reader.h"
8 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
9 #include "base/values.h" 8 #include "base/values.h"
10 #include "chrome/common/cloud_print/cloud_print_constants.h" 9 #include "chrome/common/cloud_print/cloud_print_constants.h"
11 #include "net/base/load_flags.h"
12 #include "net/http/http_status_code.h"
13 #include "net/url_request/url_request_context_getter.h"
14 #include "net/url_request/url_request_status.h"
15 #include "url/gurl.h" 10 #include "url/gurl.h"
16 11
17 namespace local_discovery { 12 namespace local_discovery {
18 13
19 namespace { 14 namespace {
20 // URL relative to cloud print root 15 // URL relative to cloud print root
21 const char kCloudPrintRequestURLFormat[] = "%s/list?proxy=none&user=%d"; 16 const char kCloudPrintRequestURLFormat[] = "%s/list?proxy=none";
22 const char kCloudPrintKeyUsers[] = "request.users"; 17 const char kCloudPrintKeyUsers[] = "request.users";
23 const char kCloudPrintKeyXsrfToken[] = "xsrf_token"; 18 const char kCloudPrintKeyXsrfToken[] = "xsrf_token";
24 } // namespace 19 } // namespace
25 20
26 CloudPrintAccountManager::CloudPrintAccountManager( 21 CloudPrintAccountManager::CloudPrintAccountManager(
27 net::URLRequestContextGetter* request_context, 22 net::URLRequestContextGetter* request_context,
28 const std::string& cloud_print_url, 23 const std::string& cloud_print_url,
29 int token_user_index, 24 int token_user_index,
30 const AccountsCallback& callback) 25 const AccountsCallback& callback)
31 : request_context_(request_context), cloud_print_url_(cloud_print_url), 26 : flow_(request_context,
32 token_user_index_(token_user_index), callback_(callback) { 27 token_user_index,
28 GURL(base::StringPrintf(kCloudPrintRequestURLFormat,
29 cloud_print_url.c_str())),
30 this),
31 callback_(callback) {
33 } 32 }
34 33
35 CloudPrintAccountManager::~CloudPrintAccountManager() { 34 CloudPrintAccountManager::~CloudPrintAccountManager() {
36 } 35 }
37 36
38 void CloudPrintAccountManager::Start() { 37 void CloudPrintAccountManager::Start() {
39 GURL url(base::StringPrintf(kCloudPrintRequestURLFormat, 38 flow_.Start();
40 cloud_print_url_.c_str(),
41 token_user_index_));
42 url_fetcher_.reset(net::URLFetcher::Create(url, net::URLFetcher::POST, this));
43 url_fetcher_->SetRequestContext(request_context_.get());
44 url_fetcher_->SetUploadData("", "");
45 url_fetcher_->AddExtraRequestHeader(
46 cloud_print::kChromeCloudPrintProxyHeader);
47 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
48 url_fetcher_->Start();
49 } 39 }
50 40
51 // If an error occurs or the user is not logged in, return an empty user list to 41 // If an error occurs or the user is not logged in, return an empty user list to
52 // signify cookie-based accounts should not be used. 42 // signify cookie-based accounts should not be used.
53 void CloudPrintAccountManager::ReportEmptyUserList() { 43 void CloudPrintAccountManager::ReportEmptyUserList() {
54 callback_.Run(std::vector<std::string>(), ""); 44 callback_.Run(std::vector<std::string>(), "");
55 } 45 }
56 46
57 void CloudPrintAccountManager::OnURLFetchComplete( 47 void CloudPrintAccountManager::OnCloudPrintAPIFlowError(
58 const net::URLFetcher* source) { 48 CloudPrintBaseApiFlow* flow,
59 std::string response_str; 49 CloudPrintBaseApiFlow::Status status) {
50 ReportEmptyUserList();
51 }
60 52
61 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || 53 void CloudPrintAccountManager::OnCloudPrintAPIFlowComplete(
62 source->GetResponseCode() != net::HTTP_OK || 54 CloudPrintBaseApiFlow* flow,
63 !source->GetResponseAsString(&response_str)) { 55 const base::DictionaryValue* value) {
64 ReportEmptyUserList();
65 return;
66 }
67
68 base::JSONReader reader;
69 scoped_ptr<const base::Value> value(reader.Read(response_str));
70 const base::DictionaryValue* dictionary_value;
71 bool success = false; 56 bool success = false;
72 57
73 std::string xsrf_token; 58 std::string xsrf_token;
74 const base::ListValue* users = NULL; 59 const base::ListValue* users = NULL;
75 std::vector<std::string> users_vector; 60 std::vector<std::string> users_vector;
76 61
77 if (!value.get() || 62 if (!value->GetBoolean(cloud_print::kSuccessValue, &success) ||
78 !value->GetAsDictionary(&dictionary_value) || 63 !value->GetList(kCloudPrintKeyUsers, &users) ||
79 !dictionary_value->GetBoolean(cloud_print::kSuccessValue, &success) || 64 !value->GetString(kCloudPrintKeyXsrfToken, &xsrf_token) ||
80 !dictionary_value->GetList(kCloudPrintKeyUsers, &users) ||
81 !dictionary_value->GetString(kCloudPrintKeyXsrfToken, &xsrf_token) ||
82 !success) { 65 !success) {
83 ReportEmptyUserList(); 66 ReportEmptyUserList();
84 return; 67 return;
85 } 68 }
86 69
87 for (size_t i = 0; i < users->GetSize(); i++) { 70 for (size_t i = 0; i < users->GetSize(); i++) {
88 std::string user; 71 std::string user;
89 if (!users->GetString(i, &user)) { 72 if (!users->GetString(i, &user)) {
90 // If we can't read a user from the list, send the users we do recognize 73 // If we can't read a user from the list, send the users we do recognize
91 // and the XSRF token from the server. 74 // and the XSRF token from the server.
92 break; 75 break;
93 } 76 }
94 77
95 users_vector.push_back(user); 78 users_vector.push_back(user);
96 } 79 }
97 80
98 callback_.Run(users_vector, xsrf_token); 81 callback_.Run(users_vector, xsrf_token);
99 } 82 }
100 83
101 } // namespace local_discovery 84 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698