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"
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"
16 const int kMaxUnlabeledDirectiveTtl
= 60000; // 1 minute
20 namespace copresence
{
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()),
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";
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();
66 op_id
= directive
.subscription_id();
68 // GCM directives will not have a publish or subscribe ID populated.
70 op_id
= base::GenerateGUID();
71 DVLOG(3) << "No operation associated with directive. Setting op id to "
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
);
84 pending_directives_
[op_id
].push_back(directive
);
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.
93 audio_handler_
->RemoveInstructions(op_id
);
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
) {
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