1 // Copyright 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 "cloud_print/gcp20/prototype/cloud_print_request.h" 6 7 #include "base/bind.h" 8 #include "base/command_line.h" 9 #include "base/message_loop/message_loop.h" 10 #include "base/strings/stringprintf.h" 11 #include "base/time/time.h" 12 #include "net/base/load_flags.h" 13 #include "net/http/http_status_code.h" 14 #include "net/url_request/url_request_context.h" 15 #include "net/url_request/url_request_context_getter.h" 16 17 using net::URLFetcher; 18 using base::MessageLoop; 19 20 namespace { 21 22 const uint32 kDefaultTimeout = 20; // in seconds 23 24 } // namespace 25 26 CloudPrintRequest::CloudPrintRequest(const GURL& url, 27 URLFetcher::RequestType method, 28 Delegate* delegate) 29 : fetcher_(URLFetcher::Create(url, method, this)), 30 delegate_(delegate) { 31 int load_flags = fetcher_->GetLoadFlags(); 32 load_flags |= net::LOAD_DO_NOT_SEND_COOKIES; 33 load_flags |= net::LOAD_DO_NOT_SAVE_COOKIES; 34 fetcher_->SetLoadFlags(load_flags); 35 36 fetcher_->AddExtraRequestHeader("X-CloudPrint-Proxy: \"\""); 37 } 38 39 CloudPrintRequest::~CloudPrintRequest() { 40 } 41 42 scoped_ptr<CloudPrintRequest> CloudPrintRequest::CreateGet( 43 const GURL& url, 44 Delegate* delegate) { 45 return scoped_ptr<CloudPrintRequest>( 46 new CloudPrintRequest(url, URLFetcher::GET, delegate)); 47 } 48 49 scoped_ptr<CloudPrintRequest> CloudPrintRequest::CreatePost( 50 const GURL& url, 51 const std::string& content, 52 const std::string& mimetype, 53 Delegate* delegate) { 54 scoped_ptr<CloudPrintRequest> request( 55 new CloudPrintRequest(url, URLFetcher::POST, delegate)); 56 request->fetcher_->SetUploadData(mimetype, content); 57 return request.Pass(); 58 } 59 60 void CloudPrintRequest::Run( 61 const std::string& access_token, 62 scoped_refptr<net::URLRequestContextGetter> context_getter) { 63 64 if (!access_token.empty()) 65 fetcher_->AddExtraRequestHeader(base::StringPrintf( 66 "Authorization: Bearer \"%s\"", access_token.c_str())); 67 68 fetcher_->SetRequestContext(context_getter); 69 fetcher_->Start(); 70 71 MessageLoop::current()->PostDelayedTask( 72 FROM_HERE, 73 base::Bind(&CloudPrintRequest::OnRequestTimeout, AsWeakPtr()), 74 base::TimeDelta::FromSeconds(kDefaultTimeout)); 75 } 76 77 void CloudPrintRequest::AddHeader(const std::string& header) { 78 fetcher_->AddExtraRequestHeader(header); 79 } 80 81 void CloudPrintRequest::OnRequestTimeout() { 82 if (!fetcher_) 83 return; 84 fetcher_.reset(); 85 LOG(WARNING) << "Request timeout reached."; 86 87 DCHECK(delegate_); 88 delegate_->OnFetchTimeoutReached(); // After this object can be deleted. 89 // Do *NOT* access members after this 90 // call. 91 } 92 93 void CloudPrintRequest::OnURLFetchComplete(const URLFetcher* source) { 94 DCHECK(source == fetcher_.get()); 95 std::string response; 96 source->GetResponseAsString(&response); 97 98 int http_code = fetcher_->GetResponseCode(); 99 fetcher_.reset(); 100 VLOG(3) << response; 101 102 DCHECK(delegate_); 103 if (http_code == net::HTTP_OK) { 104 delegate_->OnFetchComplete(response); // After this object can be deleted. 105 // Do *NOT* access members after 106 // this call. 107 108 } else { 109 // TODO(maksymb): Add |server_http_code| and |server_api| info for errors. 110 delegate_->OnFetchError("dummy", -1, http_code); // After this object can 111 // be deleted. 112 // Do *NOT* access members 113 // after this call. 114 115 NOTIMPLEMENTED() << "HTTP code: " << http_code; 116 } 117 } 118 119