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