Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: components/copresence/rpc/rpc_handler.h

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

Powered by Google App Engine
This is Rietveld 408576698