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

Side by Side Diff: components/copresence/copresence_manager_impl.cc

Issue 684273004: Moving the DirectiveHandler to be owned by CopresenceManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Using EXPECT_THAT 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
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/copresence_manager_impl.h" 5 #include "components/copresence/copresence_manager_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "components/copresence/public/copresence_delegate.h" 9 #include "components/copresence/handlers/directive_handler.h"
10 #include "components/copresence/proto/rpcs.pb.h"
10 #include "components/copresence/public/whispernet_client.h" 11 #include "components/copresence/public/whispernet_client.h"
11 #include "components/copresence/rpc/rpc_handler.h" 12 #include "components/copresence/rpc/rpc_handler.h"
12 13
13 namespace { 14 namespace {
14 15
15 // Number of characters of suffix to log for auth tokens 16 // Number of characters of suffix to log for auth tokens
16 const int kTokenSuffix = 5; 17 const int kTokenSuffix = 5;
17 18
18 } // namespace 19 } // namespace
19 20
(...skipping 10 matching lines...) Expand all
30 31
31 PendingRequest::~PendingRequest() {} 32 PendingRequest::~PendingRequest() {}
32 33
33 // static 34 // static
34 scoped_ptr<CopresenceManager> CopresenceManager::Create( 35 scoped_ptr<CopresenceManager> CopresenceManager::Create(
35 CopresenceDelegate* delegate) { 36 CopresenceDelegate* delegate) {
36 return make_scoped_ptr(new CopresenceManagerImpl(delegate)); 37 return make_scoped_ptr(new CopresenceManagerImpl(delegate));
37 } 38 }
38 39
39 40
40 // Public methods 41 // Public functions
rkc 2014/10/31 17:18:06 Any reason why this is functions instead? They all
Charlie 2014/10/31 17:47:06 C++ has functions, not methods. http://stackoverf
41 42
42 CopresenceManagerImpl::~CopresenceManagerImpl() { 43 CopresenceManagerImpl::~CopresenceManagerImpl() {
43 whispernet_init_callback_.Cancel(); 44 whispernet_init_callback_.Cancel();
44 } 45 }
45 46
46 // Returns false if any operations were malformed. 47 // Returns false if any operations were malformed.
47 void CopresenceManagerImpl::ExecuteReportRequest( 48 void CopresenceManagerImpl::ExecuteReportRequest(
48 const ReportRequest& request, 49 const ReportRequest& request,
49 const std::string& app_id, 50 const std::string& app_id,
50 const StatusCallback& callback) { 51 const StatusCallback& callback) {
51 // If initialization has failed, reject all requests. 52 // If initialization has failed, reject all requests.
52 if (init_failed_) { 53 if (init_failed_) {
53 callback.Run(FAIL); 54 callback.Run(FAIL);
54 return; 55 return;
55 } 56 }
56 57
57 // Check if we are initialized enough to execute this request. 58 // Check if we are initialized enough to execute this request.
58 // If we haven't seen this auth token yet, we need to register for it. 59 // If we haven't seen this auth token yet, we need to register for it.
59 // TODO(ckehoe): Queue per device ID instead of globally. 60 // TODO(ckehoe): Queue per device ID instead of globally.
60 DCHECK(rpc_handler_);
61 const std::string& auth_token = delegate_->GetAuthToken(); 61 const std::string& auth_token = delegate_->GetAuthToken();
62 if (!rpc_handler_->IsRegisteredForToken(auth_token)) { 62 if (!rpc_handler_->IsRegisteredForToken(auth_token)) {
63 std::string token_str = auth_token.empty() ? "(anonymous)" : 63 std::string token_str = auth_token.empty() ? "(anonymous)" :
64 base::StringPrintf("(token ...%s)", 64 base::StringPrintf("(token ...%s)",
65 auth_token.substr(auth_token.length() - kTokenSuffix, 65 auth_token.substr(auth_token.length() - kTokenSuffix,
66 kTokenSuffix).c_str()); 66 kTokenSuffix).c_str());
67 rpc_handler_->RegisterForToken( 67 rpc_handler_->RegisterForToken(
68 auth_token, 68 auth_token,
69 // The manager owns the RpcHandler, so this callback cannot outlive us. 69 // The manager owns the RpcHandler, so this callback cannot outlive us.
70 base::Bind(&CopresenceManagerImpl::InitStepComplete, 70 base::Bind(&CopresenceManagerImpl::InitStepComplete,
71 base::Unretained(this), 71 base::Unretained(this),
72 "Device registration " + token_str)); 72 "Device registration " + token_str));
73 pending_init_operations_++; 73 pending_init_operations_++;
74 } 74 }
75 75
76 // Execute the request if possible, or queue it 76 // Execute the request if possible, or queue it
77 // if initialization is still in progress. 77 // if initialization is still in progress.
78 if (pending_init_operations_) { 78 if (pending_init_operations_) {
79 pending_requests_queue_.push_back( 79 pending_requests_queue_.push_back(
80 new PendingRequest(request, app_id, auth_token, callback)); 80 new PendingRequest(request, app_id, auth_token, callback));
81 } else { 81 } else {
82 rpc_handler_->SendReportRequest( 82 rpc_handler_->SendReportRequest(
83 make_scoped_ptr(new ReportRequest(request)), 83 make_scoped_ptr(new ReportRequest(request)),
84 app_id, 84 app_id,
85 auth_token, 85 auth_token,
86 callback); 86 callback);
87 } 87 }
88 } 88 }
89 89
90 // Private methods 90
91 // Private functions
91 92
92 CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate) 93 CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate)
93 : init_failed_(false), 94 : delegate_(delegate),
95 pending_init_operations_(0),
94 // This callback gets cancelled when we are destroyed. 96 // This callback gets cancelled when we are destroyed.
95 whispernet_init_callback_( 97 whispernet_init_callback_(
96 base::Bind(&CopresenceManagerImpl::InitStepComplete, 98 base::Bind(&CopresenceManagerImpl::InitStepComplete,
97 base::Unretained(this), 99 base::Unretained(this),
98 "Whispernet proxy initialization")), 100 "Whispernet proxy initialization")),
99 pending_init_operations_(0), 101 init_failed_(false),
100 delegate_(delegate), 102 rpc_handler_(new RpcHandler(delegate, directive_handler_.get())) {
101 rpc_handler_(new RpcHandler(delegate)) {
102 DCHECK(delegate); 103 DCHECK(delegate);
103 DCHECK(delegate->GetWhispernetClient()); 104 DCHECK(delegate->GetWhispernetClient());
104 105
105 delegate->GetWhispernetClient()->Initialize( 106 delegate->GetWhispernetClient()->Initialize(
106 whispernet_init_callback_.callback()); 107 whispernet_init_callback_.callback());
107 pending_init_operations_++; 108 pending_init_operations_++;
108 } 109 }
109 110
110 void CopresenceManagerImpl::CompleteInitialization() { 111 void CopresenceManagerImpl::CompleteInitialization() {
111 if (pending_init_operations_) 112 if (pending_init_operations_)
112 return; 113 return;
113 114
114 DCHECK(rpc_handler_.get()); 115 if (!init_failed_) {
115 if (!init_failed_) 116 // When RpcHandler is destroyed, it disconnects this callback.
116 rpc_handler_->ConnectToWhispernet(); 117 // TODO(ckehoe): Use a CancelableCallback instead.
118 delegate_->GetWhispernetClient()->RegisterTokensCallback(
119 base::Bind(&RpcHandler::ReportTokens,
120 base::Unretained(rpc_handler_.get())));
121 directive_handler_->Start(delegate_->GetWhispernetClient());
122 }
117 123
118 // Not const because SendReportRequest takes ownership of the ReportRequests. 124 // Not const because SendReportRequest takes ownership of the ReportRequests.
119 // This is ok though, as the entire queue is deleted afterwards. 125 // This is ok though, as the entire queue is deleted afterwards.
120 for (PendingRequest* request : pending_requests_queue_) { 126 for (PendingRequest* request : pending_requests_queue_) {
121 if (init_failed_) { 127 if (init_failed_) {
122 request->callback.Run(FAIL); 128 request->callback.Run(FAIL);
123 } else { 129 } else {
124 rpc_handler_->SendReportRequest( 130 rpc_handler_->SendReportRequest(
125 request->report.Pass(), 131 request->report.Pass(),
126 request->app_id, 132 request->app_id,
(...skipping 12 matching lines...) Expand all
139 // TODO(ckehoe): Retry for registration failures. But maybe not here. 145 // TODO(ckehoe): Retry for registration failures. But maybe not here.
140 } 146 }
141 147
142 DVLOG(3) << step << " complete."; 148 DVLOG(3) << step << " complete.";
143 DCHECK(pending_init_operations_ > 0); 149 DCHECK(pending_init_operations_ > 0);
144 pending_init_operations_--; 150 pending_init_operations_--;
145 CompleteInitialization(); 151 CompleteInitialization();
146 } 152 }
147 153
148 } // namespace copresence 154 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698