Update V8 to version 3.30.4 (based on bleeding_edge revision r24443)
[chromium-blink-merge.git] / components / copresence / copresence_manager_impl.cc
blob342ed1999a184cb59788612f951bb25175595bc7
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"
7 #include "base/bind.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() {
23 // Public methods
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.
33 if (init_failed_) {
34 callback.Run(FAIL);
35 return;
38 DCHECK(rpc_handler_.get());
39 if (pending_init_operations_) {
40 pending_requests_queue_.push_back(
41 PendingRequest(request, app_id, callback));
42 } else {
43 rpc_handler_->SendReportRequest(
44 make_scoped_ptr(new copresence::ReportRequest(request)),
45 app_id,
46 callback);
50 // Private methods
52 CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate)
53 : init_failed_(false),
54 pending_init_operations_(0),
55 delegate_(delegate) {
56 DCHECK(delegate);
59 void CopresenceManagerImpl::CompleteInitialization() {
60 if (pending_init_operations_)
61 return;
63 DCHECK(rpc_handler_.get());
64 if (!init_failed_)
65 rpc_handler_->ConnectToWhispernet();
67 for (PendingRequest& request : pending_requests_queue_) {
68 if (init_failed_) {
69 request.callback.Run(FAIL);
70 } else {
71 rpc_handler_->SendReportRequest(
72 make_scoped_ptr(new copresence::ReportRequest(request.report)),
73 request.app_id,
74 request.callback);
77 pending_requests_queue_.clear();
80 void CopresenceManagerImpl::InitStepComplete(
81 const std::string& step, bool success) {
82 if (!success) {
83 LOG(ERROR) << step << " failed!";
84 init_failed_ = true;
87 DVLOG(3) << "Init step: " << step << " complete.";
88 pending_init_operations_--;
89 CompleteInitialization();
92 } // namespace copresence