OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/local_discovery/gcd_api_flow_impl.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <utility> | |
9 | |
10 #include "base/json/json_reader.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/local_discovery/gcd_constants.h" | |
15 #include "chrome/common/cloud_print/cloud_print_constants.h" | |
16 #include "components/cloud_devices/common/cloud_devices_urls.h" | |
17 #include "google_apis/gaia/google_service_auth_error.h" | |
18 #include "net/base/load_flags.h" | |
19 #include "net/base/url_util.h" | |
20 #include "net/http/http_status_code.h" | |
21 #include "net/url_request/url_request_status.h" | |
22 | |
23 namespace local_discovery { | |
24 | |
25 GCDApiFlowImpl::GCDApiFlowImpl(net::URLRequestContextGetter* request_context, | |
26 OAuth2TokenService* token_service, | |
27 const std::string& account_id) | |
28 : OAuth2TokenService::Consumer("cloud_print"), | |
29 request_context_(request_context), | |
30 token_service_(token_service), | |
31 account_id_(account_id) { | |
32 } | |
33 | |
34 GCDApiFlowImpl::~GCDApiFlowImpl() { | |
35 } | |
36 | |
37 void GCDApiFlowImpl::Start(scoped_ptr<Request> request) { | |
38 request_ = std::move(request); | |
39 OAuth2TokenService::ScopeSet oauth_scopes; | |
40 oauth_scopes.insert(request_->GetOAuthScope()); | |
41 oauth_request_ = | |
42 token_service_->StartRequest(account_id_, oauth_scopes, this); | |
43 } | |
44 | |
45 void GCDApiFlowImpl::OnGetTokenSuccess( | |
46 const OAuth2TokenService::Request* request, | |
47 const std::string& access_token, | |
48 const base::Time& expiration_time) { | |
49 CreateRequest(request_->GetURL()); | |
50 | |
51 std::string authorization_header = | |
52 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str()); | |
53 | |
54 url_fetcher_->AddExtraRequestHeader(authorization_header); | |
55 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | |
56 net::LOAD_DO_NOT_SEND_COOKIES); | |
57 url_fetcher_->Start(); | |
58 } | |
59 | |
60 void GCDApiFlowImpl::OnGetTokenFailure( | |
61 const OAuth2TokenService::Request* request, | |
62 const GoogleServiceAuthError& error) { | |
63 request_->OnGCDAPIFlowError(ERROR_TOKEN); | |
64 } | |
65 | |
66 void GCDApiFlowImpl::CreateRequest(const GURL& url) { | |
67 net::URLFetcher::RequestType request_type = request_->GetRequestType(); | |
68 | |
69 url_fetcher_ = net::URLFetcher::Create(url, request_type, this); | |
70 | |
71 if (request_type != net::URLFetcher::GET) { | |
72 std::string upload_type; | |
73 std::string upload_data; | |
74 request_->GetUploadData(&upload_type, &upload_data); | |
75 url_fetcher_->SetUploadData(upload_type, upload_data); | |
76 } | |
77 | |
78 url_fetcher_->SetRequestContext(request_context_.get()); | |
79 | |
80 std::vector<std::string> extra_headers = request_->GetExtraRequestHeaders(); | |
81 for (size_t i = 0; i < extra_headers.size(); ++i) | |
82 url_fetcher_->AddExtraRequestHeader(extra_headers[i]); | |
83 } | |
84 | |
85 void GCDApiFlowImpl::OnURLFetchComplete(const net::URLFetcher* source) { | |
86 // TODO(noamsml): Error logging. | |
87 | |
88 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into | |
89 // one helper method. | |
90 std::string response_str; | |
91 | |
92 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || | |
93 !source->GetResponseAsString(&response_str)) { | |
94 request_->OnGCDAPIFlowError(ERROR_NETWORK); | |
95 return; | |
96 } | |
97 | |
98 if (source->GetResponseCode() != net::HTTP_OK) { | |
99 request_->OnGCDAPIFlowError(ERROR_HTTP_CODE); | |
100 return; | |
101 } | |
102 | |
103 base::JSONReader reader; | |
104 scoped_ptr<const base::Value> value(reader.Read(response_str)); | |
105 const base::DictionaryValue* dictionary_value = NULL; | |
106 | |
107 if (!value || !value->GetAsDictionary(&dictionary_value)) { | |
108 request_->OnGCDAPIFlowError(ERROR_MALFORMED_RESPONSE); | |
109 return; | |
110 } | |
111 | |
112 request_->OnGCDAPIFlowComplete(*dictionary_value); | |
113 } | |
114 | |
115 } // namespace local_discovery | |
OLD | NEW |