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 rpc_handler_->Initialize(base::Bind(&CopresenceClient::InitStepComplete, |
31 base::Bind(&CopresenceClient::InitStepComplete, | 31 AsWeakPtr(), |
willchan no longer on Chromium
2014/08/07 20:42:43
Why is AsWeakPtr() used here? Doesn't CopresenceCl
Charlie
2014/08/07 22:40:58
Removed.
| |
32 AsWeakPtr(), | 32 "Copresence device registration")); |
33 "Copresence device registration"))); | |
34 | 33 |
35 pending_init_operations_++; | 34 pending_init_operations_++; |
36 delegate_->GetWhispernetClient()->Initialize( | 35 delegate_->GetWhispernetClient()->Initialize( |
37 base::Bind(&CopresenceClient::InitStepComplete, | 36 base::Bind(&CopresenceClient::InitStepComplete, |
38 AsWeakPtr(), | 37 AsWeakPtr(), |
39 "Whispernet proxy initialization")); | 38 "Whispernet proxy initialization")); |
40 } | 39 } |
41 | 40 |
42 CopresenceClient::~CopresenceClient() { | 41 CopresenceClient::~CopresenceClient() { |
43 } | 42 } |
44 | 43 |
45 void CopresenceClient::Shutdown() { | 44 void CopresenceClient::Shutdown() { |
46 DVLOG(3) << "Shutting down client."; | 45 DVLOG(3) << "Shutting down client."; |
47 delegate_->GetWhispernetClient()->Shutdown(); | 46 delegate_->GetWhispernetClient()->Shutdown(); |
willchan no longer on Chromium
2014/08/07 20:42:43
Who's the delegate of CopresenceClient? Who is cal
Charlie
2014/08/07 22:40:58
Addressed by Rahul?
| |
48 rpc_handler_->DisconnectFromWhispernet(); | 47 rpc_handler_->DisconnectFromWhispernet(); |
49 } | 48 } |
50 | 49 |
51 // Returns false if any operations were malformed. | 50 // Returns false if any operations were malformed. |
52 void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request, | 51 void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request, |
53 const std::string& app_id, | 52 const std::string& app_id, |
54 const StatusCallback& callback) { | 53 const StatusCallback& callback) { |
55 // Don't take on any more requests, we can't execute any, init failed. | 54 // Don't take on any more requests, we can't execute any, init failed. |
56 if (init_failed_) { | 55 if (init_failed_) { |
57 callback.Run(FAIL); | 56 callback.Run(FAIL); |
(...skipping 11 matching lines...) Expand all Loading... | |
69 } | 68 } |
70 } | 69 } |
71 | 70 |
72 // Private methods | 71 // Private methods |
73 | 72 |
74 void CopresenceClient::CompleteInitialization() { | 73 void CopresenceClient::CompleteInitialization() { |
75 if (pending_init_operations_) | 74 if (pending_init_operations_) |
76 return; | 75 return; |
77 | 76 |
78 if (!init_failed_) | 77 if (!init_failed_) |
79 rpc_handler_->ConnectToWhispernet(delegate_->GetWhispernetClient()); | 78 rpc_handler_->ConnectToWhispernet(); |
80 | 79 |
81 for (std::vector<PendingRequest>::iterator request = | 80 for (std::vector<PendingRequest>::iterator request = |
82 pending_requests_queue_.begin(); | 81 pending_requests_queue_.begin(); |
83 request != pending_requests_queue_.end(); | 82 request != pending_requests_queue_.end(); |
84 ++request) { | 83 ++request) { |
85 if (init_failed_) { | 84 if (init_failed_) { |
86 request->callback.Run(FAIL); | 85 request->callback.Run(FAIL); |
87 } else { | 86 } else { |
88 rpc_handler_->SendReportRequest( | 87 rpc_handler_->SendReportRequest( |
89 make_scoped_ptr(new copresence::ReportRequest(request->report)), | 88 make_scoped_ptr(new copresence::ReportRequest(request->report)), |
90 request->app_id, | 89 request->app_id, |
91 request->callback); | 90 request->callback); |
92 } | 91 } |
93 } | 92 } |
94 pending_requests_queue_.clear(); | 93 pending_requests_queue_.clear(); |
95 } | 94 } |
96 | 95 |
97 void CopresenceClient::InitStepComplete(const std::string& step, bool success) { | 96 void CopresenceClient::InitStepComplete(const std::string& step, bool success) { |
98 if (!success) { | 97 if (!success) { |
99 LOG(ERROR) << step << " failed!"; | 98 LOG(ERROR) << step << " failed!"; |
100 init_failed_ = true; | 99 init_failed_ = true; |
101 } | 100 } |
102 | 101 |
103 DVLOG(3) << "Init step: " << step << " complete."; | 102 DVLOG(3) << "Init step: " << step << " complete."; |
104 pending_init_operations_--; | 103 pending_init_operations_--; |
105 CompleteInitialization(); | 104 CompleteInitialization(); |
106 } | 105 } |
107 | 106 |
108 } // namespace copresence | 107 } // namespace copresence |
OLD | NEW |