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.
5 #ifndef COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_
6 #define COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_
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"
22 class URLRequestContextGetter
;
25 namespace copresence
{
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.
31 // TODO(ckehoe): Add retry logic.
32 class HttpPost
: public net::URLFetcherDelegate
{
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
&)>
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
);
49 // HTTP requests are cancelled on delete.
52 // Send an HttpPost request.
53 void Start(const ResponseCallback
& response_callback
);
56 static const int kUrlFetcherId
= 1;
57 static const char kApiKeyField
[];
58 static const char kTracingField
[];
60 friend class HttpPostTest
;
62 // Overridden from net::URLFetcherDelegate.
63 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
65 ResponseCallback response_callback_
;
67 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
69 DISALLOW_COPY_AND_ASSIGN(HttpPost
);
72 } // namespace copresence
74 #endif // COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_