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 <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
16 #include "components/copresence/proto/enums.pb.h" | 16 #include "components/copresence/proto/enums.pb.h" |
17 #include "components/copresence/public/copresence_delegate.h" | 17 #include "components/copresence/public/copresence_delegate.h" |
18 #include "components/copresence/timed_map.h" | 18 #include "components/copresence/timed_map.h" |
19 | 19 |
20 namespace copresence { | 20 namespace copresence { |
21 | 21 |
22 struct AudioToken; | 22 struct AudioToken; |
23 class CopresenceDelegate; | 23 class CopresenceDelegate; |
24 class DirectiveHandler; | 24 class DirectiveHandler; |
| 25 class GCMHandler; |
25 class HttpPost; | 26 class HttpPost; |
26 class ReportRequest; | 27 class ReportRequest; |
27 class RequestHeader; | 28 class RequestHeader; |
28 class SubscribedMessage; | 29 class SubscribedMessage; |
29 | 30 |
30 // This class currently handles all communication with the copresence server. | 31 // This class currently handles all communication with the copresence server. |
31 class RpcHandler { | 32 class RpcHandler { |
32 public: | 33 public: |
33 // A callback to indicate whether handler initialization succeeded. | 34 // A callback to indicate whether handler initialization succeeded. |
34 typedef base::Callback<void(bool)> SuccessCallback; | 35 typedef base::Callback<void(bool)> SuccessCallback; |
(...skipping 23 matching lines...) Expand all Loading... |
58 const PostCleanupCallback&)> PostCallback; | 59 const PostCleanupCallback&)> PostCallback; |
59 | 60 |
60 // Report rpc name to send to Apiary. | 61 // Report rpc name to send to Apiary. |
61 static const char kReportRequestRpcName[]; | 62 static const char kReportRequestRpcName[]; |
62 | 63 |
63 // Constructor. |delegate| and |directive_handler| | 64 // Constructor. |delegate| and |directive_handler| |
64 // are owned by the caller and must outlive the RpcHandler. | 65 // are owned by the caller and must outlive the RpcHandler. |
65 // |server_post_callback| should be set only by tests. | 66 // |server_post_callback| should be set only by tests. |
66 RpcHandler(CopresenceDelegate* delegate, | 67 RpcHandler(CopresenceDelegate* delegate, |
67 DirectiveHandler* directive_handler, | 68 DirectiveHandler* directive_handler, |
| 69 GCMHandler* gcm_handler, |
68 const PostCallback& server_post_callback = PostCallback()); | 70 const PostCallback& server_post_callback = PostCallback()); |
69 | 71 |
70 virtual ~RpcHandler(); | 72 virtual ~RpcHandler(); |
71 | 73 |
72 // Send a ReportRequest from a specific app, and get notified of completion. | 74 // Send a ReportRequest from a specific app, and get notified of completion. |
73 void SendReportRequest(scoped_ptr<ReportRequest> request, | 75 void SendReportRequest(scoped_ptr<ReportRequest> request, |
74 const std::string& app_id, | 76 const std::string& app_id, |
75 const std::string& auth_token, | 77 const std::string& auth_token, |
76 const StatusCallback& callback); | 78 const StatusCallback& callback); |
77 | 79 |
(...skipping 22 matching lines...) Expand all Loading... |
100 // which is tied to the auth token (or lack thereof) used to call Report. | 102 // which is tied to the auth token (or lack thereof) used to call Report. |
101 void RegisterForToken(const std::string& auth_token); | 103 void RegisterForToken(const std::string& auth_token); |
102 | 104 |
103 // Device registration has completed. Send the requests that it was blocking. | 105 // Device registration has completed. Send the requests that it was blocking. |
104 void ProcessQueuedRequests(const std::string& auth_token); | 106 void ProcessQueuedRequests(const std::string& auth_token); |
105 | 107 |
106 // Send a ReportRequest from Chrome itself, i.e. no app id. | 108 // Send a ReportRequest from Chrome itself, i.e. no app id. |
107 void SendReportRequest(scoped_ptr<ReportRequest> request, | 109 void SendReportRequest(scoped_ptr<ReportRequest> request, |
108 const std::string& auth_token); | 110 const std::string& auth_token); |
109 | 111 |
| 112 // Store a GCM ID and send it to the server if needed. |
| 113 void RegisterGcmId(const std::string& gcm_id); |
| 114 |
110 // Server call response handlers. | 115 // Server call response handlers. |
111 void RegisterResponseHandler(const std::string& auth_token, | 116 void RegisterResponseHandler(const std::string& auth_token, |
| 117 bool gcm_pending, |
112 HttpPost* completed_post, | 118 HttpPost* completed_post, |
113 int http_status_code, | 119 int http_status_code, |
114 const std::string& response_data); | 120 const std::string& response_data); |
115 void ReportResponseHandler(const StatusCallback& status_callback, | 121 void ReportResponseHandler(const StatusCallback& status_callback, |
116 HttpPost* completed_post, | 122 HttpPost* completed_post, |
117 int http_status_code, | 123 int http_status_code, |
118 const std::string& response_data); | 124 const std::string& response_data); |
119 | 125 |
120 // If the request has any unpublish or unsubscribe operations, it removes | 126 // If the request has any unpublish or unsubscribe operations, it removes |
121 // them from our directive handlers. | 127 // them from our directive handlers. |
(...skipping 23 matching lines...) Expand all Loading... |
145 // Wrapper for the http post constructor. This is the default way | 151 // Wrapper for the http post constructor. This is the default way |
146 // to contact the server, but it can be overridden for testing. | 152 // to contact the server, but it can be overridden for testing. |
147 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, | 153 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, |
148 const std::string& rpc_name, | 154 const std::string& rpc_name, |
149 const std::string& api_key, | 155 const std::string& api_key, |
150 const std::string& auth_token, | 156 const std::string& auth_token, |
151 scoped_ptr<google::protobuf::MessageLite> request_proto, | 157 scoped_ptr<google::protobuf::MessageLite> request_proto, |
152 const PostCleanupCallback& callback); | 158 const PostCleanupCallback& callback); |
153 | 159 |
154 // These belong to the caller. | 160 // These belong to the caller. |
155 CopresenceDelegate* delegate_; | 161 CopresenceDelegate* const delegate_; |
156 DirectiveHandler* directive_handler_; | 162 DirectiveHandler* const directive_handler_; |
| 163 GCMHandler* const gcm_handler_; |
157 | 164 |
158 PostCallback server_post_callback_; | 165 PostCallback server_post_callback_; |
159 | 166 |
160 ScopedVector<PendingRequest> pending_requests_queue_; | 167 ScopedVector<PendingRequest> pending_requests_queue_; |
161 TimedMap<std::string, bool> invalid_audio_token_cache_; | 168 TimedMap<std::string, bool> invalid_audio_token_cache_; |
162 std::map<std::string, std::string> device_id_by_auth_token_; | 169 std::map<std::string, std::string> device_id_by_auth_token_; |
163 std::set<HttpPost*> pending_posts_; | 170 std::set<HttpPost*> pending_posts_; |
| 171 std::string gcm_id_; |
164 | 172 |
165 DISALLOW_COPY_AND_ASSIGN(RpcHandler); | 173 DISALLOW_COPY_AND_ASSIGN(RpcHandler); |
166 }; | 174 }; |
167 | 175 |
168 } // namespace copresence | 176 } // namespace copresence |
169 | 177 |
170 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ | 178 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ |
OLD | NEW |