summaryrefslogtreecommitdiffstats
path: root/components/copresence/rpc/http_post.h
blob: f324b69066d76d5b51739de7169bbc885e6b34ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_
#define COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_

#include <string>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "net/url_request/url_fetcher_delegate.h"

namespace google {
namespace protobuf {
class MessageLite;
}
}

namespace net {
class URLRequestContextGetter;
}

namespace copresence {

// This class handles all Apiary calls to the Copresence server.
// It configures the HTTP request appropriately and reports any errors.
// If deleted, the HTTP request is cancelled.
//
// TODO(ckehoe): Add retry logic.
class HttpPost : public net::URLFetcherDelegate {
 public:
  // Callback to receive the HTTP status code and body of the response
  // (if any). A pointer to this HttpPost object is also passed along.
  typedef base::Callback<void(int, const std::string&)>
      ResponseCallback;

  // Create a request to the Copresence server.
  // |url_context_getter| is owned by the caller,
  // and the context it provides must be available until the request completes.
  HttpPost(net::URLRequestContextGetter* url_context_getter,
           const std::string& server_host,
           const std::string& rpc_name,
           const std::string& tracing_token,
           std::string api_key,  // If blank, we overwrite with a default.
           const google::protobuf::MessageLite& request_proto);

  // HTTP requests are cancelled on delete.
  virtual ~HttpPost();

  // Send an HttpPost request.
  void Start(const ResponseCallback& response_callback);

 private:
  static const int kUrlFetcherId = 1;
  static const char kApiKeyField[];
  static const char kTracingField[];

  friend class HttpPostTest;

  // Overridden from net::URLFetcherDelegate.
  virtual void OnURLFetchComplete(const net::URLFetcher* source) override;

  ResponseCallback response_callback_;

  scoped_ptr<net::URLFetcher> url_fetcher_;

  DISALLOW_COPY_AND_ASSIGN(HttpPost);
};

}  // namespace copresence

#endif  // COMPONENTS_COPRESENCE_RPC_HTTP_POST_H_