| OLD | NEW |
| (Empty) |
| 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 <memory> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "net/url_request/url_fetcher_delegate.h" | |
| 14 | |
| 15 namespace google { | |
| 16 namespace protobuf { | |
| 17 class MessageLite; | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 namespace net { | |
| 22 class URLRequestContextGetter; | |
| 23 } | |
| 24 | |
| 25 namespace 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. | |
| 32 class 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 using ResponseCallback = base::Callback<void(int, const std::string&)>; | |
| 37 | |
| 38 // Create a request to the Copresence server. | |
| 39 // |url_context_getter| is owned by the caller, | |
| 40 // and the context it provides must be available until the request completes. | |
| 41 HttpPost(net::URLRequestContextGetter* url_context_getter, | |
| 42 const std::string& server_host, | |
| 43 // TODO(ckehoe): Condense some of these into a struct. | |
| 44 const std::string& rpc_name, | |
| 45 std::string api_key, // If blank, we overwrite with a default. | |
| 46 const std::string& auth_token, | |
| 47 const std::string& tracing_token, | |
| 48 const google::protobuf::MessageLite& request_proto); | |
| 49 | |
| 50 // HTTP requests are cancelled on delete. | |
| 51 ~HttpPost() override; | |
| 52 | |
| 53 // Send an HttpPost request. | |
| 54 void Start(const ResponseCallback& response_callback); | |
| 55 | |
| 56 private: | |
| 57 static const int kUrlFetcherId = 1; | |
| 58 static const char kApiKeyField[]; | |
| 59 static const char kTracingField[]; | |
| 60 | |
| 61 friend class HttpPostTest; | |
| 62 | |
| 63 // Overridden from net::URLFetcherDelegate. | |
| 64 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 65 | |
| 66 ResponseCallback response_callback_; | |
| 67 | |
| 68 std::unique_ptr<net::URLFetcher> url_fetcher_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(HttpPost); | |
| 71 }; | |
| 72 | |
| 73 } // namespace copresence | |
| 74 | |
| 75 #endif // COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_ | |
| OLD | NEW |