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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 // Report rpc name to send to Apiary. | 33 // Report rpc name to send to Apiary. |
34 static const char kReportRequestRpcName[]; | 34 static const char kReportRequestRpcName[]; |
35 | 35 |
36 // Constructor. |delegate| is owned by the caller, | 36 // Constructor. |delegate| is owned by the caller, |
37 // and must be valid as long as the RpcHandler exists. | 37 // and must be valid as long as the RpcHandler exists. |
38 explicit RpcHandler(CopresenceDelegate* delegate); | 38 explicit RpcHandler(CopresenceDelegate* delegate); |
39 | 39 |
40 virtual ~RpcHandler(); | 40 virtual ~RpcHandler(); |
41 | 41 |
42 // Clients must call this and wait for |init_done_callback| | 42 // Before accepting any other calls, the server requires registration, |
43 // to be called before invoking any other methods. | 43 // which is tied to the auth token (or lack thereof) used to call Report. |
44 void Initialize(const SuccessCallback& init_done_callback); | 44 // Clients must call RegisterForToken() for each new token, |
| 45 // *including the empty token*, they need to pass to SendReportRequest(), |
| 46 // and then wait for |init_done_callback| to be invoked. |
| 47 void RegisterForToken(const std::string& auth_token, |
| 48 const SuccessCallback& init_done_callback); |
45 | 49 |
46 // Send a report request. | 50 // Check if a given auth token is already active (registered). |
47 void SendReportRequest(scoped_ptr<ReportRequest> request); | 51 bool IsRegisteredForToken(const std::string& auth_token) const; |
| 52 |
| 53 // Send a ReportRequest from Chrome itself, i.e. no app id. |
| 54 void SendReportRequest(scoped_ptr<ReportRequest> request, |
| 55 const std::string& auth_token); |
| 56 |
| 57 // Send a ReportRequest from a specific app, and get notified of completion. |
48 void SendReportRequest(scoped_ptr<ReportRequest> request, | 58 void SendReportRequest(scoped_ptr<ReportRequest> request, |
49 const std::string& app_id, | 59 const std::string& app_id, |
| 60 const std::string& auth_token, |
50 const StatusCallback& callback); | 61 const StatusCallback& callback); |
51 | 62 |
52 // Report a set of tokens to the server for a given medium. | 63 // Report a set of tokens to the server for a given medium. |
| 64 // Uses all active auth tokens (if any). |
53 void ReportTokens(const std::vector<AudioToken>& tokens); | 65 void ReportTokens(const std::vector<AudioToken>& tokens); |
54 | 66 |
55 // Create the directive handler and connect it to | 67 // Create the directive handler and connect it to |
56 // the whispernet client specified by the delegate. | 68 // the whispernet client specified by the delegate. |
57 void ConnectToWhispernet(); | 69 void ConnectToWhispernet(); |
58 | 70 |
59 private: | 71 private: |
60 // An HttpPost::ResponseCallback prepended with an HttpPost object | 72 // An HttpPost::ResponseCallback along with an HttpPost object to be deleted. |
61 // that needs to be deleted. | 73 // Arguments: |
| 74 // HttpPost*: The handler should take ownership of (i.e. delete) this object. |
| 75 // int: The HTTP status code of the response. |
| 76 // string: The contents of the response. |
62 typedef base::Callback<void(HttpPost*, int, const std::string&)> | 77 typedef base::Callback<void(HttpPost*, int, const std::string&)> |
63 PostCleanupCallback; | 78 PostCleanupCallback; |
64 | 79 |
65 // Callback to allow tests to stub out HTTP POST behavior. | 80 // Callback to allow tests to stub out HTTP POST behavior. |
66 // Arguments: | 81 // Arguments: |
67 // URLRequestContextGetter: Context for the HTTP POST request. | 82 // URLRequestContextGetter: Context for the HTTP POST request. |
68 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name | 83 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name |
| 84 // string: The API key to pass in the request. |
| 85 // string: The auth token to pass with the request. |
69 // MessageLite: Contents of POST request to be sent. This needs to be | 86 // MessageLite: Contents of POST request to be sent. This needs to be |
70 // a (scoped) pointer to ease handling of the abstract MessageLite class. | 87 // a (scoped) pointer to ease handling of the abstract MessageLite class. |
71 // ResponseCallback: Receives the response to the request. | 88 // ResponseCallback: Receives the response to the request. |
72 typedef base::Callback<void(net::URLRequestContextGetter*, | 89 typedef base::Callback<void(net::URLRequestContextGetter*, |
73 const std::string&, | 90 const std::string&, |
| 91 const std::string&, |
| 92 const std::string&, |
74 scoped_ptr<google::protobuf::MessageLite>, | 93 scoped_ptr<google::protobuf::MessageLite>, |
75 const PostCleanupCallback&)> PostCallback; | 94 const PostCleanupCallback&)> PostCallback; |
76 | 95 |
77 friend class RpcHandlerTest; | 96 friend class RpcHandlerTest; |
78 | 97 |
| 98 // Server call response handlers. |
79 void RegisterResponseHandler(const SuccessCallback& init_done_callback, | 99 void RegisterResponseHandler(const SuccessCallback& init_done_callback, |
| 100 const std::string& auth_token, |
80 HttpPost* completed_post, | 101 HttpPost* completed_post, |
81 int http_status_code, | 102 int http_status_code, |
82 const std::string& response_data); | 103 const std::string& response_data); |
83 void ReportResponseHandler(const StatusCallback& status_callback, | 104 void ReportResponseHandler(const StatusCallback& status_callback, |
84 HttpPost* completed_post, | 105 HttpPost* completed_post, |
85 int http_status_code, | 106 int http_status_code, |
86 const std::string& response_data); | 107 const std::string& response_data); |
87 | 108 |
88 // If the request has any unpublish or unsubscribe operations, it removes | 109 // If the request has any unpublish or unsubscribe operations, it removes |
89 // them from our directive handlers. | 110 // them from our directive handlers. |
90 void ProcessRemovedOperations(const ReportRequest& request); | 111 void ProcessRemovedOperations(const ReportRequest& request); |
91 | 112 |
92 // Add all currently playing tokens to the update signals in this report | 113 // Add all currently playing tokens to the update signals in this report |
93 // request. This ensures that the server doesn't keep issueing new tokens to | 114 // request. This ensures that the server doesn't keep issueing new tokens to |
94 // us when we're already playing valid tokens. | 115 // us when we're already playing valid tokens. |
95 void AddPlayingTokens(ReportRequest* request); | 116 void AddPlayingTokens(ReportRequest* request); |
96 | 117 |
97 void DispatchMessages( | 118 void DispatchMessages( |
98 const google::protobuf::RepeatedPtrField<SubscribedMessage>& | 119 const google::protobuf::RepeatedPtrField<SubscribedMessage>& |
99 subscribed_messages); | 120 subscribed_messages); |
100 | 121 |
101 RequestHeader* CreateRequestHeader(const std::string& client_name) const; | 122 RequestHeader* CreateRequestHeader(const std::string& client_name, |
| 123 const std::string& device_id) const; |
102 | 124 |
| 125 // Post a request to the server. The request should be in proto format. |
103 template <class T> | 126 template <class T> |
104 void SendServerRequest(const std::string& rpc_name, | 127 void SendServerRequest(const std::string& rpc_name, |
| 128 const std::string& device_id, |
105 const std::string& app_id, | 129 const std::string& app_id, |
| 130 const std::string& auth_token, |
106 scoped_ptr<T> request, | 131 scoped_ptr<T> request, |
107 const PostCleanupCallback& response_handler); | 132 const PostCleanupCallback& response_handler); |
108 | 133 |
109 // Wrapper for the http post constructor. This is the default way | 134 // Wrapper for the http post constructor. This is the default way |
110 // to contact the server, but it can be overridden for testing. | 135 // to contact the server, but it can be overridden for testing. |
111 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, | 136 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, |
112 const std::string& rpc_name, | 137 const std::string& rpc_name, |
| 138 const std::string& api_key, |
| 139 const std::string& auth_token, |
113 scoped_ptr<google::protobuf::MessageLite> request_proto, | 140 scoped_ptr<google::protobuf::MessageLite> request_proto, |
114 const PostCleanupCallback& callback); | 141 const PostCleanupCallback& callback); |
115 | 142 |
116 // This method receives the request to encode a token and forwards it to | 143 // This method receives the request to encode a token and forwards it to |
117 // whispernet, setting the samples return callback to samples_callback. | 144 // whispernet, setting the samples return callback to samples_callback. |
118 void AudioDirectiveListToWhispernetConnector( | 145 void AudioDirectiveListToWhispernetConnector( |
119 const std::string& token, | 146 const std::string& token, |
120 AudioType type, | 147 AudioType type, |
121 const WhispernetClient::SamplesCallback& samples_callback); | 148 const WhispernetClient::SamplesCallback& samples_callback); |
122 | 149 |
123 CopresenceDelegate* delegate_; // Belongs to the caller. | 150 CopresenceDelegate* delegate_; // Belongs to the caller. |
124 TimedMap<std::string, bool> invalid_audio_token_cache_; | 151 TimedMap<std::string, bool> invalid_audio_token_cache_; |
125 PostCallback server_post_callback_; | 152 PostCallback server_post_callback_; |
126 | 153 |
127 std::string device_id_; | 154 std::map<std::string, std::string> device_id_by_auth_token_; |
128 scoped_ptr<DirectiveHandler> directive_handler_; | 155 scoped_ptr<DirectiveHandler> directive_handler_; |
129 std::set<HttpPost*> pending_posts_; | 156 std::set<HttpPost*> pending_posts_; |
130 | 157 |
131 DISALLOW_COPY_AND_ASSIGN(RpcHandler); | 158 DISALLOW_COPY_AND_ASSIGN(RpcHandler); |
132 }; | 159 }; |
133 | 160 |
134 } // namespace copresence | 161 } // namespace copresence |
135 | 162 |
136 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ | 163 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ |
OLD | NEW |