OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/copresence/handlers/directive_handler.h" | 5 #include "components/copresence/handlers/directive_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/guid.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
10 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h" | 11 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h" |
11 #include "components/copresence/proto/data.pb.h" | 12 #include "components/copresence/proto/data.pb.h" |
12 | 13 |
| 14 namespace { |
| 15 |
| 16 const int kMaxUnlabeledDirectiveTtl = 60000; // 1 minute |
| 17 |
| 18 } // namespace |
| 19 |
13 namespace copresence { | 20 namespace copresence { |
14 | 21 |
15 // Public functions | 22 // Public functions |
16 | 23 |
17 DirectiveHandler::DirectiveHandler( | 24 DirectiveHandler::DirectiveHandler( |
18 scoped_ptr<AudioDirectiveHandler> audio_handler) | 25 scoped_ptr<AudioDirectiveHandler> audio_handler) |
19 : audio_handler_(audio_handler.Pass()), is_started_(false) { | 26 : audio_handler_(audio_handler.Pass()), is_started_(false) { |
20 } | 27 } |
21 | 28 |
22 DirectiveHandler::~DirectiveHandler() {} | 29 DirectiveHandler::~DirectiveHandler() {} |
23 | 30 |
24 void DirectiveHandler::Start(WhispernetClient* whispernet_client, | 31 void DirectiveHandler::Start(WhispernetClient* whispernet_client, |
25 const TokensCallback& tokens_cb) { | 32 const TokensCallback& tokens_cb) { |
26 audio_handler_->Initialize(whispernet_client, tokens_cb); | 33 audio_handler_->Initialize(whispernet_client, tokens_cb); |
| 34 DVLOG(2) << "Directive handler starting"; |
27 | 35 |
28 is_started_ = true; | 36 is_started_ = true; |
29 | 37 |
30 // Run all the queued directives. | 38 // Run all the queued directives. |
31 for (const auto& op_id : pending_directives_) { | 39 for (const auto& op_id : pending_directives_) { |
32 for (const Directive& directive : op_id.second) | 40 for (const Directive& directive : op_id.second) |
33 StartDirective(op_id.first, directive); | 41 StartDirective(op_id.first, directive); |
34 } | 42 } |
35 pending_directives_.clear(); | 43 pending_directives_.clear(); |
36 } | 44 } |
37 | 45 |
38 void DirectiveHandler::AddDirective(const Directive& directive) { | 46 void DirectiveHandler::AddDirective(const Directive& original_directive) { |
| 47 // We may need to modify the directive's TTL. |
| 48 Directive directive(original_directive); |
| 49 |
39 // We only handle transmit and receive directives. | 50 // We only handle transmit and receive directives. |
40 // WiFi and BLE scans aren't implemented. | 51 // WiFi and BLE scans aren't implemented. |
41 DCHECK_EQ(directive.instruction_type(), TOKEN); | 52 DCHECK_EQ(directive.instruction_type(), TOKEN); |
42 | 53 |
43 std::string op_id = directive.published_message_id(); | 54 std::string op_id = directive.published_message_id(); |
44 if (op_id.empty()) | 55 if (op_id.empty()) |
45 op_id = directive.subscription_id(); | 56 op_id = directive.subscription_id(); |
| 57 |
| 58 // GCM directives will not have a publish or subscribe ID populated. |
46 if (op_id.empty()) { | 59 if (op_id.empty()) { |
47 NOTREACHED() << "No operation associated with directive!"; | 60 op_id = base::GenerateGUID(); |
48 return; | 61 DVLOG(3) << "No operation associated with directive. Setting op id to " |
| 62 << op_id; |
| 63 |
| 64 // The app can't cancel these directives, so make sure they're not too long. |
| 65 if (directive.ttl_millis() > kMaxUnlabeledDirectiveTtl) { |
| 66 DVLOG(2) << "Cutting TTL of unlabeled directive from " |
| 67 << directive.ttl_millis() << " down to " |
| 68 << kMaxUnlabeledDirectiveTtl << " milliseconds"; |
| 69 directive.set_ttl_millis(kMaxUnlabeledDirectiveTtl); |
| 70 } |
49 } | 71 } |
50 | 72 |
51 if (!is_started_) { | 73 if (!is_started_) { |
52 pending_directives_[op_id].push_back(directive); | 74 pending_directives_[op_id].push_back(directive); |
53 } else { | 75 } else { |
54 StartDirective(op_id, directive); | 76 StartDirective(op_id, directive); |
55 } | 77 } |
56 } | 78 } |
57 | 79 |
58 void DirectiveHandler::RemoveDirectives(const std::string& op_id) { | 80 void DirectiveHandler::RemoveDirectives(const std::string& op_id) { |
(...skipping 25 matching lines...) Expand all Loading... |
84 audio_handler_->AddInstruction( | 106 audio_handler_->AddInstruction( |
85 ti, op_id, base::TimeDelta::FromMilliseconds(directive.ttl_millis())); | 107 ti, op_id, base::TimeDelta::FromMilliseconds(directive.ttl_millis())); |
86 } else { | 108 } else { |
87 // We should only get audio directives. | 109 // We should only get audio directives. |
88 NOTREACHED() << "Received directive for unimplemented medium " | 110 NOTREACHED() << "Received directive for unimplemented medium " |
89 << ti.medium(); | 111 << ti.medium(); |
90 } | 112 } |
91 } | 113 } |
92 | 114 |
93 } // namespace copresence | 115 } // namespace copresence |
OLD | NEW |