Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / copresence / copresence_manager_impl.h
blobbdb7ee133aa380a66535e7d1badcb29c9cfacc63
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.
5 #ifndef COMPONENTS_COPRESENCE_COPRESENCE_MANAGER_IMPL_H_
6 #define COMPONENTS_COPRESENCE_COPRESENCE_MANAGER_IMPL_H_
8 #include <google/protobuf/repeated_field.h>
10 #include <string>
11 #include <vector>
13 #include "base/cancelable_callback.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "components/copresence/copresence_state_impl.h"
17 #include "components/copresence/public/copresence_manager.h"
18 #include "components/copresence/timed_map.h"
20 namespace audio_modem {
21 struct AudioToken;
24 namespace base {
25 class Timer;
28 namespace net {
29 class URLContextGetter;
32 namespace copresence {
34 class DirectiveHandler;
35 class GCMHandler;
36 class ReportRequest;
37 class RpcHandler;
38 class SubscribedMessage;
40 // The implementation for CopresenceManager. Responsible primarily for
41 // client-side initialization. The RpcHandler handles all the details
42 // of interacting with the server.
43 // TODO(ckehoe, rkc): Add tests for this class.
44 class CopresenceManagerImpl : public CopresenceManager {
45 public:
46 // The delegate is owned by the caller, and must outlive the manager.
47 explicit CopresenceManagerImpl(CopresenceDelegate* delegate);
49 ~CopresenceManagerImpl() override;
51 // CopresenceManager overrides.
52 CopresenceState* state() override;
53 void ExecuteReportRequest(const ReportRequest& request,
54 const std::string& app_id,
55 const std::string& auth_token,
56 const StatusCallback& callback) override;
58 private:
59 // Complete initialization when Whispernet is available.
60 void WhispernetInitComplete(bool success);
62 // Handle tokens decoded by Whispernet.
63 // TODO(ckehoe): Replace AudioToken with ReceivedToken.
64 void ReceivedTokens(const std::vector<audio_modem::AudioToken>& tokens);
66 // Verifies that we can hear the audio we're playing.
67 // This gets called every kAudioCheckIntervalMs milliseconds.
68 void AudioCheck();
70 // This gets called every kPollTimerIntervalMs milliseconds
71 // to poll the server for new messages.
72 void PollForMessages();
74 // Send SubscribedMessages to the appropriate clients.
75 void DispatchMessages(
76 const google::protobuf::RepeatedPtrField<SubscribedMessage>&
77 subscribed_messages);
79 // Belongs to the caller.
80 CopresenceDelegate* const delegate_;
82 // We use a CancelableCallback here because Whispernet
83 // does not provide a way to unregister its init callback.
84 base::CancelableCallback<void(bool)> whispernet_init_callback_;
86 bool init_failed_;
88 // This order is required because each object
89 // makes calls to those listed before it.
90 scoped_ptr<CopresenceStateImpl> state_;
91 scoped_ptr<RpcHandler> rpc_handler_;
92 scoped_ptr<DirectiveHandler> directive_handler_;
93 scoped_ptr<GCMHandler> gcm_handler_;
95 scoped_ptr<base::Timer> poll_timer_;
96 scoped_ptr<base::Timer> audio_check_timer_;
98 TimedMap<std::string, google::protobuf::RepeatedPtrField<SubscribedMessage>>
99 queued_messages_by_token_;
101 DISALLOW_COPY_AND_ASSIGN(CopresenceManagerImpl);
104 } // namespace copresence
106 #endif // COMPONENTS_COPRESENCE_COPRESENCE_MANAGER_IMPL_H_