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> | |
9 | |
10 #include <set> | |
8 #include <string> | 11 #include <string> |
9 #include <vector> | 12 #include <vector> |
10 | 13 |
11 #include "base/callback.h" | 14 #include "base/callback.h" |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | |
17 #include "components/copresence/proto/enums.pb.h" | |
14 #include "components/copresence/public/copresence_client_delegate.h" | 18 #include "components/copresence/public/copresence_client_delegate.h" |
19 #include "components/copresence/public/whispernet_client.h" | |
20 #include "components/copresence/rpc/http_post.h" | |
21 #include "components/copresence/timed_map.h" | |
15 | 22 |
16 namespace copresence { | 23 namespace copresence { |
17 | 24 |
25 class DirectiveHandler; | |
18 class ReportRequest; | 26 class ReportRequest; |
19 class WhispernetClient; | 27 class RequestHeader; |
28 class SubscribedMessage; | |
20 | 29 |
21 // This class currently handles all communication with the copresence server. | 30 // This class currently handles all communication with the copresence server. |
22 // NOTE: This class is a stub. | 31 class RpcHandler : public base::SupportsWeakPtr<RpcHandler> { |
willchan no longer on Chromium
2014/08/07 20:27:05
My same comment about base::SupportsWeakPtr applie
Charlie
2014/08/07 22:40:58
Done. Using base::Unretained(this) instead, and re
| |
23 class RpcHandler { | |
24 public: | 32 public: |
25 // A callback to indicate whether handler initialization succeeded. | 33 // A callback to indicate whether handler initialization succeeded. |
26 typedef base::Callback<void(bool)> SuccessCallback; | 34 typedef base::Callback<void(bool)> SuccessCallback; |
27 | 35 |
28 // Constructor. May call the server to register a device, | 36 // Report rpc name to send to Apiary. |
29 // so completion status is indicated by the SuccessCallback. | 37 static const char kReportRequestRpcName[]; |
30 RpcHandler(CopresenceClientDelegate* delegate, | 38 |
31 SuccessCallback init_done_callback); | 39 // Constructor. |delegate| is owned by the caller, |
40 // and must be valid as long as the RpcHandler exists. | |
41 explicit RpcHandler(CopresenceClientDelegate* delegate); | |
32 | 42 |
33 virtual ~RpcHandler(); | 43 virtual ~RpcHandler(); |
34 | 44 |
45 // Clients must call this and wait for |init_done_callback| | |
46 // to be called before invoking any other methods. | |
47 void Initialize(const SuccessCallback& init_done_callback); | |
48 | |
35 // Send a report request | 49 // Send a report request |
36 void SendReportRequest(scoped_ptr<copresence::ReportRequest> request); | 50 void SendReportRequest(scoped_ptr<ReportRequest> request); |
37 void SendReportRequest(scoped_ptr<copresence::ReportRequest> request, | 51 void SendReportRequest(scoped_ptr<ReportRequest> request, |
38 const std::string& app_id, | 52 const std::string& app_id, |
39 const StatusCallback& callback); | 53 const StatusCallback& callback); |
40 | 54 |
41 // Report a set of tokens to the server for a given medium. | 55 // Report a set of tokens to the server for a given medium. |
42 void ReportTokens(copresence::TokenMedium medium, | 56 void ReportTokens(TokenMedium medium, const std::vector<std::string>& tokens); |
43 const std::vector<std::string>& tokens); | |
44 | 57 |
45 // Create the directive handler and connect it to the whispernet client. | 58 // Create the directive handler and connect it to |
46 void ConnectToWhispernet(WhispernetClient* whispernet_client); | 59 // the whispernet client specified by the delegate. |
60 void ConnectToWhispernet(); | |
47 | 61 |
48 // Disconnect the directive handler from the whispernet client. | 62 // Disconnect the directive handler from the whispernet client. |
49 void DisconnectFromWhispernet(); | 63 void DisconnectFromWhispernet(); |
50 | 64 |
51 private: | 65 private: |
66 // Callback to allow tests to stub out HTTP POST behavior. | |
67 // Arguments: | |
68 // URLRequestContextGetter: Context for the HTTP POST request. | |
69 // string: Name of the rpc to invoke. URL format: server.google.com/rpc_name | |
70 // MessageLite: Contents of POST request to be sent. This needs to be | |
71 // a (scoped) pointer to ease handling of the abstract MessageLite class. | |
72 // ResponseCallback: Receives the response to the request. | |
73 typedef base::Callback<void(net::URLRequestContextGetter*, | |
74 const std::string&, | |
75 scoped_ptr<google::protobuf::MessageLite>, | |
76 const HttpPost::ResponseCallback&)> PostCallback; | |
77 | |
78 friend class RpcHandlerTest; | |
79 | |
80 void RegisterResponseHandler(const SuccessCallback& init_done_callback, | |
81 int http_status_code, | |
82 const std::string& response_data, | |
83 HttpPost* completed_post); | |
84 void ReportResponseHandler(const StatusCallback& status_callback, | |
85 int http_status_code, | |
86 const std::string& response_data, | |
87 HttpPost* completed_post); | |
88 | |
89 void DispatchMessages( | |
90 const google::protobuf::RepeatedPtrField<SubscribedMessage>& | |
91 subscribed_messages); | |
92 | |
93 RequestHeader* CreateRequestHeader(const std::string& client_name) const; | |
94 | |
95 template <class T> | |
96 void SendServerRequest(const std::string& rpc_name, | |
97 const std::string& app_id, | |
98 scoped_ptr<T> request, | |
99 const HttpPost::ResponseCallback& response_handler); | |
100 | |
101 // Wrapper for the http post constructor. This is the default way | |
102 // to contact the server, but it can be overridden for testing. | |
103 void SendHttpPost(net::URLRequestContextGetter* url_context_getter, | |
104 const std::string& rpc_name, | |
105 scoped_ptr<google::protobuf::MessageLite> request_proto, | |
106 const HttpPost::ResponseCallback& callback); | |
107 | |
108 // This method receives the request to encode a token and forwards it to | |
109 // whispernet, setting the samples return callback to samples_callback. | |
110 void AudioDirectiveListToWhispernetConnector( | |
111 const std::string& token, | |
112 const WhispernetClient::SamplesCallback& samples_callback); | |
113 | |
114 CopresenceClientDelegate* delegate_; // Belongs to the caller. | |
115 TimedMap<std::string, bool> invalid_audio_token_cache_; | |
116 PostCallback server_post_callback_; | |
117 | |
118 std::string device_id_; | |
119 scoped_ptr<DirectiveHandler> directive_handler_; | |
120 std::set<HttpPost*> pending_posts_; | |
121 | |
52 DISALLOW_COPY_AND_ASSIGN(RpcHandler); | 122 DISALLOW_COPY_AND_ASSIGN(RpcHandler); |
53 }; | 123 }; |
54 | 124 |
55 } // namespace copresence | 125 } // namespace copresence |
56 | 126 |
57 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ | 127 #endif // COMPONENTS_COPRESENCE_RPC_RPC_HANDLER_H_ |
OLD | NEW |