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

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