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_RPC_HANDLER_H_
6 #define COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_
13 #include "base/callback_forward.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "components/copresence/proto/enums.pb.h"
17 #include "components/copresence/public/copresence_delegate.h"
18 #include "components/copresence/timed_map.h"
20 namespace copresence {
23 class CopresenceDelegate;
24 class DirectiveHandler;
29 class SubscribedMessage;
31 // This class currently handles all communication with the copresence server.
34 // A callback to indicate whether handler initialization succeeded.
35 typedef base::Callback<void(bool)> SuccessCallback;
37 // An HttpPost::ResponseCallback along with an HttpPost object to be deleted.
39 // HttpPost*: The handler should take ownership of (i.e. delete) this object.
40 // int: The HTTP status code of the response.
41 // string: The contents of the response.
42 typedef base::Callback<void(HttpPost*, int, const std::string&)>
45 // Callback to allow tests to stub out HTTP POST behavior.
47 // URLRequestContextGetter: Context for the HTTP POST request.
48 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name
49 // string: The API key to pass in the request.
50 // string: The auth token to pass with the request.
51 // MessageLite: Contents of POST request to be sent. This needs to be
52 // a (scoped) pointer to ease handling of the abstract MessageLite class.
53 // PostCleanupCallback: Receives the response to the request.
54 typedef base::Callback<void(net::URLRequestContextGetter*,
58 scoped_ptr<google::protobuf::MessageLite>,
59 const PostCleanupCallback&)> PostCallback;
61 // Report rpc name to send to Apiary.
62 static const char kReportRequestRpcName[];
64 // Constructor. |delegate| and |directive_handler|
65 // are owned by the caller and must outlive the RpcHandler.
66 // |server_post_callback| should be set only by tests.
67 RpcHandler(CopresenceDelegate* delegate,
68 DirectiveHandler* directive_handler,
69 GCMHandler* gcm_handler,
70 const PostCallback& server_post_callback = PostCallback());
72 virtual ~RpcHandler();
74 // Send a ReportRequest from a specific app, and get notified of completion.
75 void SendReportRequest(scoped_ptr<ReportRequest> request,
76 const std::string& app_id,
77 const std::string& auth_token,
78 const StatusCallback& callback);
80 // Report a set of tokens to the server for a given medium.
81 // Uses all active auth tokens (if any).
82 void ReportTokens(const std::vector<AudioToken>& tokens);
85 // A queued ReportRequest along with its metadata.
86 struct PendingRequest {
87 PendingRequest(scoped_ptr<ReportRequest> report,
88 const std::string& app_id,
89 const std::string& auth_token,
90 const StatusCallback& callback);
93 scoped_ptr<ReportRequest> report;
94 const std::string app_id;
95 const std::string auth_token;
96 const StatusCallback callback;
99 friend class RpcHandlerTest;
101 // Before accepting any other calls, the server requires registration,
102 // which is tied to the auth token (or lack thereof) used to call Report.
103 void RegisterForToken(const std::string& auth_token);
105 // Device registration has completed. Send the requests that it was blocking.
106 void ProcessQueuedRequests(const std::string& auth_token);
108 // Send a ReportRequest from Chrome itself, i.e. no app id.
109 void SendReportRequest(scoped_ptr<ReportRequest> request,
110 const std::string& auth_token);
112 // Store a GCM ID and send it to the server if needed.
113 void RegisterGcmId(const std::string& gcm_id);
115 // Server call response handlers.
116 void RegisterResponseHandler(const std::string& auth_token,
118 HttpPost* completed_post,
119 int http_status_code,
120 const std::string& response_data);
121 void ReportResponseHandler(const StatusCallback& status_callback,
122 HttpPost* completed_post,
123 int http_status_code,
124 const std::string& response_data);
126 // If the request has any unpublish or unsubscribe operations, it removes
127 // them from our directive handlers.
128 void ProcessRemovedOperations(const ReportRequest& request);
130 // Add all currently playing tokens to the update signals in this report
131 // request. This ensures that the server doesn't keep issueing new tokens to
132 // us when we're already playing valid tokens.
133 void AddPlayingTokens(ReportRequest* request);
135 void DispatchMessages(
136 const google::protobuf::RepeatedPtrField<SubscribedMessage>&
137 subscribed_messages);
139 RequestHeader* CreateRequestHeader(const std::string& client_name,
140 const std::string& device_id) const;
142 // Post a request to the server. The request should be in proto format.
144 void SendServerRequest(const std::string& rpc_name,
145 const std::string& device_id,
146 const std::string& app_id,
147 const std::string& auth_token,
148 scoped_ptr<T> request,
149 const PostCleanupCallback& response_handler);
151 // Wrapper for the http post constructor. This is the default way
152 // to contact the server, but it can be overridden for testing.
153 void SendHttpPost(net::URLRequestContextGetter* url_context_getter,
154 const std::string& rpc_name,
155 const std::string& api_key,
156 const std::string& auth_token,
157 scoped_ptr<google::protobuf::MessageLite> request_proto,
158 const PostCleanupCallback& callback);
160 // These belong to the caller.
161 CopresenceDelegate* const delegate_;
162 DirectiveHandler* const directive_handler_;
163 GCMHandler* const gcm_handler_;
165 PostCallback server_post_callback_;
167 ScopedVector<PendingRequest> pending_requests_queue_;
168 TimedMap<std::string, bool> invalid_audio_token_cache_;
169 std::map<std::string, std::string> device_id_by_auth_token_;
170 std::set<HttpPost*> pending_posts_;
173 DISALLOW_COPY_AND_ASSIGN(RpcHandler);
176 } // namespace copresence
178 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_