| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ | 5 #ifndef COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ |
| 6 #define COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ | 6 #define COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
| 14 #include "url/gurl.h" |
| 14 | 15 |
| 15 namespace google { | 16 namespace google { |
| 16 namespace protobuf { | 17 namespace protobuf { |
| 17 class MessageLite; | 18 class MessageLite; |
| 18 } | 19 } |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 class URLRequestContextGetter; | 23 class URLRequestContextGetter; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace copresence { | 26 namespace copresence { |
| 26 | 27 |
| 27 // This class handles all Apiary calls to the Copresence server. | 28 // This class handles all Apiary calls to the Copresence server. |
| 28 // It configures the HTTP request appropriately and reports any errors. | 29 // It configures the HTTP request appropriately and reports any errors. |
| 29 // If deleted, the HTTP request is cancelled. | 30 // If deleted, the HTTP request is cancelled. |
| 30 // | 31 // |
| 31 // TODO(ckehoe): Add retry logic. | 32 // TODO(ckehoe): Add retry logic. |
| 32 class HttpPost : public net::URLFetcherDelegate { | 33 class HttpPost : public net::URLFetcherDelegate { |
| 33 public: | 34 public: |
| 34 // Callback to receive the HTTP status code and body of the response | 35 // 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 // (if any). A pointer to this HttpPost object is also passed along. |
| 36 typedef base::Callback<void(int, const std::string&)> | 37 typedef base::Callback<void(int, const std::string&)> |
| 37 ResponseCallback; | 38 ResponseCallback; |
| 38 | 39 |
| 39 // An id for testing url fetching. | 40 // Create a request to the Copresence server. |
| 40 static const int kUrlFetcherId = 1; | 41 // |url_context_getter| is owned by the caller, |
| 41 | 42 // and the context it provides must be available until the request completes. |
| 42 // The query parameter to send Apiary tracing tokens. | |
| 43 static const char kTracingTokenField[]; | |
| 44 | |
| 45 // Create a request to the Copresence server. |url_context_getter| | |
| 46 // is owned by the caller, and the context it provides must be available | |
| 47 // until the request completes. | |
| 48 HttpPost(net::URLRequestContextGetter* url_context_getter, | 43 HttpPost(net::URLRequestContextGetter* url_context_getter, |
| 49 const std::string& server_host, | 44 const std::string& server_host, |
| 50 const std::string& rpc_name, | 45 const std::string& rpc_name); |
| 51 const std::string& tracing_token, | |
| 52 const google::protobuf::MessageLite& request_proto); | |
| 53 | 46 |
| 54 // HTTP requests are cancelled on delete. | 47 // HTTP requests are cancelled on delete. |
| 55 virtual ~HttpPost(); | 48 virtual ~HttpPost(); |
| 56 | 49 |
| 50 // Add an Apiary tracing token to the request. |
| 51 void set_tracing_token(const std::string& tracing_token); |
| 52 |
| 53 // Add an API key to the request. If none is specified, |
| 54 // we attempt to use Chrome's key. |
| 55 void set_api_key(const std::string& api_key) { api_key_ = api_key; } |
| 56 |
| 57 // Send an HttpPost request. | 57 // Send an HttpPost request. |
| 58 void Start(const ResponseCallback& response_callback); | 58 void Start(const ResponseCallback& response_callback, |
| 59 const google::protobuf::MessageLite& request_proto); |
| 59 | 60 |
| 60 private: | 61 private: |
| 62 static const int kUrlFetcherId = 1; |
| 63 static const char kApiKeyField[]; |
| 64 static const char kTracingTokenField[]; |
| 65 |
| 66 friend class HttpPostTest; |
| 67 |
| 61 // Overridden from net::URLFetcherDelegate. | 68 // Overridden from net::URLFetcherDelegate. |
| 62 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 69 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 63 | 70 |
| 71 net::URLRequestContextGetter* url_context_getter_; |
| 72 GURL url_; |
| 73 |
| 64 ResponseCallback response_callback_; | 74 ResponseCallback response_callback_; |
| 75 |
| 65 scoped_ptr<net::URLFetcher> url_fetcher_; | 76 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 77 std::string api_key_; |
| 66 | 78 |
| 67 DISALLOW_COPY_AND_ASSIGN(HttpPost); | 79 DISALLOW_COPY_AND_ASSIGN(HttpPost); |
| 68 }; | 80 }; |
| 69 | 81 |
| 70 } // namespace copresence | 82 } // namespace copresence |
| 71 | 83 |
| 72 #endif // COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ | 84 #endif // COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ |
| OLD | NEW |