Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: components/copresence/handlers/directive_handler.cc

Issue 718303002: GCM Directive fix (M40 merge) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2214
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/copresence/handlers/directive_handler_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
OLDNEW
« no previous file with comments | « no previous file | components/copresence/handlers/directive_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698