Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 "base/json/json_reader.h" | |
| 6 #include "base/strings/stringprintf.h" | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h" | |
| 9 #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 | |
| 15 namespace local_discovery { | |
| 16 | |
| 17 namespace { | |
| 18 const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s"; | |
| 19 } | |
| 20 | |
| 21 PrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow( | |
| 22 net::URLRequestContextGetter* request_context, | |
| 23 OAuth2TokenService* token_service, | |
| 24 const GURL& automated_claim_url, | |
| 25 const ResponseCallback& callback) | |
| 26 : request_context_(request_context), | |
| 27 token_service_(token_service), | |
| 28 automated_claim_url_(automated_claim_url), | |
| 29 callback_(callback) { | |
| 30 } | |
| 31 | |
| 32 PrivetConfirmApiCallFlow::~PrivetConfirmApiCallFlow() { | |
| 33 } | |
| 34 | |
| 35 void PrivetConfirmApiCallFlow::Start() { | |
| 36 OAuth2TokenService::ScopeSet oauth_scopes; | |
| 37 oauth_scopes.insert(cloud_print::kCloudPrintAuth); | |
| 38 oauth_request_ = token_service_->StartRequest(oauth_scopes, this); | |
| 39 } | |
| 40 | |
| 41 void PrivetConfirmApiCallFlow::OnGetTokenSuccess( | |
| 42 const OAuth2TokenService::Request* request, | |
| 43 const std::string& access_token, | |
| 44 const base::Time& expiration_time) { | |
| 45 url_fetcher_.reset(net::URLFetcher::Create(automated_claim_url_, | |
| 46 net::URLFetcher::GET, | |
| 47 this)); | |
| 48 url_fetcher_->SetRequestContext(request_context_.get()); | |
| 49 std::string authorization_header = | |
| 50 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str()); | |
| 51 | |
| 52 url_fetcher_->AddExtraRequestHeader( | |
| 53 cloud_print::kChromeCloudPrintProxyHeader); | |
| 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 PrivetConfirmApiCallFlow::OnGetTokenFailure( | |
| 61 const OAuth2TokenService::Request* request, | |
| 62 const GoogleServiceAuthError& error) { | |
| 63 callback_.Run(ERROR_TOKEN); | |
| 64 } | |
| 65 | |
| 66 void PrivetConfirmApiCallFlow::OnURLFetchComplete( | |
| 67 const net::URLFetcher* source) { | |
| 68 // TODO(noamsml): Error logging. | |
| 69 | |
| 70 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into | |
| 71 // one helper method. | |
| 72 std::string response_str; | |
| 73 | |
| 74 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || | |
| 75 !source->GetResponseAsString(&response_str)) { | |
| 76 callback_.Run(ERROR_NETWORK); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 if (source->GetResponseCode() != net::HTTP_OK) { | |
| 81 callback_.Run(ERROR_HTTP_CODE); | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 base::JSONReader reader; | |
| 86 scoped_ptr<const base::Value> value(reader.Read(response_str)); | |
| 87 const base::DictionaryValue* dictionary_value; | |
| 88 bool success; | |
|
Vitaly Buka (NO REVIEWS)
2013/07/22 23:21:45
unitialized var
Noam Samuel
2013/07/23 00:02:42
Done.
| |
| 89 | |
| 90 if (!value.get() || !value->GetAsDictionary(&dictionary_value) | |
| 91 || !dictionary_value->GetBoolean(cloud_print::kSuccessValue, &success)) { | |
| 92 callback_.Run(ERROR_MALFORMED_RESPONSE); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 if (success) { | |
| 97 callback_.Run(SUCCESS); | |
| 98 } else { | |
| 99 callback_.Run(ERROR_FROM_SERVER); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 } // namespace local_discovery | |
| OLD | NEW |