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/public/copresence_client.h" | 5 #include "components/copresence/public/copresence_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "components/copresence/public/copresence_client_delegate.h" | 8 #include "components/copresence/public/copresence_client_delegate.h" |
9 #include "components/copresence/public/whispernet_client.h" | 9 #include "components/copresence/public/whispernet_client.h" |
10 #include "components/copresence/rpc/rpc_handler.h" | 10 #include "components/copresence/rpc/rpc_handler.h" |
11 | 11 |
12 namespace copresence { | 12 namespace copresence { |
13 | 13 |
14 PendingRequest::PendingRequest(const copresence::ReportRequest& report, | 14 PendingRequest::PendingRequest(const copresence::ReportRequest& report, |
15 const std::string app_id, | 15 const std::string app_id, |
16 const StatusCallback& callback) | 16 const StatusCallback& callback) |
17 : report(report), app_id(app_id), callback(callback) { | 17 : report(report), app_id(app_id), callback(callback) { |
18 } | 18 } |
19 | 19 |
20 PendingRequest::~PendingRequest() { | 20 PendingRequest::~PendingRequest() { |
21 } | 21 } |
22 | 22 |
23 // Public methods | 23 // Public methods |
24 | 24 |
25 CopresenceClient::CopresenceClient(CopresenceClientDelegate* delegate) | 25 CopresenceClient::CopresenceClient(CopresenceClientDelegate* delegate) |
26 : delegate_(delegate), init_failed_(false), pending_init_operations_(0) { | 26 : delegate_(delegate), init_failed_(false), pending_init_operations_(0) { |
27 DVLOG(3) << "Initializing client."; | 27 DVLOG(3) << "Initializing client."; |
28 pending_init_operations_++; | 28 pending_init_operations_++; |
29 rpc_handler_.reset( | 29 rpc_handler_.reset(new RpcHandler(delegate)); |
30 new RpcHandler(delegate, | 30 // We own the RpcHandler, so it won't outlive us. |
31 base::Bind(&CopresenceClient::InitStepComplete, | 31 rpc_handler_->Initialize(base::Bind(&CopresenceClient::InitStepComplete, |
32 AsWeakPtr(), | 32 base::Unretained(this), |
33 "Copresence device registration"))); | 33 "Copresence device registration")); |
34 | 34 |
35 pending_init_operations_++; | 35 pending_init_operations_++; |
36 delegate_->GetWhispernetClient()->Initialize( | 36 delegate_->GetWhispernetClient()->Initialize( |
37 base::Bind(&CopresenceClient::InitStepComplete, | 37 base::Bind(&CopresenceClient::InitStepComplete, |
38 AsWeakPtr(), | 38 // The WhispernetClient must discard this on Shutdown. |
willchan no longer on Chromium
2014/08/08 01:05:58
I like that you got rid of the AsWeakPtr(). It's i
rkc
2014/08/08 01:29:07
So this is actually a bug. If the whispernet clien
willchan no longer on Chromium
2014/08/08 18:01:20
I chatted with rkc@ offline about this, but I can'
Charlie
2014/08/08 21:57:52
Putting just this one back to AsWeakPtr(), and get
willchan no longer on Chromium
2014/08/08 22:53:33
Please don't use SupportsWeakPtr(), there's reason
Charlie
2014/08/08 23:38:30
Noted. Will fix after this is in.
| |
39 base::Unretained(this), | |
39 "Whispernet proxy initialization")); | 40 "Whispernet proxy initialization")); |
40 } | 41 } |
41 | 42 |
42 CopresenceClient::~CopresenceClient() { | 43 CopresenceClient::~CopresenceClient() { |
43 } | |
44 | |
45 void CopresenceClient::Shutdown() { | |
46 DVLOG(3) << "Shutting down client."; | |
47 delegate_->GetWhispernetClient()->Shutdown(); | 44 delegate_->GetWhispernetClient()->Shutdown(); |
48 rpc_handler_->DisconnectFromWhispernet(); | |
49 } | 45 } |
50 | 46 |
51 // Returns false if any operations were malformed. | 47 // Returns false if any operations were malformed. |
52 void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request, | 48 void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request, |
53 const std::string& app_id, | 49 const std::string& app_id, |
54 const StatusCallback& callback) { | 50 const StatusCallback& callback) { |
55 // Don't take on any more requests, we can't execute any, init failed. | 51 // Don't take on any more requests, we can't execute any, init failed. |
56 if (init_failed_) { | 52 if (init_failed_) { |
57 callback.Run(FAIL); | 53 callback.Run(FAIL); |
58 return; | 54 return; |
(...skipping 10 matching lines...) Expand all Loading... | |
69 } | 65 } |
70 } | 66 } |
71 | 67 |
72 // Private methods | 68 // Private methods |
73 | 69 |
74 void CopresenceClient::CompleteInitialization() { | 70 void CopresenceClient::CompleteInitialization() { |
75 if (pending_init_operations_) | 71 if (pending_init_operations_) |
76 return; | 72 return; |
77 | 73 |
78 if (!init_failed_) | 74 if (!init_failed_) |
79 rpc_handler_->ConnectToWhispernet(delegate_->GetWhispernetClient()); | 75 rpc_handler_->ConnectToWhispernet(); |
80 | 76 |
81 for (std::vector<PendingRequest>::iterator request = | 77 for (std::vector<PendingRequest>::iterator request = |
82 pending_requests_queue_.begin(); | 78 pending_requests_queue_.begin(); |
83 request != pending_requests_queue_.end(); | 79 request != pending_requests_queue_.end(); |
84 ++request) { | 80 ++request) { |
85 if (init_failed_) { | 81 if (init_failed_) { |
86 request->callback.Run(FAIL); | 82 request->callback.Run(FAIL); |
87 } else { | 83 } else { |
88 rpc_handler_->SendReportRequest( | 84 rpc_handler_->SendReportRequest( |
89 make_scoped_ptr(new copresence::ReportRequest(request->report)), | 85 make_scoped_ptr(new copresence::ReportRequest(request->report)), |
90 request->app_id, | 86 request->app_id, |
91 request->callback); | 87 request->callback); |
92 } | 88 } |
93 } | 89 } |
94 pending_requests_queue_.clear(); | 90 pending_requests_queue_.clear(); |
95 } | 91 } |
96 | 92 |
97 void CopresenceClient::InitStepComplete(const std::string& step, bool success) { | 93 void CopresenceClient::InitStepComplete(const std::string& step, bool success) { |
98 if (!success) { | 94 if (!success) { |
99 LOG(ERROR) << step << " failed!"; | 95 LOG(ERROR) << step << " failed!"; |
100 init_failed_ = true; | 96 init_failed_ = true; |
101 } | 97 } |
102 | 98 |
103 DVLOG(3) << "Init step: " << step << " complete."; | 99 DVLOG(3) << "Init step: " << step << " complete."; |
104 pending_init_operations_--; | 100 pending_init_operations_--; |
105 CompleteInitialization(); | 101 CompleteInitialization(); |
106 } | 102 } |
107 | 103 |
108 } // namespace copresence | 104 } // namespace copresence |
OLD | NEW |