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 if (!access_token.empty()) 64 fetcher_->AddExtraRequestHeader(base::StringPrintf( 65 "Authorization: Bearer \"%s\"", access_token.c_str())); 66 67 fetcher_->SetRequestContext(context_getter.get()); 68 fetcher_->Start(); 69 70 MessageLoop::current()->PostDelayedTask( 71 FROM_HERE, 72 base::Bind(&CloudPrintRequest::OnRequestTimeout, AsWeakPtr()), 73 base::TimeDelta::FromSeconds(kDefaultTimeout)); 74 } 75 76 void CloudPrintRequest::AddHeader(const std::string& header) { 77 fetcher_->AddExtraRequestHeader(header); 78 } 79 80 void CloudPrintRequest::OnRequestTimeout() { 81 if (!fetcher_) 82 return; 83 fetcher_.reset(); 84 LOG(WARNING) << "Request timeout reached."; 85 86 DCHECK(delegate_); 87 delegate_->OnFetchTimeoutReached(); // After this object can be deleted. 88 // Do *NOT* access members after this 89 // call. 90 } 91 92 void CloudPrintRequest::OnURLFetchComplete(const URLFetcher* source) { 93 DCHECK(source == fetcher_.get()); 94 std::string response; 95 source->GetResponseAsString(&response); 96 97 int http_code = fetcher_->GetResponseCode(); 98 fetcher_.reset(); 99 VLOG(3) << response; 100 101 DCHECK(delegate_); 102 if (http_code == net::HTTP_OK) { 103 delegate_->OnFetchComplete(response); // After this object can be deleted. 104 // Do *NOT* access members after 105 // this call. 106 107 } else { 108 // TODO(maksymb): Add Privet |server_http_code| and |server_api| support in 109 // case of server errors. 110 NOTIMPLEMENTED() << "HTTP code: " << http_code; 111 delegate_->OnFetchError("dummy", -1, http_code); // After this object can 112 // be deleted. 113 // Do *NOT* access members 114 // after this call. 115 } 116 } 117 118