blob: 378351aceefa3ddc224c92e86ca5d4c021fe8b7c [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenkod05725f2015-05-27 15:48:19 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Stefan Sauer2d16dfa2015-09-25 17:08:35 +02005#include "src/notification/pull_channel.h"
Alex Vakulenkod05725f2015-05-27 15:48:19 -07006
7#include <base/bind.h>
Vitaly Buka0801a1f2015-08-14 10:03:46 -07008#include <base/location.h>
Vitaly Buka1e363672015-09-25 14:01:16 -07009#include <weave/provider/task_runner.h>
Alex Vakulenkod05725f2015-05-27 15:48:19 -070010
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020011#include "src/notification/notification_delegate.h"
Alex Vakulenkod05725f2015-05-27 15:48:19 -070012
Vitaly Bukab6f015a2015-07-09 14:59:23 -070013namespace weave {
Alex Vakulenkod05725f2015-05-27 15:48:19 -070014
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070015const char kPullChannelName[] = "pull";
16
Vitaly Buka1e363672015-09-25 14:01:16 -070017PullChannel::PullChannel(base::TimeDelta pull_interval,
18 provider::TaskRunner* task_runner)
Vitaly Bukabf415b22015-08-12 21:30:30 -070019 : pull_interval_{pull_interval}, task_runner_{task_runner} {}
Alex Vakulenkod05725f2015-05-27 15:48:19 -070020
21std::string PullChannel::GetName() const {
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070022 return kPullChannelName;
Alex Vakulenkod05725f2015-05-27 15:48:19 -070023}
24
Vitaly Bukaa647c852015-07-06 14:51:01 -070025bool PullChannel::IsConnected() const {
26 return true;
27}
Alex Vakulenko6b028ae2015-05-29 09:38:59 -070028
Alex Vakulenkod05725f2015-05-27 15:48:19 -070029void PullChannel::AddChannelParameters(base::DictionaryValue* channel_json) {
30 // No extra parameters needed for "Pull" channel.
31}
32
33void PullChannel::Start(NotificationDelegate* delegate) {
34 CHECK(delegate);
35 delegate_ = delegate;
Vitaly Bukabf415b22015-08-12 21:30:30 -070036 RePost();
37}
38
39void 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 Vakulenkod05725f2015-05-27 15:48:19 -070046}
47
48void PullChannel::Stop() {
49 weak_ptr_factory_.InvalidateWeakPtrs();
Vitaly Bukabf415b22015-08-12 21:30:30 -070050 delegate_ = nullptr;
Alex Vakulenkod05725f2015-05-27 15:48:19 -070051}
52
53void PullChannel::UpdatePullInterval(base::TimeDelta pull_interval) {
Alex Vakulenkod05725f2015-05-27 15:48:19 -070054 pull_interval_ = pull_interval;
Vitaly Bukabf415b22015-08-12 21:30:30 -070055 if (delegate_)
56 RePost();
Alex Vakulenkod05725f2015-05-27 15:48:19 -070057}
58
59void PullChannel::OnTimer() {
Vitaly Bukabf415b22015-08-12 21:30:30 -070060 // Repost before delegate notification to give it a chance to stop channel.
61 RePost();
Alex Vakulenkod05725f2015-05-27 15:48:19 -070062 base::DictionaryValue empty_dict;
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070063 delegate_->OnCommandCreated(empty_dict, GetName());
Alex Vakulenkod05725f2015-05-27 15:48:19 -070064}
65
Vitaly Bukab6f015a2015-07-09 14:59:23 -070066} // namespace weave