[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / local_discovery / gcd_base_api_flow.cc
blobf44c418fbde8a73540efaff73eab1a12f386b6e1
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.
5 #include "chrome/browser/local_discovery/gcd_base_api_flow.h"
7 #include "base/json/json_reader.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/values.h"
11 #include "chrome/browser/local_discovery/gcd_constants.h"
12 #include "chrome/common/cloud_print/cloud_print_constants.h"
13 #include "components/cloud_devices/common/cloud_devices_urls.h"
14 #include "google_apis/gaia/google_service_auth_error.h"
15 #include "net/base/load_flags.h"
16 #include "net/base/url_util.h"
17 #include "net/http/http_status_code.h"
18 #include "net/url_request/url_request_status.h"
20 namespace local_discovery {
22 namespace {
23 const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s";
26 net::URLFetcher::RequestType GCDBaseApiFlow::Delegate::GetRequestType() {
27 return net::URLFetcher::GET;
30 void GCDBaseApiFlow::Delegate::GetUploadData(std::string* upload_type,
31 std::string* upload_data) {
32 *upload_type = std::string();
33 *upload_data = std::string();
36 GCDBaseApiFlow::GCDBaseApiFlow(net::URLRequestContextGetter* request_context,
37 OAuth2TokenService* token_service,
38 const std::string& account_id,
39 const GURL& url,
40 Delegate* delegate)
41 : OAuth2TokenService::Consumer("cloud_print"),
42 request_context_(request_context),
43 token_service_(token_service),
44 account_id_(account_id),
45 url_(url),
46 delegate_(delegate) {
49 GCDBaseApiFlow::~GCDBaseApiFlow() {}
51 void GCDBaseApiFlow::Start() {
52 OAuth2TokenService::ScopeSet oauth_scopes;
53 if (delegate_->GCDIsCloudPrint()) {
54 oauth_scopes.insert(cloud_devices::kCloudPrintAuthScope);
55 } else {
56 oauth_scopes.insert(cloud_devices::kCloudDevicesAuthScope);
58 oauth_request_ =
59 token_service_->StartRequest(account_id_, oauth_scopes, this);
62 void GCDBaseApiFlow::OnGetTokenSuccess(
63 const OAuth2TokenService::Request* request,
64 const std::string& access_token,
65 const base::Time& expiration_time) {
66 CreateRequest(url_);
68 std::string authorization_header =
69 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str());
71 url_fetcher_->AddExtraRequestHeader(authorization_header);
72 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
73 net::LOAD_DO_NOT_SEND_COOKIES);
74 url_fetcher_->Start();
77 void GCDBaseApiFlow::OnGetTokenFailure(
78 const OAuth2TokenService::Request* request,
79 const GoogleServiceAuthError& error) {
80 delegate_->OnGCDAPIFlowError(this, ERROR_TOKEN);
83 void GCDBaseApiFlow::CreateRequest(const GURL& url) {
84 net::URLFetcher::RequestType request_type = delegate_->GetRequestType();
86 url_fetcher_.reset(net::URLFetcher::Create(url, request_type, this));
88 if (request_type != net::URLFetcher::GET) {
89 std::string upload_type;
90 std::string upload_data;
91 delegate_->GetUploadData(&upload_type, &upload_data);
92 url_fetcher_->SetUploadData(upload_type, upload_data);
95 url_fetcher_->SetRequestContext(request_context_.get());
97 if (delegate_->GCDIsCloudPrint()) {
98 url_fetcher_->AddExtraRequestHeader(
99 cloud_print::kChromeCloudPrintProxyHeader);
103 void GCDBaseApiFlow::OnURLFetchComplete(const net::URLFetcher* source) {
104 // TODO(noamsml): Error logging.
106 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into
107 // one helper method.
108 std::string response_str;
110 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS ||
111 !source->GetResponseAsString(&response_str)) {
112 delegate_->OnGCDAPIFlowError(this, ERROR_NETWORK);
113 return;
116 if (source->GetResponseCode() != net::HTTP_OK) {
117 delegate_->OnGCDAPIFlowError(this, ERROR_HTTP_CODE);
118 return;
121 base::JSONReader reader;
122 scoped_ptr<const base::Value> value(reader.Read(response_str));
123 const base::DictionaryValue* dictionary_value = NULL;
125 if (!value || !value->GetAsDictionary(&dictionary_value)) {
126 delegate_->OnGCDAPIFlowError(this, ERROR_MALFORMED_RESPONSE);
127 return;
130 delegate_->OnGCDAPIFlowComplete(this, dictionary_value);
133 } // namespace local_discovery