Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Unified Diff: cloud_print/gcp20/prototype/cloud_print_request.cc

Issue 19866002: GCP2.0 Device: Receiving printjobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@confirmation
Patch Set: Unretained -> WeakPtr. Printjob -> PrintJob. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..798393d1d28a610371271b00ee178a976e2515aa
--- /dev/null
+++ b/cloud_print/gcp20/prototype/cloud_print_request.cc
@@ -0,0 +1,127 @@
+// 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();
+ if (CommandLine::ForCurrentProcess()->HasSwitch("print-responses"))
+ VLOG(1) << response;
Vitaly Buka (NO REVIEWS) 2013/07/22 23:46:02 remove if, you can control VLOG level
maksymb 2013/07/23 00:40:38 Done.
+
+ 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()));
+
+ 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();
+}
+

Powered by Google App Engine
This is Rietveld 408576698