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.
5 #include "components/copresence/copresence_manager_impl.h"
8 #include "components/copresence/public/copresence_delegate.h"
9 #include "components/copresence/public/whispernet_client.h"
10 #include "components/copresence/rpc/rpc_handler.h"
12 namespace copresence {
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) {
20 PendingRequest::~PendingRequest() {
25 CopresenceManagerImpl::~CopresenceManagerImpl() {}
27 // Returns false if any operations were malformed.
28 void CopresenceManagerImpl::ExecuteReportRequest(
29 ReportRequest request,
30 const std::string& app_id,
31 const StatusCallback& callback) {
32 // Don't take on any more requests. We can't execute them since init failed.
38 DCHECK(rpc_handler_.get());
39 if (pending_init_operations_) {
40 pending_requests_queue_.push_back(
41 PendingRequest(request, app_id, callback));
43 rpc_handler_->SendReportRequest(
44 make_scoped_ptr(new copresence::ReportRequest(request)),
52 CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate)
53 : init_failed_(false),
54 pending_init_operations_(0),
59 void CopresenceManagerImpl::CompleteInitialization() {
60 if (pending_init_operations_)
63 DCHECK(rpc_handler_.get());
65 rpc_handler_->ConnectToWhispernet();
67 for (std::vector<PendingRequest>::iterator request =
68 pending_requests_queue_.begin();
69 request != pending_requests_queue_.end();
72 request->callback.Run(FAIL);
74 rpc_handler_->SendReportRequest(
75 make_scoped_ptr(new copresence::ReportRequest(request->report)),
80 pending_requests_queue_.clear();
83 void CopresenceManagerImpl::InitStepComplete(
84 const std::string& step, bool success) {
86 LOG(ERROR) << step << " failed!";
90 DVLOG(3) << "Init step: " << step << " complete.";
91 pending_init_operations_--;
92 CompleteInitialization();
95 } // namespace copresence