Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / copresence / handlers / directive_handler_impl.cc
blob854a5c9fe8dc90cd39e6df75324cac6422c1c614
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 #include "components/copresence/handlers/directive_handler_impl.h"
7 #include "base/bind.h"
8 #include "base/guid.h"
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h"
12 #include "components/copresence/proto/data.pb.h"
14 namespace {
16 const int kMaxUnlabeledDirectiveTtl = 60000; // 1 minute
18 } // namespace
20 namespace copresence {
22 // Public functions.
24 DirectiveHandlerImpl::DirectiveHandlerImpl()
25 : DirectiveHandlerImpl(make_scoped_ptr(new AudioDirectiveHandlerImpl(
26 DirectivesCallback()))) {}
28 DirectiveHandlerImpl::DirectiveHandlerImpl(
29 const DirectivesCallback& update_directives_callback)
30 : DirectiveHandlerImpl(make_scoped_ptr(new AudioDirectiveHandlerImpl(
31 update_directives_callback))) {}
33 DirectiveHandlerImpl::DirectiveHandlerImpl(
34 scoped_ptr<AudioDirectiveHandler> audio_handler)
35 : audio_handler_(audio_handler.Pass()),
36 is_started_(false) {}
38 DirectiveHandlerImpl::~DirectiveHandlerImpl() {}
40 void DirectiveHandlerImpl::Start(
41 audio_modem::WhispernetClient* whispernet_client,
42 const audio_modem::TokensCallback& tokens_cb) {
43 audio_handler_->Initialize(whispernet_client, tokens_cb);
44 DVLOG(2) << "Directive handler starting";
46 is_started_ = true;
48 // Run all the queued directives.
49 for (const auto& op_id : pending_directives_) {
50 for (const Directive& directive : op_id.second)
51 StartDirective(op_id.first, directive);
53 pending_directives_.clear();
56 void DirectiveHandlerImpl::AddDirective(const Directive& original_directive) {
57 // We may need to modify the directive's TTL.
58 Directive directive(original_directive);
60 // We only handle transmit and receive directives.
61 // WiFi and BLE scans aren't implemented.
62 DCHECK_EQ(directive.instruction_type(), TOKEN);
64 std::string op_id = directive.published_message_id();
65 if (op_id.empty())
66 op_id = directive.subscription_id();
68 // GCM directives will not have a publish or subscribe ID populated.
69 if (op_id.empty()) {
70 op_id = base::GenerateGUID();
71 DVLOG(3) << "No operation associated with directive. Setting op id to "
72 << op_id;
74 // The app can't cancel these directives, so make sure they're not too long.
75 if (directive.ttl_millis() > kMaxUnlabeledDirectiveTtl) {
76 DVLOG(2) << "Cutting TTL of unlabeled directive from "
77 << directive.ttl_millis() << " down to "
78 << kMaxUnlabeledDirectiveTtl << " milliseconds";
79 directive.set_ttl_millis(kMaxUnlabeledDirectiveTtl);
83 if (!is_started_) {
84 pending_directives_[op_id].push_back(directive);
85 } else {
86 StartDirective(op_id, directive);
90 void DirectiveHandlerImpl::RemoveDirectives(const std::string& op_id) {
91 // If whispernet_client_ is null, audio_handler_ hasn't been Initialized.
92 if (is_started_) {
93 audio_handler_->RemoveInstructions(op_id);
94 } else {
95 pending_directives_.erase(op_id);
99 const std::string DirectiveHandlerImpl::GetCurrentAudioToken(
100 audio_modem::AudioType type) const {
101 // If whispernet_client_ is null, audio_handler_ hasn't been Initialized.
102 return is_started_ ? audio_handler_->PlayingToken(type) : "";
105 bool DirectiveHandlerImpl::IsAudioTokenHeard(
106 audio_modem::AudioType type) const {
107 return is_started_ ? audio_handler_->IsPlayingTokenHeard(type) : false;
111 // Private functions.
113 void DirectiveHandlerImpl::StartDirective(const std::string& op_id,
114 const Directive& directive) {
115 DCHECK(is_started_);
116 DLOG_IF(WARNING, directive.delay_millis() > 0)
117 << "Ignoring " << directive.delay_millis() << " delay for directive";
118 const TokenMedium& medium = directive.token_instruction().medium();
119 DCHECK(medium == AUDIO_ULTRASOUND_PASSBAND || medium == AUDIO_AUDIBLE_DTMF)
120 << "Received directive for unimplemented medium " << medium;
121 audio_handler_->AddInstruction(directive, op_id);
124 } // namespace copresence