OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/copresence/copresence_client.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "components/copresence/interface/copresence_delegate.h" |
| 9 #include "components/copresence/interface/whispernet_client.h" |
| 10 #include "components/copresence/rpc/rpc_handler.h" |
| 11 |
| 12 namespace copresence { |
| 13 |
| 14 PendingRequest::PendingRequest(const copresence::ReportRequest& report, |
| 15 const std::string app_id, |
| 16 const StatusCallback& callback) |
| 17 : report(report), app_id(app_id), callback(callback) { |
| 18 } |
| 19 |
| 20 PendingRequest::~PendingRequest() { |
| 21 } |
| 22 |
| 23 // Public methods |
| 24 |
| 25 CopresenceClient::CopresenceClient( |
| 26 CopresenceDelegate* delegate, |
| 27 scoped_ptr<WhispernetClient> whispernet_client) |
| 28 : init_failed_(false), |
| 29 pending_init_operations_(0), |
| 30 whispernet_client_(whispernet_client.Pass()) { |
| 31 DVLOG(3) << "Initializing client."; |
| 32 pending_init_operations_++; |
| 33 rpc_handler_.reset( |
| 34 new RpcHandler(delegate, |
| 35 base::Bind(&CopresenceClient::InitStepComplete, |
| 36 AsWeakPtr(), |
| 37 "Copresence device registration"))); |
| 38 |
| 39 pending_init_operations_++; |
| 40 whispernet_client_->Initialize(base::Bind(&CopresenceClient::InitStepComplete, |
| 41 AsWeakPtr(), |
| 42 "Whispernet proxy initialization")); |
| 43 } |
| 44 |
| 45 CopresenceClient::~CopresenceClient() { |
| 46 } |
| 47 |
| 48 void CopresenceClient::Shutdown() { |
| 49 DVLOG(3) << "Shutting down client."; |
| 50 whispernet_client_->Shutdown(); |
| 51 rpc_handler_->DisconnectFromWhispernet(); |
| 52 } |
| 53 |
| 54 WhispernetClient* CopresenceClient::whispernet_client() { |
| 55 return whispernet_client_.get(); |
| 56 } |
| 57 |
| 58 // Returns false if any operations were malformed. |
| 59 void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request, |
| 60 const std::string& app_id, |
| 61 const StatusCallback& callback) { |
| 62 // Don't take on any more requests, we can't execute any, init failed. |
| 63 if (init_failed_) { |
| 64 callback.Run(FAIL); |
| 65 return; |
| 66 } |
| 67 |
| 68 if (pending_init_operations_) { |
| 69 pending_requests_queue_.push_back( |
| 70 PendingRequest(request, app_id, callback)); |
| 71 } else { |
| 72 rpc_handler_->SendReportRequest( |
| 73 make_scoped_ptr(new copresence::ReportRequest(request)), |
| 74 app_id, |
| 75 callback); |
| 76 } |
| 77 } |
| 78 |
| 79 // Private methods |
| 80 |
| 81 void CopresenceClient::CompleteInitialization() { |
| 82 if (pending_init_operations_) |
| 83 return; |
| 84 |
| 85 if (!init_failed_) |
| 86 rpc_handler_->ConnectToWhispernet(whispernet_client_.get()); |
| 87 |
| 88 for (std::vector<PendingRequest>::iterator request = |
| 89 pending_requests_queue_.begin(); |
| 90 request != pending_requests_queue_.end(); |
| 91 ++request) { |
| 92 if (init_failed_) { |
| 93 request->callback.Run(FAIL); |
| 94 } else { |
| 95 rpc_handler_->SendReportRequest( |
| 96 make_scoped_ptr(new copresence::ReportRequest(request->report)), |
| 97 request->app_id, |
| 98 request->callback); |
| 99 } |
| 100 } |
| 101 pending_requests_queue_.clear(); |
| 102 } |
| 103 |
| 104 void CopresenceClient::InitStepComplete(const std::string& step, bool success) { |
| 105 if (!success) { |
| 106 LOG(ERROR) << step << " failed!"; |
| 107 init_failed_ = true; |
| 108 } |
| 109 |
| 110 DVLOG(3) << "Init step: " << step << " complete."; |
| 111 pending_init_operations_--; |
| 112 CompleteInitialization(); |
| 113 } |
| 114 |
| 115 } // namespace copresence |
OLD | NEW |