Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / copresence / rpc / rpc_handler.h
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.
4
5 #ifndef COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_
6 #define COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
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"
19
20 namespace copresence {
21
22 struct AudioToken;
23 class CopresenceDelegate;
24 class DirectiveHandler;
25 class GCMHandler;
26 class HttpPost;
27 class ReportRequest;
28 class RequestHeader;
29 class SubscribedMessage;
30
31 // This class currently handles all communication with the copresence server.
32 class RpcHandler {
33  public:
34   // A callback to indicate whether handler initialization succeeded.
35   typedef base::Callback<void(bool)> SuccessCallback;
36
37   // An HttpPost::ResponseCallback along with an HttpPost object to be deleted.
38   // Arguments:
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&)>
43       PostCleanupCallback;
44
45   // Callback to allow tests to stub out HTTP POST behavior.
46   // Arguments:
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*,
55                               const std::string&,
56                               const std::string&,
57                               const std::string&,
58                               scoped_ptr<google::protobuf::MessageLite>,
59                               const PostCleanupCallback&)> PostCallback;
60
61   // Report rpc name to send to Apiary.
62   static const char kReportRequestRpcName[];
63
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());
71
72   virtual ~RpcHandler();
73
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);
79
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);
83
84  private:
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);
91     ~PendingRequest();
92
93     scoped_ptr<ReportRequest> report;
94     const std::string app_id;
95     const std::string auth_token;
96     const StatusCallback callback;
97   };
98
99   friend class RpcHandlerTest;
100
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);
104
105   // Device registration has completed. Send the requests that it was blocking.
106   void ProcessQueuedRequests(const std::string& auth_token);
107
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);
111
112   // Store a GCM ID and send it to the server if needed.
113   void RegisterGcmId(const std::string& gcm_id);
114
115   // Server call response handlers.
116   void RegisterResponseHandler(const std::string& auth_token,
117                                bool gcm_pending,
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);
125
126   // If the request has any unpublish or unsubscribe operations, it removes
127   // them from our directive handlers.
128   void ProcessRemovedOperations(const ReportRequest& request);
129
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);
134
135   void DispatchMessages(
136       const google::protobuf::RepeatedPtrField<SubscribedMessage>&
137       subscribed_messages);
138
139   RequestHeader* CreateRequestHeader(const std::string& client_name,
140                                      const std::string& device_id) const;
141
142   // Post a request to the server. The request should be in proto format.
143   template <class T>
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);
150
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);
159
160   // These belong to the caller.
161   CopresenceDelegate* const delegate_;
162   DirectiveHandler* const directive_handler_;
163   GCMHandler* const gcm_handler_;
164
165   PostCallback server_post_callback_;
166
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_;
171   std::string gcm_id_;
172
173   DISALLOW_COPY_AND_ASSIGN(RpcHandler);
174 };
175
176 }  // namespace copresence
177
178 #endif  // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_