summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/gcd_api_flow_impl.cc
diff options
context:
space:
mode:
authornoamsml@chromium.org <noamsml@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-06-10 21:40:25 +0000
committernoamsml@chromium.org <noamsml@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-06-10 21:40:25 +0000
commit25d227e36ecdd4941f43cbd1090a39414f9b816a (patch)
treeb85926ec05d8398a7300e9931de78d9e2efde838 /chrome/browser/local_discovery/gcd_api_flow_impl.cc
parentc917e2d630f2521cbc2812b781593377e1184e64 (diff)
downloadchromium_src-25d227e36ecdd4941f43cbd1090a39414f9b816a.zip
chromium_src-25d227e36ecdd4941f43cbd1090a39414f9b816a.tar.gz
chromium_src-25d227e36ecdd4941f43cbd1090a39414f9b816a.tar.bz2
Rename GCDApiFlowInterface to GCDApiFlow and GCDApiFlow to GCDApiFlowImpl
This should bring GCDApiFlow in line with other interface/class pairs in Chrome. BUG=372843 NOTRY=true Review URL: https://codereview.chromium.org/320383002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@276165 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/local_discovery/gcd_api_flow_impl.cc')
-rw-r--r--chrome/browser/local_discovery/gcd_api_flow_impl.cc112
1 files changed, 112 insertions, 0 deletions
diff --git a/chrome/browser/local_discovery/gcd_api_flow_impl.cc b/chrome/browser/local_discovery/gcd_api_flow_impl.cc
new file mode 100644
index 0000000..d4abd93
--- /dev/null
+++ b/chrome/browser/local_discovery/gcd_api_flow_impl.cc
@@ -0,0 +1,112 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/local_discovery/gcd_api_flow_impl.h"
+
+#include "base/json/json_reader.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/stringprintf.h"
+#include "base/values.h"
+#include "chrome/browser/local_discovery/gcd_constants.h"
+#include "chrome/common/cloud_print/cloud_print_constants.h"
+#include "components/cloud_devices/common/cloud_devices_urls.h"
+#include "google_apis/gaia/google_service_auth_error.h"
+#include "net/base/load_flags.h"
+#include "net/base/url_util.h"
+#include "net/http/http_status_code.h"
+#include "net/url_request/url_request_status.h"
+
+namespace local_discovery {
+
+GCDApiFlowImpl::GCDApiFlowImpl(net::URLRequestContextGetter* request_context,
+ OAuth2TokenService* token_service,
+ const std::string& account_id)
+ : OAuth2TokenService::Consumer("cloud_print"),
+ request_context_(request_context),
+ token_service_(token_service),
+ account_id_(account_id) {
+}
+
+GCDApiFlowImpl::~GCDApiFlowImpl() {
+}
+
+void GCDApiFlowImpl::Start(scoped_ptr<Request> request) {
+ request_ = request.Pass();
+ OAuth2TokenService::ScopeSet oauth_scopes;
+ oauth_scopes.insert(request_->GetOAuthScope());
+ oauth_request_ =
+ token_service_->StartRequest(account_id_, oauth_scopes, this);
+}
+
+void GCDApiFlowImpl::OnGetTokenSuccess(
+ const OAuth2TokenService::Request* request,
+ const std::string& access_token,
+ const base::Time& expiration_time) {
+ CreateRequest(request_->GetURL());
+
+ std::string authorization_header =
+ base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str());
+
+ url_fetcher_->AddExtraRequestHeader(authorization_header);
+ url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
+ net::LOAD_DO_NOT_SEND_COOKIES);
+ url_fetcher_->Start();
+}
+
+void GCDApiFlowImpl::OnGetTokenFailure(
+ const OAuth2TokenService::Request* request,
+ const GoogleServiceAuthError& error) {
+ request_->OnGCDAPIFlowError(ERROR_TOKEN);
+}
+
+void GCDApiFlowImpl::CreateRequest(const GURL& url) {
+ net::URLFetcher::RequestType request_type = request_->GetRequestType();
+
+ url_fetcher_.reset(net::URLFetcher::Create(url, request_type, this));
+
+ if (request_type != net::URLFetcher::GET) {
+ std::string upload_type;
+ std::string upload_data;
+ request_->GetUploadData(&upload_type, &upload_data);
+ url_fetcher_->SetUploadData(upload_type, upload_data);
+ }
+
+ url_fetcher_->SetRequestContext(request_context_.get());
+
+ std::vector<std::string> extra_headers = request_->GetExtraRequestHeaders();
+ for (size_t i = 0; i < extra_headers.size(); ++i)
+ url_fetcher_->AddExtraRequestHeader(extra_headers[i]);
+}
+
+void GCDApiFlowImpl::OnURLFetchComplete(const net::URLFetcher* source) {
+ // TODO(noamsml): Error logging.
+
+ // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into
+ // one helper method.
+ std::string response_str;
+
+ if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS ||
+ !source->GetResponseAsString(&response_str)) {
+ request_->OnGCDAPIFlowError(ERROR_NETWORK);
+ return;
+ }
+
+ if (source->GetResponseCode() != net::HTTP_OK) {
+ request_->OnGCDAPIFlowError(ERROR_HTTP_CODE);
+ return;
+ }
+
+ base::JSONReader reader;
+ scoped_ptr<const base::Value> value(reader.Read(response_str));
+ const base::DictionaryValue* dictionary_value = NULL;
+
+ if (!value || !value->GetAsDictionary(&dictionary_value)) {
+ request_->OnGCDAPIFlowError(ERROR_MALFORMED_RESPONSE);
+ return;
+ }
+
+ request_->OnGCDAPIFlowComplete(*dictionary_value);
+}
+
+} // namespace local_discovery