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

Side by Side Diff: chrome/browser/local_discovery/cloud_print_base_api_flow.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 "base/json/json_reader.h" 5 #include "base/json/json_reader.h"
6 #include "base/strings/string_number_conversions.h"
6 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
7 #include "base/values.h" 8 #include "base/values.h"
8 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h" 9 #include "chrome/browser/local_discovery/cloud_print_base_api_flow.h"
9 #include "chrome/common/cloud_print/cloud_print_constants.h" 10 #include "chrome/common/cloud_print/cloud_print_constants.h"
10 #include "google_apis/gaia/google_service_auth_error.h" 11 #include "google_apis/gaia/google_service_auth_error.h"
11 #include "net/base/load_flags.h" 12 #include "net/base/load_flags.h"
13 #include "net/base/url_util.h"
12 #include "net/http/http_status_code.h" 14 #include "net/http/http_status_code.h"
13 #include "net/url_request/url_request_status.h" 15 #include "net/url_request/url_request_status.h"
14 16
15 namespace local_discovery { 17 namespace local_discovery {
16 18
17 namespace { 19 namespace {
18 const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s"; 20 const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s";
19 const char kCookieURLFormat[] = "%s&xsrf=%s&user=%d"; 21 const char kXSRFURLParameterKey[] = "xsrf";
22 const char kUserURLParameterKey[] = "user";
20 } 23 }
21 24
22 PrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow( 25 CloudPrintBaseApiFlow::CloudPrintBaseApiFlow(
23 net::URLRequestContextGetter* request_context, 26 net::URLRequestContextGetter* request_context,
24 OAuth2TokenService* token_service, 27 OAuth2TokenService* token_service,
25 const GURL& automated_claim_url, 28 const GURL& automated_claim_url,
26 const ResponseCallback& callback) 29 Delegate* delegate)
27 : request_context_(request_context), 30 : request_context_(request_context),
28 token_service_(token_service), 31 token_service_(token_service),
29 automated_claim_url_(automated_claim_url), 32 user_index_(kAccountIndexUseOAuth2),
30 callback_(callback) { 33 url_(automated_claim_url),
34 delegate_(delegate) {
31 } 35 }
32 36
33 PrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow( 37 CloudPrintBaseApiFlow::CloudPrintBaseApiFlow(
34 net::URLRequestContextGetter* request_context, 38 net::URLRequestContextGetter* request_context,
35 int user_index, 39 int user_index,
36 const std::string& xsrf_token, 40 const std::string& xsrf_token,
37 const GURL& automated_claim_url, 41 const GURL& automated_claim_url,
38 const ResponseCallback& callback) 42 Delegate* delegate)
39 : request_context_(request_context), 43 : request_context_(request_context),
40 token_service_(NULL), 44 token_service_(NULL),
41 user_index_(user_index), 45 user_index_(user_index),
42 xsrf_token_(xsrf_token), 46 xsrf_token_(xsrf_token),
43 automated_claim_url_(automated_claim_url), 47 url_(automated_claim_url),
44 callback_(callback) { 48 delegate_(delegate) {
45 } 49 }
46 50
47 PrivetConfirmApiCallFlow::~PrivetConfirmApiCallFlow() { 51 CloudPrintBaseApiFlow::CloudPrintBaseApiFlow(
52 net::URLRequestContextGetter* request_context,
53 int user_index,
54 const GURL& automated_claim_url,
55 Delegate* delegate)
56 : request_context_(request_context),
57 token_service_(NULL),
58 user_index_(user_index),
59 url_(automated_claim_url),
60 delegate_(delegate) {
48 } 61 }
49 62
50 void PrivetConfirmApiCallFlow::Start() { 63 CloudPrintBaseApiFlow::~CloudPrintBaseApiFlow() {
64 }
65
66 void CloudPrintBaseApiFlow::Start() {
51 if (UseOAuth2()) { 67 if (UseOAuth2()) {
52 OAuth2TokenService::ScopeSet oauth_scopes; 68 OAuth2TokenService::ScopeSet oauth_scopes;
53 oauth_scopes.insert(cloud_print::kCloudPrintAuth); 69 oauth_scopes.insert(cloud_print::kCloudPrintAuth);
54 oauth_request_ = token_service_->StartRequest(oauth_scopes, this); 70 oauth_request_ = token_service_->StartRequest(oauth_scopes, this);
55 } else { 71 } else {
56 GURL cookie_url( 72 GURL cookie_url = url_;
57 base::StringPrintf(kCookieURLFormat, 73
58 automated_claim_url_.spec().c_str(), 74 if (!xsrf_token_.empty()) {
59 xsrf_token_.c_str(), 75 cookie_url = net::AppendQueryParameter(cookie_url,
60 user_index_)); 76 kXSRFURLParameterKey,
77 xsrf_token_);
78 }
79
80 cookie_url = net::AppendQueryParameter(cookie_url,
81 kUserURLParameterKey,
82 base::IntToString(user_index_));
61 83
62 CreateRequest(cookie_url); 84 CreateRequest(cookie_url);
63 85
64 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); 86 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
65 87
66 url_fetcher_->Start(); 88 url_fetcher_->Start();
67 } 89 }
68 } 90 }
69 91
70 void PrivetConfirmApiCallFlow::OnGetTokenSuccess( 92 void CloudPrintBaseApiFlow::OnGetTokenSuccess(
71 const OAuth2TokenService::Request* request, 93 const OAuth2TokenService::Request* request,
72 const std::string& access_token, 94 const std::string& access_token,
73 const base::Time& expiration_time) { 95 const base::Time& expiration_time) {
74 CreateRequest(automated_claim_url_); 96 CreateRequest(url_);
75 97
76 std::string authorization_header = 98 std::string authorization_header =
77 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str()); 99 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str());
78 100
79 url_fetcher_->AddExtraRequestHeader(authorization_header); 101 url_fetcher_->AddExtraRequestHeader(authorization_header);
80 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | 102 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
81 net::LOAD_DO_NOT_SEND_COOKIES); 103 net::LOAD_DO_NOT_SEND_COOKIES);
82 url_fetcher_->Start(); 104 url_fetcher_->Start();
83 } 105 }
84 106
85 void PrivetConfirmApiCallFlow::OnGetTokenFailure( 107 void CloudPrintBaseApiFlow::OnGetTokenFailure(
86 const OAuth2TokenService::Request* request, 108 const OAuth2TokenService::Request* request,
87 const GoogleServiceAuthError& error) { 109 const GoogleServiceAuthError& error) {
88 callback_.Run(ERROR_TOKEN); 110 delegate_->OnCloudPrintAPIFlowError(this, ERROR_TOKEN);
89 } 111 }
90 112
91 void PrivetConfirmApiCallFlow::CreateRequest(const GURL& url) { 113 void CloudPrintBaseApiFlow::CreateRequest(const GURL& url) {
92 url_fetcher_.reset(net::URLFetcher::Create(url, 114 url_fetcher_.reset(net::URLFetcher::Create(url,
93 net::URLFetcher::GET, 115 net::URLFetcher::GET,
94 this)); 116 this));
95 117
96 url_fetcher_->SetRequestContext(request_context_.get()); 118 url_fetcher_->SetRequestContext(request_context_.get());
97 119
98 url_fetcher_->AddExtraRequestHeader( 120 url_fetcher_->AddExtraRequestHeader(
99 cloud_print::kChromeCloudPrintProxyHeader); 121 cloud_print::kChromeCloudPrintProxyHeader);
100 } 122 }
101 123
102 void PrivetConfirmApiCallFlow::OnURLFetchComplete( 124 void CloudPrintBaseApiFlow::OnURLFetchComplete(
103 const net::URLFetcher* source) { 125 const net::URLFetcher* source) {
104 // TODO(noamsml): Error logging. 126 // TODO(noamsml): Error logging.
105 127
106 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into 128 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into
107 // one helper method. 129 // one helper method.
108 std::string response_str; 130 std::string response_str;
109 131
110 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || 132 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS ||
111 !source->GetResponseAsString(&response_str)) { 133 !source->GetResponseAsString(&response_str)) {
112 callback_.Run(ERROR_NETWORK); 134 delegate_->OnCloudPrintAPIFlowError(this, ERROR_NETWORK);
113 return; 135 return;
114 } 136 }
115 137
116 if (source->GetResponseCode() != net::HTTP_OK) { 138 if (source->GetResponseCode() != net::HTTP_OK) {
117 callback_.Run(ERROR_HTTP_CODE); 139 delegate_->OnCloudPrintAPIFlowError(this, ERROR_HTTP_CODE);
118 return; 140 return;
119 } 141 }
120 142
121 base::JSONReader reader; 143 base::JSONReader reader;
122 scoped_ptr<const base::Value> value(reader.Read(response_str)); 144 scoped_ptr<const base::Value> value(reader.Read(response_str));
123 const base::DictionaryValue* dictionary_value; 145 const base::DictionaryValue* dictionary_value = NULL;
124 bool success = false;
125 146
126 if (!value.get() || !value->GetAsDictionary(&dictionary_value) 147 if (!value || !value->GetAsDictionary(&dictionary_value)) {
127 || !dictionary_value->GetBoolean(cloud_print::kSuccessValue, &success)) { 148 delegate_->OnCloudPrintAPIFlowError(this, ERROR_MALFORMED_RESPONSE);
128 callback_.Run(ERROR_MALFORMED_RESPONSE);
129 return; 149 return;
130 } 150 }
131 151
132 if (success) { 152 delegate_->OnCloudPrintAPIFlowComplete(this, dictionary_value);
133 callback_.Run(SUCCESS);
134 } else {
135 callback_.Run(ERROR_FROM_SERVER);
136 }
137 } 153 }
138 154
139 } // namespace local_discovery 155 } // namespace local_discovery
OLDNEW
« no previous file with comments | « chrome/browser/local_discovery/cloud_print_base_api_flow.h ('k') | chrome/browser/local_discovery/privet_confirm_api_flow.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698