Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/notification/pull_channel.h" |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 6 | |
| 7 | #include <base/bind.h> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 8 | #include <base/location.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 9 | #include <weave/provider/task_runner.h> |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 10 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 11 | #include "src/notification/notification_delegate.h" |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 12 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 13 | namespace weave { |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 15 | const char kPullChannelName[] = "pull"; |
| 16 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 17 | PullChannel::PullChannel(base::TimeDelta pull_interval, |
| 18 | provider::TaskRunner* task_runner) |
Vitaly Buka | bf415b2 | 2015-08-12 21:30:30 -0700 | [diff] [blame] | 19 | : pull_interval_{pull_interval}, task_runner_{task_runner} {} |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 20 | |
| 21 | std::string PullChannel::GetName() const { |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 22 | return kPullChannelName; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 25 | bool PullChannel::IsConnected() const { |
| 26 | return true; |
| 27 | } |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 28 | |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 29 | void PullChannel::AddChannelParameters(base::DictionaryValue* channel_json) { |
| 30 | // No extra parameters needed for "Pull" channel. |
| 31 | } |
| 32 | |
| 33 | void PullChannel::Start(NotificationDelegate* delegate) { |
| 34 | CHECK(delegate); |
| 35 | delegate_ = delegate; |
Vitaly Buka | bf415b2 | 2015-08-12 21:30:30 -0700 | [diff] [blame] | 36 | RePost(); |
| 37 | } |
| 38 | |
| 39 | void PullChannel::RePost() { |
| 40 | CHECK(delegate_); |
| 41 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 42 | task_runner_->PostDelayedTask( |
| 43 | FROM_HERE, |
| 44 | base::Bind(&PullChannel::OnTimer, weak_ptr_factory_.GetWeakPtr()), |
| 45 | pull_interval_); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void PullChannel::Stop() { |
| 49 | weak_ptr_factory_.InvalidateWeakPtrs(); |
Vitaly Buka | bf415b2 | 2015-08-12 21:30:30 -0700 | [diff] [blame] | 50 | delegate_ = nullptr; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void PullChannel::UpdatePullInterval(base::TimeDelta pull_interval) { |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 54 | pull_interval_ = pull_interval; |
Vitaly Buka | bf415b2 | 2015-08-12 21:30:30 -0700 | [diff] [blame] | 55 | if (delegate_) |
| 56 | RePost(); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void PullChannel::OnTimer() { |
Vitaly Buka | bf415b2 | 2015-08-12 21:30:30 -0700 | [diff] [blame] | 60 | // Repost before delegate notification to give it a chance to stop channel. |
| 61 | RePost(); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 62 | base::DictionaryValue empty_dict; |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 63 | delegate_->OnCommandCreated(empty_dict, GetName()); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 66 | } // namespace weave |