Index: cloud_print/gcp20/prototype/cloud_print_request.cc |
diff --git a/cloud_print/gcp20/prototype/cloud_print_request.cc b/cloud_print/gcp20/prototype/cloud_print_request.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..49d348f260e047efced965041cd0e8c53da76ed3 |
--- /dev/null |
+++ b/cloud_print/gcp20/prototype/cloud_print_request.cc |
@@ -0,0 +1,126 @@ |
+// Copyright 2013 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 "cloud_print/gcp20/prototype/cloud_print_request.h" |
+ |
+#include "base/bind.h" |
+#include "base/command_line.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/time/time.h" |
+#include "net/base/load_flags.h" |
+#include "net/http/http_status_code.h" |
+#include "net/url_request/url_request_context.h" |
+#include "net/url_request/url_request_context_getter.h" |
+ |
+CloudPrintRequest::CloudPrintRequest(Delegate* delegate): delegate_(delegate) { |
+} |
+ |
+CloudPrintRequest::~CloudPrintRequest() { |
+} |
+ |
+using net::URLFetcher; |
+using base::MessageLoop; |
+ |
+namespace { |
+ |
+const uint32 kDefaultTimeout = 20; // in seconds |
+ |
+} // namespace |
+ |
+scoped_ptr<CloudPrintRequest> CloudPrintRequest::CreateGet( |
+ const GURL& url, |
+ Delegate* delegate) { |
+ return CreateRequest(url, URLFetcher::GET, delegate); |
+} |
+ |
+scoped_ptr<CloudPrintRequest> CloudPrintRequest::CreatePost( |
+ const GURL& url, |
+ const DataForUpload& data, |
+ Delegate* delegate) { |
+ scoped_ptr<CloudPrintRequest> request = |
+ CreateRequest(url, URLFetcher::POST, delegate); |
+ request->fetcher_->SetUploadData(data.content_type, data.content); |
+ return request.Pass(); |
+} |
+ |
+void CloudPrintRequest::Run(const ParserCallback& parser) { |
+ DCHECK(!parser.is_null()); |
+ |
+ std::string access_token = delegate_->access_token(); |
+ if (!access_token.empty()) |
+ fetcher_->AddExtraRequestHeader(base::StringPrintf( |
+ "Authorization: Bearer \"%s\"", access_token.c_str())); |
+ |
+ parser_ = parser; |
+ fetcher_->SetRequestContext(delegate_->context_getter()); |
+ fetcher_->Start(); |
+ |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind(&CloudPrintRequest::OnRequestTimeout, AsWeakPtr()), |
+ base::TimeDelta::FromSeconds(kDefaultTimeout)); |
+} |
+ |
+void CloudPrintRequest::AddHeader(const std::string& header) { |
+ fetcher_->AddExtraRequestHeader(header); |
+} |
+ |
+void CloudPrintRequest::OnRequestTimeout() { |
+ if (!fetcher_) |
+ return; |
+ fetcher_.reset(); |
+ LOG(WARNING) << "Request timeout reached."; |
+ |
+ DCHECK(delegate_); |
+ delegate_->OnFetchTimeoutReached(); // After this object can be deleted. |
+ // Do *NOT* access members after this |
+ // call. |
+} |
+ |
+void CloudPrintRequest::OnURLFetchComplete(const URLFetcher* source) { |
+ DCHECK(source == fetcher_.get()); |
+ std::string response; |
+ source->GetResponseAsString(&response); |
+ |
+ int http_code = fetcher_->GetResponseCode(); |
+ fetcher_.reset(); |
+ VLOG(3) << response; |
+ |
+ DCHECK(delegate_); |
+ if (http_code == net::HTTP_OK) { |
+ base::Closure parser = base::Bind(parser_, response); |
+ delegate_->OnFetchComplete(); // After this object can be deleted. |
+ // Do *NOT* access members after this call. |
+ |
+ parser.Run(); |
+ } else { |
+ // TODO(maksymb): Add |server_http_code| and |server_api| info for errors. |
+ delegate_->OnFetchError("dummy", -1, http_code); // After this object can |
+ // be deleted. |
+ // Do *NOT* access members |
+ // after this call. |
+ |
+ NOTIMPLEMENTED() << "HTTP code: " << http_code; |
+ } |
+} |
+ |
+scoped_ptr<CloudPrintRequest> CloudPrintRequest::CreateRequest( |
+ const GURL& url, |
+ URLFetcher::RequestType method, |
+ Delegate* delegate) { |
+ scoped_ptr<CloudPrintRequest> request(new CloudPrintRequest(delegate)); |
+ |
+ request->fetcher_.reset(URLFetcher::Create(url, method, request.get())); |
gene
2013/07/23 09:36:23
nit: this is not very clean method to create an ob
maksymb
2013/07/23 19:02:37
Done.
|
+ |
+ int load_flags = request->fetcher_->GetLoadFlags(); |
+ load_flags |= net::LOAD_DO_NOT_SEND_COOKIES; |
+ load_flags |= net::LOAD_DO_NOT_SAVE_COOKIES; |
+ request->fetcher_->SetLoadFlags(load_flags); |
+ |
+ request->fetcher_->AddExtraRequestHeader("X-CloudPrint-Proxy: \"\""); |
+ |
+ return request.Pass(); |
+} |
+ |