| 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 <google/protobuf/repeated_field.h> | 8 #include <google/protobuf/repeated_field.h> |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 class DirectiveHandler; | 24 class DirectiveHandler; |
| 25 class ReportRequest; | 25 class ReportRequest; |
| 26 class RequestHeader; | 26 class RequestHeader; |
| 27 class SubscribedMessage; | 27 class SubscribedMessage; |
| 28 | 28 |
| 29 // This class currently handles all communication with the copresence server. | 29 // This class currently handles all communication with the copresence server. |
| 30 class RpcHandler { | 30 class RpcHandler { |
| 31 public: | 31 public: |
| 32 // A callback to indicate whether handler initialization succeeded. | 32 // A callback to indicate whether handler initialization succeeded. |
| 33 typedef base::Callback<void(bool)> SuccessCallback; | 33 typedef base::Callback<void(bool)> SuccessCallback; |
| 34 typedef base::Callback<void(HttpPost*, int, const std::string&)> PostCallback; |
| 34 | 35 |
| 35 // Report rpc name to send to Apiary. | 36 // Report rpc name to send to Apiary. |
| 36 static const char kReportRequestRpcName[]; | 37 static const char kReportRequestRpcName[]; |
| 37 | 38 |
| 38 // Constructor. |delegate| is owned by the caller, | 39 // Constructor. |delegate| is owned by the caller, |
| 39 // and must be valid as long as the RpcHandler exists. | 40 // and must be valid as long as the RpcHandler exists. |
| 40 explicit RpcHandler(CopresenceClientDelegate* delegate); | 41 explicit RpcHandler(CopresenceClientDelegate* delegate); |
| 41 | 42 |
| 42 virtual ~RpcHandler(); | 43 virtual ~RpcHandler(); |
| 43 | 44 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 62 // Callback to allow tests to stub out HTTP POST behavior. | 63 // Callback to allow tests to stub out HTTP POST behavior. |
| 63 // Arguments: | 64 // Arguments: |
| 64 // URLRequestContextGetter: Context for the HTTP POST request. | 65 // URLRequestContextGetter: Context for the HTTP POST request. |
| 65 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name | 66 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name |
| 66 // MessageLite: Contents of POST request to be sent. This needs to be | 67 // MessageLite: Contents of POST request to be sent. This needs to be |
| 67 // a (scoped) pointer to ease handling of the abstract MessageLite class. | 68 // a (scoped) pointer to ease handling of the abstract MessageLite class. |
| 68 // ResponseCallback: Receives the response to the request. | 69 // ResponseCallback: Receives the response to the request. |
| 69 typedef base::Callback<void(net::URLRequestContextGetter*, | 70 typedef base::Callback<void(net::URLRequestContextGetter*, |
| 70 const std::string&, | 71 const std::string&, |
| 71 scoped_ptr<google::protobuf::MessageLite>, | 72 scoped_ptr<google::protobuf::MessageLite>, |
| 72 const HttpPost::ResponseCallback&)> PostCallback; | 73 const PostCallback&)> SendPostCallback; |
| 73 | 74 |
| 74 friend class RpcHandlerTest; | 75 friend class RpcHandlerTest; |
| 75 | 76 |
| 76 void RegisterResponseHandler(const SuccessCallback& init_done_callback, | 77 void RegisterResponseHandler(const SuccessCallback& init_done_callback, |
| 78 HttpPost* completed_post, |
| 77 int http_status_code, | 79 int http_status_code, |
| 78 const std::string& response_data, | 80 const std::string& response_data); |
| 79 HttpPost* completed_post); | |
| 80 void ReportResponseHandler(const StatusCallback& status_callback, | 81 void ReportResponseHandler(const StatusCallback& status_callback, |
| 82 HttpPost* completed_post, |
| 81 int http_status_code, | 83 int http_status_code, |
| 82 const std::string& response_data, | 84 const std::string& response_data); |
| 83 HttpPost* completed_post); | |
| 84 | 85 |
| 85 void DispatchMessages( | 86 void DispatchMessages( |
| 86 const google::protobuf::RepeatedPtrField<SubscribedMessage>& | 87 const google::protobuf::RepeatedPtrField<SubscribedMessage>& |
| 87 subscribed_messages); | 88 subscribed_messages); |
| 88 | 89 |
| 89 RequestHeader* CreateRequestHeader(const std::string& client_name) const; | 90 RequestHeader* CreateRequestHeader(const std::string& client_name) const; |
| 90 | 91 |
| 91 template <class T> | 92 template <class T> |
| 92 void SendServerRequest(const std::string& rpc_name, | 93 void SendServerRequest(const std::string& rpc_name, |
| 93 const std::string& app_id, | 94 const std::string& app_id, |
| 94 scoped_ptr<T> request, | 95 scoped_ptr<T> request, |
| 95 const HttpPost::ResponseCallback& response_handler); | 96 const PostCallback& response_handler); |
| 96 | 97 |
| 97 // Wrapper for the http post constructor. This is the default way | 98 // Wrapper for the http post constructor. This is the default way |
| 98 // to contact the server, but it can be overridden for testing. | 99 // to contact the server, but it can be overridden for testing. |
| 99 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, | 100 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, |
| 100 const std::string& rpc_name, | 101 const std::string& rpc_name, |
| 101 scoped_ptr<google::protobuf::MessageLite> request_proto, | 102 scoped_ptr<google::protobuf::MessageLite> request_proto, |
| 102 const HttpPost::ResponseCallback& callback); | 103 const PostCallback& callback); |
| 103 | 104 |
| 104 // This method receives the request to encode a token and forwards it to | 105 // This method receives the request to encode a token and forwards it to |
| 105 // whispernet, setting the samples return callback to samples_callback. | 106 // whispernet, setting the samples return callback to samples_callback. |
| 106 void AudioDirectiveListToWhispernetConnector( | 107 void AudioDirectiveListToWhispernetConnector( |
| 107 const std::string& token, | 108 const std::string& token, |
| 109 bool audible, |
| 108 const WhispernetClient::SamplesCallback& samples_callback); | 110 const WhispernetClient::SamplesCallback& samples_callback); |
| 109 | 111 |
| 110 CopresenceClientDelegate* delegate_; // Belongs to the caller. | 112 CopresenceClientDelegate* delegate_; // Belongs to the caller. |
| 111 TimedMap<std::string, bool> invalid_audio_token_cache_; | 113 TimedMap<std::string, bool> invalid_audio_token_cache_; |
| 112 PostCallback server_post_callback_; | 114 SendPostCallback server_post_callback_; |
| 113 | 115 |
| 114 std::string device_id_; | 116 std::string device_id_; |
| 115 scoped_ptr<DirectiveHandler> directive_handler_; | 117 scoped_ptr<DirectiveHandler> directive_handler_; |
| 116 std::set<HttpPost*> pending_posts_; | 118 std::set<HttpPost*> pending_posts_; |
| 117 | 119 |
| 118 DISALLOW_COPY_AND_ASSIGN(RpcHandler); | 120 DISALLOW_COPY_AND_ASSIGN(RpcHandler); |
| 119 }; | 121 }; |
| 120 | 122 |
| 121 } // namespace copresence | 123 } // namespace copresence |
| 122 | 124 |
| 123 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ | 125 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ |
| OLD | NEW |