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> |
(...skipping 13 matching lines...) Expand all Loading... |
24 class ReportRequest; | 24 class ReportRequest; |
25 class RequestHeader; | 25 class RequestHeader; |
26 class SubscribedMessage; | 26 class SubscribedMessage; |
27 | 27 |
28 // This class currently handles all communication with the copresence server. | 28 // This class currently handles all communication with the copresence server. |
29 class RpcHandler { | 29 class RpcHandler { |
30 public: | 30 public: |
31 // A callback to indicate whether handler initialization succeeded. | 31 // A callback to indicate whether handler initialization succeeded. |
32 typedef base::Callback<void(bool)> SuccessCallback; | 32 typedef base::Callback<void(bool)> SuccessCallback; |
33 | 33 |
| 34 // An HttpPost::ResponseCallback along with an HttpPost object to be deleted. |
| 35 // Arguments: |
| 36 // HttpPost*: The handler should take ownership of (i.e. delete) this object. |
| 37 // int: The HTTP status code of the response. |
| 38 // string: The contents of the response. |
| 39 typedef base::Callback<void(HttpPost*, int, const std::string&)> |
| 40 PostCleanupCallback; |
| 41 |
| 42 // Callback to allow tests to stub out HTTP POST behavior. |
| 43 // Arguments: |
| 44 // URLRequestContextGetter: Context for the HTTP POST request. |
| 45 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name |
| 46 // string: The API key to pass in the request. |
| 47 // string: The auth token to pass with the request. |
| 48 // MessageLite: Contents of POST request to be sent. This needs to be |
| 49 // a (scoped) pointer to ease handling of the abstract MessageLite class. |
| 50 // PostCleanupCallback: Receives the response to the request. |
| 51 typedef base::Callback<void(net::URLRequestContextGetter*, |
| 52 const std::string&, |
| 53 const std::string&, |
| 54 const std::string&, |
| 55 scoped_ptr<google::protobuf::MessageLite>, |
| 56 const PostCleanupCallback&)> PostCallback; |
| 57 |
34 // Report rpc name to send to Apiary. | 58 // Report rpc name to send to Apiary. |
35 static const char kReportRequestRpcName[]; | 59 static const char kReportRequestRpcName[]; |
36 | 60 |
37 // Constructor. |delegate| and |directive_handler| | 61 // Constructor. |delegate| and |directive_handler| |
38 // are owned by the caller and must outlive the RpcHandler. | 62 // are owned by the caller and must outlive the RpcHandler. |
39 RpcHandler(CopresenceDelegate* delegate, DirectiveHandler* directive_handler); | 63 RpcHandler(CopresenceDelegate* delegate, |
| 64 DirectiveHandler* directive_handler, |
| 65 const PostCallback& server_post_callback = PostCallback()); |
40 | 66 |
41 virtual ~RpcHandler(); | 67 virtual ~RpcHandler(); |
42 | 68 |
43 // Before accepting any other calls, the server requires registration, | 69 // Before accepting any other calls, the server requires registration, |
44 // which is tied to the auth token (or lack thereof) used to call Report. | 70 // which is tied to the auth token (or lack thereof) used to call Report. |
45 // Clients must call RegisterForToken() for each new token, | 71 // Clients must call RegisterForToken() for each new token, |
46 // *including the empty token*, they need to pass to SendReportRequest(), | 72 // *including the empty token*, they need to pass to SendReportRequest(), |
47 // and then wait for |init_done_callback| to be invoked. | 73 // and then wait for |init_done_callback| to be invoked. |
48 void RegisterForToken(const std::string& auth_token, | 74 void RegisterForToken(const std::string& auth_token, |
49 const SuccessCallback& init_done_callback); | 75 const SuccessCallback& init_done_callback); |
50 | 76 |
51 // Check if a given auth token is already active (registered). | 77 // Check if a given auth token is already active (registered). |
52 bool IsRegisteredForToken(const std::string& auth_token) const; | 78 bool IsRegisteredForToken(const std::string& auth_token) const; |
53 | 79 |
54 // Send a ReportRequest from Chrome itself, i.e. no app id. | 80 // Send a ReportRequest from Chrome itself, i.e. no app id. |
55 void SendReportRequest(scoped_ptr<ReportRequest> request, | 81 void SendReportRequest(scoped_ptr<ReportRequest> request, |
56 const std::string& auth_token); | 82 const std::string& auth_token); |
57 | 83 |
58 // Send a ReportRequest from a specific app, and get notified of completion. | 84 // Send a ReportRequest from a specific app, and get notified of completion. |
59 void SendReportRequest(scoped_ptr<ReportRequest> request, | 85 void SendReportRequest(scoped_ptr<ReportRequest> request, |
60 const std::string& app_id, | 86 const std::string& app_id, |
61 const std::string& auth_token, | 87 const std::string& auth_token, |
62 const StatusCallback& callback); | 88 const StatusCallback& callback); |
63 | 89 |
64 // Report a set of tokens to the server for a given medium. | 90 // Report a set of tokens to the server for a given medium. |
65 // Uses all active auth tokens (if any). | 91 // Uses all active auth tokens (if any). |
66 void ReportTokens(const std::vector<AudioToken>& tokens); | 92 void ReportTokens(const std::vector<AudioToken>& tokens); |
67 | 93 |
68 private: | 94 private: |
69 // An HttpPost::ResponseCallback along with an HttpPost object to be deleted. | |
70 // Arguments: | |
71 // HttpPost*: The handler should take ownership of (i.e. delete) this object. | |
72 // int: The HTTP status code of the response. | |
73 // string: The contents of the response. | |
74 typedef base::Callback<void(HttpPost*, int, const std::string&)> | |
75 PostCleanupCallback; | |
76 | |
77 // Callback to allow tests to stub out HTTP POST behavior. | |
78 // Arguments: | |
79 // URLRequestContextGetter: Context for the HTTP POST request. | |
80 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name | |
81 // string: The API key to pass in the request. | |
82 // string: The auth token to pass with the request. | |
83 // MessageLite: Contents of POST request to be sent. This needs to be | |
84 // a (scoped) pointer to ease handling of the abstract MessageLite class. | |
85 // PostCleanupCallback: Receives the response to the request. | |
86 typedef base::Callback<void(net::URLRequestContextGetter*, | |
87 const std::string&, | |
88 const std::string&, | |
89 const std::string&, | |
90 scoped_ptr<google::protobuf::MessageLite>, | |
91 const PostCleanupCallback&)> PostCallback; | |
92 | |
93 friend class RpcHandlerTest; | 95 friend class RpcHandlerTest; |
94 | 96 |
95 // Server call response handlers. | 97 // Server call response handlers. |
96 void RegisterResponseHandler(const SuccessCallback& init_done_callback, | 98 void RegisterResponseHandler(const SuccessCallback& init_done_callback, |
97 const std::string& auth_token, | 99 const std::string& auth_token, |
98 HttpPost* completed_post, | 100 HttpPost* completed_post, |
99 int http_status_code, | 101 int http_status_code, |
100 const std::string& response_data); | 102 const std::string& response_data); |
101 void ReportResponseHandler(const StatusCallback& status_callback, | 103 void ReportResponseHandler(const StatusCallback& status_callback, |
102 HttpPost* completed_post, | 104 HttpPost* completed_post, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 const std::string& auth_token, | 138 const std::string& auth_token, |
137 scoped_ptr<google::protobuf::MessageLite> request_proto, | 139 scoped_ptr<google::protobuf::MessageLite> request_proto, |
138 const PostCleanupCallback& callback); | 140 const PostCleanupCallback& callback); |
139 | 141 |
140 // These belong to the caller. | 142 // These belong to the caller. |
141 CopresenceDelegate* delegate_; | 143 CopresenceDelegate* delegate_; |
142 DirectiveHandler* directive_handler_; | 144 DirectiveHandler* directive_handler_; |
143 | 145 |
144 TimedMap<std::string, bool> invalid_audio_token_cache_; | 146 TimedMap<std::string, bool> invalid_audio_token_cache_; |
145 | 147 |
146 // TODO(ckehoe): Allow passing this into the constructor for testing. | |
147 PostCallback server_post_callback_; | 148 PostCallback server_post_callback_; |
148 | 149 |
149 std::map<std::string, std::string> device_id_by_auth_token_; | 150 std::map<std::string, std::string> device_id_by_auth_token_; |
150 std::set<HttpPost*> pending_posts_; | 151 std::set<HttpPost*> pending_posts_; |
151 | 152 |
152 DISALLOW_COPY_AND_ASSIGN(RpcHandler); | 153 DISALLOW_COPY_AND_ASSIGN(RpcHandler); |
153 }; | 154 }; |
154 | 155 |
155 } // namespace copresence | 156 } // namespace copresence |
156 | 157 |
157 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ | 158 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ |
OLD | NEW |