ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / copresence / handlers / directive_handler_impl.cc
blob0ee56d81fc986aac12bcb1d9b8d5cf88964bc2da
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 const DirectivesCallback& update_directives_callback)
26 : DirectiveHandlerImpl(update_directives_callback,
27 make_scoped_ptr(new AudioDirectiveHandlerImpl(
28 update_directives_callback))) {}
30 DirectiveHandlerImpl::DirectiveHandlerImpl(
31 const DirectivesCallback& update_directives_callback,
32 scoped_ptr<AudioDirectiveHandler> audio_handler)
33 : audio_handler_(audio_handler.Pass()),
34 is_started_(false) {}
36 DirectiveHandlerImpl::~DirectiveHandlerImpl() {}
38 void DirectiveHandlerImpl::Start(
39 audio_modem::WhispernetClient* whispernet_client,
40 const audio_modem::TokensCallback& tokens_cb) {
41 audio_handler_->Initialize(whispernet_client, tokens_cb);
42 DVLOG(2) << "Directive handler starting";
44 is_started_ = true;
46 // Run all the queued directives.
47 for (const auto& op_id : pending_directives_) {
48 for (const Directive& directive : op_id.second)
49 StartDirective(op_id.first, directive);
51 pending_directives_.clear();
54 void DirectiveHandlerImpl::AddDirective(const Directive& original_directive) {
55 // We may need to modify the directive's TTL.
56 Directive directive(original_directive);
58 // We only handle transmit and receive directives.
59 // WiFi and BLE scans aren't implemented.
60 DCHECK_EQ(directive.instruction_type(), TOKEN);
62 std::string op_id = directive.published_message_id();
63 if (op_id.empty())
64 op_id = directive.subscription_id();
66 // GCM directives will not have a publish or subscribe ID populated.
67 if (op_id.empty()) {
68 op_id = base::GenerateGUID();
69 DVLOG(3) << "No operation associated with directive. Setting op id to "
70 << op_id;
72 // The app can't cancel these directives, so make sure they're not too long.
73 if (directive.ttl_millis() > kMaxUnlabeledDirectiveTtl) {
74 DVLOG(2) << "Cutting TTL of unlabeled directive from "
75 << directive.ttl_millis() << " down to "
76 << kMaxUnlabeledDirectiveTtl << " milliseconds";
77 directive.set_ttl_millis(kMaxUnlabeledDirectiveTtl);
81 if (!is_started_) {
82 pending_directives_[op_id].push_back(directive);
83 } else {
84 StartDirective(op_id, directive);
88 void DirectiveHandlerImpl::RemoveDirectives(const std::string& op_id) {
89 // If whispernet_client_ is null, audio_handler_ hasn't been Initialized.
90 if (is_started_) {
91 audio_handler_->RemoveInstructions(op_id);
92 } else {
93 pending_directives_.erase(op_id);
97 const std::string DirectiveHandlerImpl::GetCurrentAudioToken(
98 audio_modem::AudioType type) const {
99 // If whispernet_client_ is null, audio_handler_ hasn't been Initialized.
100 return is_started_ ? audio_handler_->PlayingToken(type) : "";
103 bool DirectiveHandlerImpl::IsAudioTokenHeard(
104 audio_modem::AudioType type) const {
105 return is_started_ ? audio_handler_->IsPlayingTokenHeard(type) : false;
109 // Private functions.
111 void DirectiveHandlerImpl::StartDirective(const std::string& op_id,
112 const Directive& directive) {
113 DCHECK(is_started_);
114 DLOG_IF(WARNING, directive.delay_millis() > 0)
115 << "Ignoring " << directive.delay_millis() << " delay for directive";
116 const TokenMedium& medium = directive.token_instruction().medium();
117 DCHECK(medium == AUDIO_ULTRASOUND_PASSBAND || medium == AUDIO_AUDIBLE_DTMF)
118 << "Received directive for unimplemented medium " << medium;
119 audio_handler_->AddInstruction(directive, op_id);
122 } // namespace copresence