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 #ifndef CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_REQUEST_H_ 6 #define CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_REQUEST_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/callback.h" 12 #include "base/memory/weak_ptr.h" 13 #include "net/url_request/url_fetcher.h" 14 #include "net/url_request/url_fetcher_delegate.h" 15 16 // Request to CloudPrint with timeout control. 17 // Delegate should delete this object once it is deleting. 18 class CloudPrintRequest : public net::URLFetcherDelegate, 19 public base::SupportsWeakPtr<CloudPrintRequest> { 20 public: 21 class Delegate { 22 public: 23 Delegate() {} 24 virtual ~Delegate() {} 25 26 // Invoked when |fetcher_| finished fetching successfully. 27 // Use for erasing instance of CloudPrintRequest class. 28 virtual void OnFetchComplete(const std::string& response) = 0; 29 30 // Invoked when |fetcher_| finished fetching successfully. 31 // Use for erasing instance of CloudPrintRequest class. 32 virtual void OnFetchError(const std::string& server_api, 33 int server_code, 34 int server_http_code) = 0; 35 36 // Invoked when timeout is reached. 37 // Use for erasing instance of CloudPrintRequest class. 38 virtual void OnFetchTimeoutReached() = 0; 39 }; 40 41 virtual ~CloudPrintRequest(); 42 43 // Creates GET request. 44 static scoped_ptr<CloudPrintRequest> CreateGet(const GURL& url, 45 Delegate* delegate); 46 47 // Creates POST request. 48 static scoped_ptr<CloudPrintRequest> CreatePost(const GURL& url, 49 const std::string& content, 50 const std::string& mimetype, 51 Delegate* delegate); 52 53 // Starts request. Once fetch was completed, parser will be called. 54 void Run(const std::string& access_token, 55 scoped_refptr<net::URLRequestContextGetter> context_getter); 56 57 // Add header to request. 58 void AddHeader(const std::string& header); 59 60 private: 61 CloudPrintRequest(const GURL& url, 62 net::URLFetcher::RequestType method, 63 Delegate* delegate); 64 65 // net::URLFetcherDelegate methods: 66 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 67 68 // Method for handling timeout. 69 void OnRequestTimeout(); 70 71 scoped_ptr<net::URLFetcher> fetcher_; 72 73 Delegate* delegate_; 74 75 DISALLOW_COPY_AND_ASSIGN(CloudPrintRequest); 76 }; 77 78 #endif // CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_REQUEST_H_ 79 80