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>
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"
25 class URLContextGetter
;
28 namespace copresence
{
30 class DirectiveHandler
;
34 class SubscribedMessage
;
35 class WhispernetClient
;
37 // The implementation for CopresenceManager. Responsible primarily for
38 // client-side initialization. The RpcHandler handles all the details
39 // of interacting with the server.
40 // TODO(ckehoe, rkc): Add tests for this class.
41 class CopresenceManagerImpl
: public CopresenceManager
{
43 // The delegate is owned by the caller, and must outlive the manager.
44 explicit CopresenceManagerImpl(CopresenceDelegate
* delegate
);
46 ~CopresenceManagerImpl() override
;
48 // CopresenceManager overrides.
49 CopresenceState
* state() override
;
50 void ExecuteReportRequest(const ReportRequest
& request
,
51 const std::string
& app_id
,
52 const std::string
& auth_token
,
53 const StatusCallback
& callback
) override
;
56 // Complete initialization when Whispernet is available.
57 void WhispernetInitComplete(bool success
);
59 // Handle tokens decoded by Whispernet.
60 // TODO(ckehoe): Replace AudioToken with ReceivedToken.
61 void ReceivedTokens(const std::vector
<AudioToken
>& tokens
);
63 // Verifies that we can hear the audio we're playing.
64 // This gets called every kAudioCheckIntervalMs milliseconds.
67 // This gets called every kPollTimerIntervalMs milliseconds
68 // to poll the server for new messages.
69 void PollForMessages();
71 // Send SubscribedMessages to the appropriate clients.
72 void DispatchMessages(
73 const google::protobuf::RepeatedPtrField
<SubscribedMessage
>&
76 // Belongs to the caller.
77 CopresenceDelegate
* const delegate_
;
79 // We use a CancelableCallback here because Whispernet
80 // does not provide a way to unregister its init callback.
81 base::CancelableCallback
<void(bool)> whispernet_init_callback_
;
85 // This order is required because each object
86 // makes calls to those listed before it.
87 scoped_ptr
<CopresenceStateImpl
> state_
;
88 scoped_ptr
<RpcHandler
> rpc_handler_
;
89 scoped_ptr
<DirectiveHandler
> directive_handler_
;
90 scoped_ptr
<GCMHandler
> gcm_handler_
;
92 scoped_ptr
<base::Timer
> poll_timer_
;
93 scoped_ptr
<base::Timer
> audio_check_timer_
;
95 TimedMap
<std::string
, google::protobuf::RepeatedPtrField
<SubscribedMessage
>>
96 queued_messages_by_token_
;
98 DISALLOW_COPY_AND_ASSIGN(CopresenceManagerImpl
);
101 } // namespace copresence
103 #endif // COMPONENTS_COPRESENCE_COPRESENCE_MANAGER_IMPL_H_