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_RPC_HANDLER_H_ | 5 #ifndef COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ |
6 #define COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ | 6 #define COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback_forward.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "components/copresence/proto/enums.pb.h" | 14 #include "components/copresence/proto/enums.pb.h" |
15 #include "components/copresence/public/copresence_delegate.h" | 15 #include "components/copresence/public/copresence_delegate.h" |
16 #include "components/copresence/public/whispernet_client.h" | |
17 #include "components/copresence/rpc/http_post.h" | |
18 #include "components/copresence/timed_map.h" | 16 #include "components/copresence/timed_map.h" |
19 | 17 |
20 namespace copresence { | 18 namespace copresence { |
21 | 19 |
| 20 struct AudioToken; |
| 21 class CopresenceDelegate; |
22 class DirectiveHandler; | 22 class DirectiveHandler; |
| 23 class HttpPost; |
23 class ReportRequest; | 24 class ReportRequest; |
24 class RequestHeader; | 25 class RequestHeader; |
25 class SubscribedMessage; | 26 class SubscribedMessage; |
26 | 27 |
27 // This class currently handles all communication with the copresence server. | 28 // This class currently handles all communication with the copresence server. |
28 class RpcHandler { | 29 class RpcHandler { |
29 public: | 30 public: |
30 // A callback to indicate whether handler initialization succeeded. | 31 // A callback to indicate whether handler initialization succeeded. |
31 typedef base::Callback<void(bool)> SuccessCallback; | 32 typedef base::Callback<void(bool)> SuccessCallback; |
32 | 33 |
33 // Report rpc name to send to Apiary. | 34 // Report rpc name to send to Apiary. |
34 static const char kReportRequestRpcName[]; | 35 static const char kReportRequestRpcName[]; |
35 | 36 |
36 // Constructor. |delegate| is owned by the caller, | 37 // Constructor. |delegate| and |directive_handler| |
37 // and must be valid as long as the RpcHandler exists. | 38 // are owned by the caller and must outlive the RpcHandler. |
38 explicit RpcHandler(CopresenceDelegate* delegate); | 39 RpcHandler(CopresenceDelegate* delegate, DirectiveHandler* directive_handler); |
39 | 40 |
40 virtual ~RpcHandler(); | 41 virtual ~RpcHandler(); |
41 | 42 |
42 // Before accepting any other calls, the server requires registration, | 43 // Before accepting any other calls, the server requires registration, |
43 // which is tied to the auth token (or lack thereof) used to call Report. | 44 // which is tied to the auth token (or lack thereof) used to call Report. |
44 // Clients must call RegisterForToken() for each new token, | 45 // Clients must call RegisterForToken() for each new token, |
45 // *including the empty token*, they need to pass to SendReportRequest(), | 46 // *including the empty token*, they need to pass to SendReportRequest(), |
46 // and then wait for |init_done_callback| to be invoked. | 47 // and then wait for |init_done_callback| to be invoked. |
47 void RegisterForToken(const std::string& auth_token, | 48 void RegisterForToken(const std::string& auth_token, |
48 const SuccessCallback& init_done_callback); | 49 const SuccessCallback& init_done_callback); |
49 | 50 |
50 // Check if a given auth token is already active (registered). | 51 // Check if a given auth token is already active (registered). |
51 bool IsRegisteredForToken(const std::string& auth_token) const; | 52 bool IsRegisteredForToken(const std::string& auth_token) const; |
52 | 53 |
53 // Send a ReportRequest from Chrome itself, i.e. no app id. | 54 // Send a ReportRequest from Chrome itself, i.e. no app id. |
54 void SendReportRequest(scoped_ptr<ReportRequest> request, | 55 void SendReportRequest(scoped_ptr<ReportRequest> request, |
55 const std::string& auth_token); | 56 const std::string& auth_token); |
56 | 57 |
57 // Send a ReportRequest from a specific app, and get notified of completion. | 58 // Send a ReportRequest from a specific app, and get notified of completion. |
58 void SendReportRequest(scoped_ptr<ReportRequest> request, | 59 void SendReportRequest(scoped_ptr<ReportRequest> request, |
59 const std::string& app_id, | 60 const std::string& app_id, |
60 const std::string& auth_token, | 61 const std::string& auth_token, |
61 const StatusCallback& callback); | 62 const StatusCallback& callback); |
62 | 63 |
63 // Report a set of tokens to the server for a given medium. | 64 // Report a set of tokens to the server for a given medium. |
64 // Uses all active auth tokens (if any). | 65 // Uses all active auth tokens (if any). |
65 void ReportTokens(const std::vector<AudioToken>& tokens); | 66 void ReportTokens(const std::vector<AudioToken>& tokens); |
66 | 67 |
67 // Create the directive handler and connect it to | |
68 // the whispernet client specified by the delegate. | |
69 void ConnectToWhispernet(); | |
70 | |
71 private: | 68 private: |
72 // An HttpPost::ResponseCallback along with an HttpPost object to be deleted. | 69 // An HttpPost::ResponseCallback along with an HttpPost object to be deleted. |
73 // Arguments: | 70 // Arguments: |
74 // HttpPost*: The handler should take ownership of (i.e. delete) this object. | 71 // HttpPost*: The handler should take ownership of (i.e. delete) this object. |
75 // int: The HTTP status code of the response. | 72 // int: The HTTP status code of the response. |
76 // string: The contents of the response. | 73 // string: The contents of the response. |
77 typedef base::Callback<void(HttpPost*, int, const std::string&)> | 74 typedef base::Callback<void(HttpPost*, int, const std::string&)> |
78 PostCleanupCallback; | 75 PostCleanupCallback; |
79 | 76 |
80 // Callback to allow tests to stub out HTTP POST behavior. | 77 // Callback to allow tests to stub out HTTP POST behavior. |
81 // Arguments: | 78 // Arguments: |
82 // URLRequestContextGetter: Context for the HTTP POST request. | 79 // URLRequestContextGetter: Context for the HTTP POST request. |
83 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name | 80 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name |
84 // string: The API key to pass in the request. | 81 // string: The API key to pass in the request. |
85 // string: The auth token to pass with the request. | 82 // string: The auth token to pass with the request. |
86 // MessageLite: Contents of POST request to be sent. This needs to be | 83 // MessageLite: Contents of POST request to be sent. This needs to be |
87 // a (scoped) pointer to ease handling of the abstract MessageLite class. | 84 // a (scoped) pointer to ease handling of the abstract MessageLite class. |
88 // ResponseCallback: Receives the response to the request. | 85 // PostCleanupCallback: Receives the response to the request. |
89 typedef base::Callback<void(net::URLRequestContextGetter*, | 86 typedef base::Callback<void(net::URLRequestContextGetter*, |
90 const std::string&, | 87 const std::string&, |
91 const std::string&, | 88 const std::string&, |
92 const std::string&, | 89 const std::string&, |
93 scoped_ptr<google::protobuf::MessageLite>, | 90 scoped_ptr<google::protobuf::MessageLite>, |
94 const PostCleanupCallback&)> PostCallback; | 91 const PostCleanupCallback&)> PostCallback; |
95 | 92 |
96 friend class RpcHandlerTest; | 93 friend class RpcHandlerTest; |
97 | 94 |
98 // Server call response handlers. | 95 // Server call response handlers. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 130 |
134 // Wrapper for the http post constructor. This is the default way | 131 // Wrapper for the http post constructor. This is the default way |
135 // to contact the server, but it can be overridden for testing. | 132 // to contact the server, but it can be overridden for testing. |
136 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, | 133 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, |
137 const std::string& rpc_name, | 134 const std::string& rpc_name, |
138 const std::string& api_key, | 135 const std::string& api_key, |
139 const std::string& auth_token, | 136 const std::string& auth_token, |
140 scoped_ptr<google::protobuf::MessageLite> request_proto, | 137 scoped_ptr<google::protobuf::MessageLite> request_proto, |
141 const PostCleanupCallback& callback); | 138 const PostCleanupCallback& callback); |
142 | 139 |
143 // This method receives the request to encode a token and forwards it to | 140 // These belong to the caller. |
144 // whispernet, setting the samples return callback to samples_callback. | 141 CopresenceDelegate* delegate_; |
145 void AudioDirectiveListToWhispernetConnector( | 142 DirectiveHandler* directive_handler_; |
146 const std::string& token, | |
147 AudioType type, | |
148 const WhispernetClient::SamplesCallback& samples_callback); | |
149 | 143 |
150 CopresenceDelegate* delegate_; // Belongs to the caller. | |
151 TimedMap<std::string, bool> invalid_audio_token_cache_; | 144 TimedMap<std::string, bool> invalid_audio_token_cache_; |
| 145 |
| 146 // TODO(ckehoe): Allow passing this into the constructor for testing. |
152 PostCallback server_post_callback_; | 147 PostCallback server_post_callback_; |
153 | 148 |
154 std::map<std::string, std::string> device_id_by_auth_token_; | 149 std::map<std::string, std::string> device_id_by_auth_token_; |
155 scoped_ptr<DirectiveHandler> directive_handler_; | |
156 std::set<HttpPost*> pending_posts_; | 150 std::set<HttpPost*> pending_posts_; |
157 | 151 |
158 DISALLOW_COPY_AND_ASSIGN(RpcHandler); | 152 DISALLOW_COPY_AND_ASSIGN(RpcHandler); |
159 }; | 153 }; |
160 | 154 |
161 } // namespace copresence | 155 } // namespace copresence |
162 | 156 |
163 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ | 157 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ |
OLD | NEW |