summaryrefslogtreecommitdiffstats
path: root/components/copresence/copresence_manager_impl.h
blob: 0fc32807e5569798cef2a4952e9fe581dd9d8c23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_COPRESENCE_COPRESENCE_MANAGER_IMPL_H_
#define COMPONENTS_COPRESENCE_COPRESENCE_MANAGER_IMPL_H_

#include <google/protobuf/repeated_field.h>

#include <string>
#include <vector>

#include "base/cancelable_callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "components/copresence/copresence_state_impl.h"
#include "components/copresence/public/copresence_manager.h"
#include "components/copresence/timed_map.h"

namespace audio_modem {
struct AudioToken;
}

namespace base {
class Timer;
}

namespace net {
class URLContextGetter;
}

namespace copresence {

class DirectiveHandler;
class GCMHandler;
class ReportRequest;
class RpcHandler;
class SubscribedMessage;

// The implementation for CopresenceManager. Responsible primarily for
// client-side initialization. The RpcHandler handles all the details
// of interacting with the server.
// TODO(ckehoe, rkc): Add tests for this class.
class CopresenceManagerImpl : public CopresenceManager {
 public:
  // The delegate is owned by the caller, and must outlive the manager.
  explicit CopresenceManagerImpl(CopresenceDelegate* delegate);

  ~CopresenceManagerImpl() override;

  // CopresenceManager overrides.
  CopresenceState* state() override;
  void ExecuteReportRequest(const ReportRequest& request,
                            const std::string& app_id,
                            const std::string& auth_token,
                            const StatusCallback& callback) override;

 private:
  // Complete initialization when Whispernet is available.
  void WhispernetInitComplete(bool success);

  // Handle tokens decoded by Whispernet.
  // TODO(ckehoe): Replace AudioToken with ReceivedToken.
  void ReceivedTokens(const std::vector<audio_modem::AudioToken>& tokens);

  // Verifies that we can hear the audio we're playing.
  // This gets called every kAudioCheckIntervalMs milliseconds.
  void AudioCheck();

  // This gets called every kPollTimerIntervalMs milliseconds
  // to poll the server for new messages.
  void PollForMessages();

  // Send SubscribedMessages to the appropriate clients.
  void DispatchMessages(
      const google::protobuf::RepeatedPtrField<SubscribedMessage>&
      subscribed_messages);

  // Belongs to the caller.
  CopresenceDelegate* const delegate_;

  // We use a CancelableCallback here because Whispernet
  // does not provide a way to unregister its init callback.
  base::CancelableCallback<void(bool)> whispernet_init_callback_;

  bool init_failed_;

  // The RpcHandler makes calls to the other objects here, so it must come last.
  scoped_ptr<CopresenceStateImpl> state_;
  scoped_ptr<DirectiveHandler> directive_handler_;
  scoped_ptr<GCMHandler> gcm_handler_;
  scoped_ptr<RpcHandler> rpc_handler_;

  scoped_ptr<base::Timer> poll_timer_;
  scoped_ptr<base::Timer> audio_check_timer_;

  TimedMap<std::string, google::protobuf::RepeatedPtrField<SubscribedMessage>>
  queued_messages_by_token_;

  DISALLOW_COPY_AND_ASSIGN(CopresenceManagerImpl);
};

}  // namespace copresence

#endif  // COMPONENTS_COPRESENCE_COPRESENCE_MANAGER_IMPL_H_