OLD | NEW |
---|---|
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&xsrf=%s&user=%d"; |
20 const char kCookieURLFormatNoXSRF[] = "%s&user=%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 if (xsrf_token_.empty()) { |
58 automated_claim_url_.spec().c_str(), | 71 cookie_url = GURL(base::StringPrintf(kCookieURLFormatNoXSRF, |
59 xsrf_token_.c_str(), | 72 url_.spec().c_str(), |
Vitaly Buka (NO REVIEWS)
2013/09/05 01:08:33
net::AppendQueryParameter
Noam Samuel
2013/09/05 23:22:27
Done.
| |
60 user_index_)); | 73 user_index_)); |
74 } else { | |
75 cookie_url = GURL(base::StringPrintf(kCookieURLFormat, | |
76 url_.spec().c_str(), | |
77 xsrf_token_.c_str(), | |
78 user_index_)); | |
79 } | |
61 | 80 |
62 CreateRequest(cookie_url); | 81 CreateRequest(cookie_url); |
63 | 82 |
64 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 83 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
65 | 84 |
66 url_fetcher_->Start(); | 85 url_fetcher_->Start(); |
67 } | 86 } |
68 } | 87 } |
69 | 88 |
70 void PrivetConfirmApiCallFlow::OnGetTokenSuccess( | 89 void CloudPrintBaseApiFlow::OnGetTokenSuccess( |
71 const OAuth2TokenService::Request* request, | 90 const OAuth2TokenService::Request* request, |
72 const std::string& access_token, | 91 const std::string& access_token, |
73 const base::Time& expiration_time) { | 92 const base::Time& expiration_time) { |
74 CreateRequest(automated_claim_url_); | 93 CreateRequest(url_); |
75 | 94 |
76 std::string authorization_header = | 95 std::string authorization_header = |
77 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str()); | 96 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str()); |
78 | 97 |
79 url_fetcher_->AddExtraRequestHeader(authorization_header); | 98 url_fetcher_->AddExtraRequestHeader(authorization_header); |
80 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 99 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
81 net::LOAD_DO_NOT_SEND_COOKIES); | 100 net::LOAD_DO_NOT_SEND_COOKIES); |
82 url_fetcher_->Start(); | 101 url_fetcher_->Start(); |
83 } | 102 } |
84 | 103 |
85 void PrivetConfirmApiCallFlow::OnGetTokenFailure( | 104 void CloudPrintBaseApiFlow::OnGetTokenFailure( |
86 const OAuth2TokenService::Request* request, | 105 const OAuth2TokenService::Request* request, |
87 const GoogleServiceAuthError& error) { | 106 const GoogleServiceAuthError& error) { |
88 callback_.Run(ERROR_TOKEN); | 107 delegate_->OnCloudPrintAPIFlowError(this, ERROR_TOKEN); |
89 } | 108 } |
90 | 109 |
91 void PrivetConfirmApiCallFlow::CreateRequest(const GURL& url) { | 110 void CloudPrintBaseApiFlow::CreateRequest(const GURL& url) { |
92 url_fetcher_.reset(net::URLFetcher::Create(url, | 111 url_fetcher_.reset(net::URLFetcher::Create(url, |
93 net::URLFetcher::GET, | 112 net::URLFetcher::GET, |
94 this)); | 113 this)); |
95 | 114 |
96 url_fetcher_->SetRequestContext(request_context_.get()); | 115 url_fetcher_->SetRequestContext(request_context_.get()); |
97 | 116 |
98 url_fetcher_->AddExtraRequestHeader( | 117 url_fetcher_->AddExtraRequestHeader( |
99 cloud_print::kChromeCloudPrintProxyHeader); | 118 cloud_print::kChromeCloudPrintProxyHeader); |
100 } | 119 } |
101 | 120 |
102 void PrivetConfirmApiCallFlow::OnURLFetchComplete( | 121 void CloudPrintBaseApiFlow::OnURLFetchComplete( |
103 const net::URLFetcher* source) { | 122 const net::URLFetcher* source) { |
104 // TODO(noamsml): Error logging. | 123 // TODO(noamsml): Error logging. |
105 | 124 |
106 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into | 125 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into |
107 // one helper method. | 126 // one helper method. |
108 std::string response_str; | 127 std::string response_str; |
109 | 128 |
110 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || | 129 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || |
111 !source->GetResponseAsString(&response_str)) { | 130 !source->GetResponseAsString(&response_str)) { |
112 callback_.Run(ERROR_NETWORK); | 131 delegate_->OnCloudPrintAPIFlowError(this, ERROR_NETWORK); |
113 return; | 132 return; |
114 } | 133 } |
115 | 134 |
116 if (source->GetResponseCode() != net::HTTP_OK) { | 135 if (source->GetResponseCode() != net::HTTP_OK) { |
117 callback_.Run(ERROR_HTTP_CODE); | 136 delegate_->OnCloudPrintAPIFlowError(this, ERROR_HTTP_CODE); |
118 return; | 137 return; |
119 } | 138 } |
120 | 139 |
121 base::JSONReader reader; | 140 base::JSONReader reader; |
122 scoped_ptr<const base::Value> value(reader.Read(response_str)); | 141 scoped_ptr<const base::Value> value(reader.Read(response_str)); |
123 const base::DictionaryValue* dictionary_value; | 142 const base::DictionaryValue* dictionary_value = NULL; |
124 bool success = false; | |
125 | 143 |
126 if (!value.get() || !value->GetAsDictionary(&dictionary_value) | 144 if (!value || !value->GetAsDictionary(&dictionary_value)) { |
127 || !dictionary_value->GetBoolean(cloud_print::kSuccessValue, &success)) { | 145 delegate_->OnCloudPrintAPIFlowError(this, ERROR_MALFORMED_RESPONSE); |
128 callback_.Run(ERROR_MALFORMED_RESPONSE); | |
129 return; | 146 return; |
130 } | 147 } |
131 | 148 |
132 if (success) { | 149 delegate_->OnCloudPrintAPIFlowComplete(this, dictionary_value); |
133 callback_.Run(SUCCESS); | |
134 } else { | |
135 callback_.Run(ERROR_FROM_SERVER); | |
136 } | |
137 } | 150 } |
138 | 151 |
139 } // namespace local_discovery | 152 } // namespace local_discovery |
OLD | NEW |