http_post.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
1// Copyright 2014 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 COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ 6#define COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ 7 8#include <string> 9 10#include "base/callback.h" 11#include "base/macros.h" 12#include "base/memory/scoped_ptr.h" 13#include "net/url_request/url_fetcher_delegate.h" 14 15namespace google { 16namespace protobuf { 17class MessageLite; 18} 19} 20 21namespace net { 22class URLRequestContextGetter; 23} 24 25namespace copresence { 26 27// This class handles all Apiary calls to the Copresence server. 28// It configures the HTTP request appropriately and reports any errors. 29// If deleted, the HTTP request is cancelled. 30// 31// TODO(ckehoe): Add retry logic. 32class HttpPost : public net::URLFetcherDelegate { 33 public: 34 // Callback to receive the HTTP status code and body of the response 35 // (if any). A pointer to this HttpPost object is also passed along. 36 typedef base::Callback<void(int, const std::string&)> 37 ResponseCallback; 38 39 // Create a request to the Copresence server. 40 // |url_context_getter| is owned by the caller, 41 // and the context it provides must be available until the request completes. 42 HttpPost(net::URLRequestContextGetter* url_context_getter, 43 const std::string& server_host, 44 const std::string& rpc_name, 45 const std::string& tracing_token, 46 std::string api_key, // If blank, we overwrite with a default. 47 const google::protobuf::MessageLite& request_proto); 48 49 // HTTP requests are cancelled on delete. 50 virtual ~HttpPost(); 51 52 // Send an HttpPost request. 53 void Start(const ResponseCallback& response_callback); 54 55 private: 56 static const int kUrlFetcherId = 1; 57 static const char kApiKeyField[]; 58 static const char kTracingTokenField[]; 59 60 friend class HttpPostTest; 61 62 // Overridden from net::URLFetcherDelegate. 63 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 64 65 ResponseCallback response_callback_; 66 67 scoped_ptr<net::URLFetcher> url_fetcher_; 68 69 DISALLOW_COPY_AND_ASSIGN(HttpPost); 70}; 71 72} // namespace copresence 73 74#endif // COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ 75