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 #include "components/copresence/copresence_manager_impl.h" | 5 #include "components/copresence/copresence_manager_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/time/time.h" | |
12 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
13 #include "components/copresence/copresence_state_impl.h" | |
14 #include "components/copresence/handlers/directive_handler_impl.h" | 12 #include "components/copresence/handlers/directive_handler_impl.h" |
15 #include "components/copresence/handlers/gcm_handler_impl.h" | 13 #include "components/copresence/handlers/gcm_handler_impl.h" |
16 #include "components/copresence/proto/rpcs.pb.h" | 14 #include "components/copresence/proto/rpcs.pb.h" |
17 #include "components/copresence/public/whispernet_client.h" | 15 #include "components/copresence/public/whispernet_client.h" |
18 #include "components/copresence/rpc/rpc_handler.h" | 16 #include "components/copresence/rpc/rpc_handler.h" |
19 | 17 |
20 namespace { | 18 namespace { |
21 | 19 |
22 const int kPollTimerIntervalMs = 3000; // milliseconds. | 20 const int kPollTimerIntervalMs = 3000; // milliseconds. |
23 const int kAudioCheckIntervalMs = 1000; // milliseconds. | 21 const int kAudioCheckIntervalMs = 1000; // milliseconds. |
24 | 22 |
25 } // namespace | 23 } // namespace |
26 | 24 |
27 namespace copresence { | 25 namespace copresence { |
28 | 26 |
29 // Public functions. | 27 // Public functions. |
30 | 28 |
31 CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate) | 29 CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate) |
32 : delegate_(delegate), | 30 : delegate_(delegate), |
33 whispernet_init_callback_(base::Bind( | 31 whispernet_init_callback_(base::Bind( |
34 &CopresenceManagerImpl::WhispernetInitComplete, | 32 &CopresenceManagerImpl::WhispernetInitComplete, |
35 // This callback gets cancelled when we are destroyed. | 33 // This callback gets cancelled when we are destroyed. |
36 base::Unretained(this))), | 34 base::Unretained(this))), |
37 init_failed_(false), | 35 init_failed_(false), |
38 state_(new CopresenceStateImpl), | 36 directive_handler_(new DirectiveHandlerImpl), |
39 directive_handler_(new DirectiveHandlerImpl( | |
40 // The directive handler and its descendants | |
41 // will be destructed before the CopresenceState instance. | |
42 base::Bind(&CopresenceStateImpl::UpdateDirectives, | |
43 base::Unretained(state_.get())))), | |
44 poll_timer_(new base::RepeatingTimer<CopresenceManagerImpl>), | 37 poll_timer_(new base::RepeatingTimer<CopresenceManagerImpl>), |
45 audio_check_timer_(new base::RepeatingTimer<CopresenceManagerImpl>) { | 38 audio_check_timer_(new base::RepeatingTimer<CopresenceManagerImpl>) { |
46 DCHECK(delegate_); | 39 DCHECK(delegate_); |
47 DCHECK(delegate_->GetWhispernetClient()); | 40 DCHECK(delegate_->GetWhispernetClient()); |
48 delegate_->GetWhispernetClient()->Initialize( | 41 delegate_->GetWhispernetClient()->Initialize( |
49 whispernet_init_callback_.callback()); | 42 whispernet_init_callback_.callback()); |
50 | 43 |
51 if (delegate->GetGCMDriver()) | 44 if (delegate->GetGCMDriver()) |
52 gcm_handler_.reset(new GCMHandlerImpl(delegate->GetGCMDriver(), | 45 gcm_handler_.reset(new GCMHandlerImpl(delegate->GetGCMDriver(), |
53 directive_handler_.get())); | 46 directive_handler_.get())); |
54 | 47 |
55 rpc_handler_.reset(new RpcHandler(delegate, | 48 rpc_handler_.reset(new RpcHandler(delegate, |
56 state_.get(), | |
57 directive_handler_.get(), | 49 directive_handler_.get(), |
58 gcm_handler_.get())); | 50 gcm_handler_.get())); |
59 } | 51 } |
60 | 52 |
61 CopresenceManagerImpl::~CopresenceManagerImpl() { | 53 CopresenceManagerImpl::~CopresenceManagerImpl() { |
62 whispernet_init_callback_.Cancel(); | 54 whispernet_init_callback_.Cancel(); |
63 } | 55 } |
64 | 56 |
65 CopresenceState* CopresenceManagerImpl::state() { | |
66 return state_.get(); | |
67 } | |
68 | |
69 // Returns false if any operations were malformed. | 57 // Returns false if any operations were malformed. |
70 void CopresenceManagerImpl::ExecuteReportRequest( | 58 void CopresenceManagerImpl::ExecuteReportRequest( |
71 const ReportRequest& request, | 59 const ReportRequest& request, |
72 const std::string& app_id, | 60 const std::string& app_id, |
73 const std::string& auth_token, | 61 const std::string& auth_token, |
74 const StatusCallback& callback) { | 62 const StatusCallback& callback) { |
75 // If initialization has failed, reject all requests. | 63 // If initialization has failed, reject all requests. |
76 if (init_failed_) { | 64 if (init_failed_) { |
77 callback.Run(FAIL); | 65 callback.Run(FAIL); |
78 return; | 66 return; |
79 } | 67 } |
80 | 68 |
81 // We'll need to modify the ReportRequest, so we make our own copy to send. | 69 // We'll need to modify the ReportRequest, so we make our own copy to send. |
82 scoped_ptr<ReportRequest> request_copy(new ReportRequest(request)); | 70 scoped_ptr<ReportRequest> request_copy(new ReportRequest(request)); |
83 rpc_handler_->SendReportRequest( | 71 rpc_handler_->SendReportRequest( |
84 request_copy.Pass(), app_id, auth_token, callback); | 72 request_copy.Pass(), app_id, auth_token, callback); |
85 } | 73 } |
86 | 74 |
87 | 75 |
88 // Private functions. | 76 // Private functions. |
89 | 77 |
90 void CopresenceManagerImpl::WhispernetInitComplete(bool success) { | 78 void CopresenceManagerImpl::WhispernetInitComplete(bool success) { |
91 if (success) { | 79 if (success) { |
92 DVLOG(3) << "Whispernet initialized successfully."; | 80 DVLOG(3) << "Whispernet initialized successfully."; |
93 | 81 |
| 82 // We destroy |directive_handler_| before |rpc_handler_|, hence passing |
| 83 // in |rpc_handler_|'s pointer is safe here. |
94 directive_handler_->Start(delegate_->GetWhispernetClient(), | 84 directive_handler_->Start(delegate_->GetWhispernetClient(), |
95 base::Bind(&CopresenceManagerImpl::ReceivedTokens, | 85 base::Bind(&RpcHandler::ReportTokens, |
96 base::Unretained(this))); | 86 base::Unretained(rpc_handler_.get()))); |
97 | 87 |
98 // Start up timers. | 88 // Start up timers. |
99 poll_timer_->Start(FROM_HERE, | 89 poll_timer_->Start(FROM_HERE, |
100 base::TimeDelta::FromMilliseconds(kPollTimerIntervalMs), | 90 base::TimeDelta::FromMilliseconds(kPollTimerIntervalMs), |
101 base::Bind(&CopresenceManagerImpl::PollForMessages, | 91 base::Bind(&CopresenceManagerImpl::PollForMessages, |
102 base::Unretained(this))); | 92 base::Unretained(this))); |
103 audio_check_timer_->Start( | 93 audio_check_timer_->Start( |
104 FROM_HERE, base::TimeDelta::FromMilliseconds(kAudioCheckIntervalMs), | 94 FROM_HERE, base::TimeDelta::FromMilliseconds(kAudioCheckIntervalMs), |
105 base::Bind(&CopresenceManagerImpl::AudioCheck, base::Unretained(this))); | 95 base::Bind(&CopresenceManagerImpl::AudioCheck, base::Unretained(this))); |
106 } else { | 96 } else { |
107 LOG(ERROR) << "Whispernet initialization failed!"; | 97 LOG(ERROR) << "Whispernet initialization failed!"; |
108 init_failed_ = true; | 98 init_failed_ = true; |
109 } | 99 } |
110 } | 100 } |
111 | 101 |
112 void CopresenceManagerImpl::ReceivedTokens( | |
113 const std::vector<AudioToken>& tokens) { | |
114 rpc_handler_->ReportTokens(tokens); | |
115 | |
116 // Update the CopresenceState. | |
117 for (const AudioToken audio_token : tokens) { | |
118 DVLOG(3) << "Heard token: " << audio_token.token; | |
119 ReceivedToken token( | |
120 audio_token.token, | |
121 audio_token.audible ? AUDIO_AUDIBLE_DTMF : AUDIO_ULTRASOUND_PASSBAND, | |
122 base::Time::Now()); | |
123 state_->UpdateReceivedToken(token); | |
124 } | |
125 } | |
126 | |
127 void CopresenceManagerImpl::PollForMessages() { | 102 void CopresenceManagerImpl::PollForMessages() { |
128 // Report our currently playing tokens. | 103 // Report our currently playing tokens. |
129 const std::string& audible_token = | 104 const std::string& audible_token = |
130 directive_handler_->GetCurrentAudioToken(AUDIBLE); | 105 directive_handler_->GetCurrentAudioToken(AUDIBLE); |
131 const std::string& inaudible_token = | 106 const std::string& inaudible_token = |
132 directive_handler_->GetCurrentAudioToken(INAUDIBLE); | 107 directive_handler_->GetCurrentAudioToken(INAUDIBLE); |
133 | 108 |
134 std::vector<AudioToken> tokens; | 109 std::vector<AudioToken> tokens; |
135 if (!audible_token.empty()) | 110 if (!audible_token.empty()) |
136 tokens.push_back(AudioToken(audible_token, true)); | 111 tokens.push_back(AudioToken(audible_token, true)); |
137 if (!inaudible_token.empty()) | 112 if (!inaudible_token.empty()) |
138 tokens.push_back(AudioToken(inaudible_token, false)); | 113 tokens.push_back(AudioToken(inaudible_token, false)); |
139 | 114 |
140 if (!tokens.empty()) | 115 if (!tokens.empty()) |
141 rpc_handler_->ReportTokens(tokens); | 116 rpc_handler_->ReportTokens(tokens); |
142 } | 117 } |
143 | 118 |
144 void CopresenceManagerImpl::AudioCheck() { | 119 void CopresenceManagerImpl::AudioCheck() { |
145 if (!directive_handler_->GetCurrentAudioToken(AUDIBLE).empty() && | 120 if (!directive_handler_->GetCurrentAudioToken(AUDIBLE).empty() && |
146 !directive_handler_->IsAudioTokenHeard(AUDIBLE)) { | 121 !directive_handler_->IsAudioTokenHeard(AUDIBLE)) { |
147 delegate_->HandleStatusUpdate(AUDIO_FAIL); | 122 delegate_->HandleStatusUpdate(AUDIO_FAIL); |
148 } else if (!directive_handler_->GetCurrentAudioToken(INAUDIBLE).empty() && | 123 } else if (!directive_handler_->GetCurrentAudioToken(INAUDIBLE).empty() && |
149 !directive_handler_->IsAudioTokenHeard(INAUDIBLE)) { | 124 !directive_handler_->IsAudioTokenHeard(INAUDIBLE)) { |
150 delegate_->HandleStatusUpdate(AUDIO_FAIL); | 125 delegate_->HandleStatusUpdate(AUDIO_FAIL); |
151 } | 126 } |
152 } | 127 } |
153 | 128 |
154 } // namespace copresence | 129 } // namespace copresence |
OLD | NEW |