| 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/copresence_manager_impl.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_manager_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 CopresenceManagerImpl::~CopresenceManagerImpl() {} |
| 26 : delegate_(delegate), init_failed_(false), pending_init_operations_(0) { | |
| 27 DVLOG(3) << "Initializing client."; | |
| 28 pending_init_operations_++; | |
| 29 rpc_handler_.reset(new RpcHandler(delegate)); | |
| 30 // We own the RpcHandler, so it won't outlive us. | |
| 31 rpc_handler_->Initialize(base::Bind(&CopresenceClient::InitStepComplete, | |
| 32 base::Unretained(this), | |
| 33 "Copresence device registration")); | |
| 34 | |
| 35 pending_init_operations_++; | |
| 36 delegate_->GetWhispernetClient()->Initialize( | |
| 37 base::Bind(&CopresenceClient::InitStepComplete, | |
| 38 // We cannot cancel WhispernetClient initialization. | |
| 39 // TODO(ckehoe): Get rid of this. | |
| 40 AsWeakPtr(), | |
| 41 "Whispernet proxy initialization")); | |
| 42 } | |
| 43 | |
| 44 CopresenceClient::~CopresenceClient() {} | |
| 45 | 26 |
| 46 // Returns false if any operations were malformed. | 27 // Returns false if any operations were malformed. |
| 47 void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request, | 28 void CopresenceManagerImpl::ExecuteReportRequest( |
| 48 const std::string& app_id, | 29 ReportRequest request, |
| 49 const StatusCallback& callback) { | 30 const std::string& app_id, |
| 50 // Don't take on any more requests, we can't execute any, init failed. | 31 const StatusCallback& callback) { |
| 32 // Don't take on any more requests. We can't execute them since init failed. |
| 51 if (init_failed_) { | 33 if (init_failed_) { |
| 52 callback.Run(FAIL); | 34 callback.Run(FAIL); |
| 53 return; | 35 return; |
| 54 } | 36 } |
| 55 | 37 |
| 38 DCHECK(rpc_handler_.get()); |
| 56 if (pending_init_operations_) { | 39 if (pending_init_operations_) { |
| 57 pending_requests_queue_.push_back( | 40 pending_requests_queue_.push_back( |
| 58 PendingRequest(request, app_id, callback)); | 41 PendingRequest(request, app_id, callback)); |
| 59 } else { | 42 } else { |
| 60 rpc_handler_->SendReportRequest( | 43 rpc_handler_->SendReportRequest( |
| 61 make_scoped_ptr(new copresence::ReportRequest(request)), | 44 make_scoped_ptr(new copresence::ReportRequest(request)), |
| 62 app_id, | 45 app_id, |
| 63 callback); | 46 callback); |
| 64 } | 47 } |
| 65 } | 48 } |
| 66 | 49 |
| 67 // Private methods | 50 // Private methods |
| 68 | 51 |
| 69 void CopresenceClient::CompleteInitialization() { | 52 CopresenceManagerImpl::CopresenceManagerImpl( |
| 53 CopresenceManagerDelegate* delegate) |
| 54 : init_failed_(false), |
| 55 pending_init_operations_(0), |
| 56 delegate_(delegate) { |
| 57 DCHECK(delegate); |
| 58 } |
| 59 |
| 60 void CopresenceManagerImpl::CompleteInitialization() { |
| 70 if (pending_init_operations_) | 61 if (pending_init_operations_) |
| 71 return; | 62 return; |
| 72 | 63 |
| 64 DCHECK(rpc_handler_.get()); |
| 73 if (!init_failed_) | 65 if (!init_failed_) |
| 74 rpc_handler_->ConnectToWhispernet(); | 66 rpc_handler_->ConnectToWhispernet(); |
| 75 | 67 |
| 76 for (std::vector<PendingRequest>::iterator request = | 68 for (std::vector<PendingRequest>::iterator request = |
| 77 pending_requests_queue_.begin(); | 69 pending_requests_queue_.begin(); |
| 78 request != pending_requests_queue_.end(); | 70 request != pending_requests_queue_.end(); |
| 79 ++request) { | 71 ++request) { |
| 80 if (init_failed_) { | 72 if (init_failed_) { |
| 81 request->callback.Run(FAIL); | 73 request->callback.Run(FAIL); |
| 82 } else { | 74 } else { |
| 83 rpc_handler_->SendReportRequest( | 75 rpc_handler_->SendReportRequest( |
| 84 make_scoped_ptr(new copresence::ReportRequest(request->report)), | 76 make_scoped_ptr(new copresence::ReportRequest(request->report)), |
| 85 request->app_id, | 77 request->app_id, |
| 86 request->callback); | 78 request->callback); |
| 87 } | 79 } |
| 88 } | 80 } |
| 89 pending_requests_queue_.clear(); | 81 pending_requests_queue_.clear(); |
| 90 } | 82 } |
| 91 | 83 |
| 92 void CopresenceClient::InitStepComplete(const std::string& step, bool success) { | 84 void CopresenceManagerImpl::InitStepComplete( |
| 85 const std::string& step, bool success) { |
| 93 if (!success) { | 86 if (!success) { |
| 94 LOG(ERROR) << step << " failed!"; | 87 LOG(ERROR) << step << " failed!"; |
| 95 init_failed_ = true; | 88 init_failed_ = true; |
| 96 } | 89 } |
| 97 | 90 |
| 98 DVLOG(3) << "Init step: " << step << " complete."; | 91 DVLOG(3) << "Init step: " << step << " complete."; |
| 99 pending_init_operations_--; | 92 pending_init_operations_--; |
| 100 CompleteInitialization(); | 93 CompleteInitialization(); |
| 101 } | 94 } |
| 102 | 95 |
| 103 } // namespace copresence | 96 } // namespace copresence |
| OLD | NEW |