| Index: components/copresence/copresence_client.cc
|
| diff --git a/components/copresence/copresence_client.cc b/components/copresence/copresence_client.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8ff2653d6a37a3c6acf16b2e12b76792d7744c7b
|
| --- /dev/null
|
| +++ b/components/copresence/copresence_client.cc
|
| @@ -0,0 +1,115 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "components/copresence/copresence_client.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "components/copresence/interface/copresence_delegate.h"
|
| +#include "components/copresence/interface/whispernet_client.h"
|
| +#include "components/copresence/rpc/rpc_handler.h"
|
| +
|
| +namespace copresence {
|
| +
|
| +PendingRequest::PendingRequest(const copresence::ReportRequest& report,
|
| + const std::string app_id,
|
| + const StatusCallback& callback)
|
| + : report(report), app_id(app_id), callback(callback) {
|
| +}
|
| +
|
| +PendingRequest::~PendingRequest() {
|
| +}
|
| +
|
| +// Public methods
|
| +
|
| +CopresenceClient::CopresenceClient(
|
| + CopresenceDelegate* delegate,
|
| + scoped_ptr<WhispernetClient> whispernet_client)
|
| + : init_failed_(false),
|
| + pending_init_operations_(0),
|
| + whispernet_client_(whispernet_client.Pass()) {
|
| + DVLOG(3) << "Initializing client.";
|
| + pending_init_operations_++;
|
| + rpc_handler_.reset(
|
| + new RpcHandler(delegate,
|
| + base::Bind(&CopresenceClient::InitStepComplete,
|
| + AsWeakPtr(),
|
| + "Copresence device registration")));
|
| +
|
| + pending_init_operations_++;
|
| + whispernet_client_->Initialize(base::Bind(&CopresenceClient::InitStepComplete,
|
| + AsWeakPtr(),
|
| + "Whispernet proxy initialization"));
|
| +}
|
| +
|
| +CopresenceClient::~CopresenceClient() {
|
| +}
|
| +
|
| +void CopresenceClient::Shutdown() {
|
| + DVLOG(3) << "Shutting down client.";
|
| + whispernet_client_->Shutdown();
|
| + rpc_handler_->DisconnectFromWhispernet();
|
| +}
|
| +
|
| +WhispernetClient* CopresenceClient::whispernet_client() {
|
| + return whispernet_client_.get();
|
| +}
|
| +
|
| +// Returns false if any operations were malformed.
|
| +void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request,
|
| + const std::string& app_id,
|
| + const StatusCallback& callback) {
|
| + // Don't take on any more requests, we can't execute any, init failed.
|
| + if (init_failed_) {
|
| + callback.Run(FAIL);
|
| + return;
|
| + }
|
| +
|
| + if (pending_init_operations_) {
|
| + pending_requests_queue_.push_back(
|
| + PendingRequest(request, app_id, callback));
|
| + } else {
|
| + rpc_handler_->SendReportRequest(
|
| + make_scoped_ptr(new copresence::ReportRequest(request)),
|
| + app_id,
|
| + callback);
|
| + }
|
| +}
|
| +
|
| +// Private methods
|
| +
|
| +void CopresenceClient::CompleteInitialization() {
|
| + if (pending_init_operations_)
|
| + return;
|
| +
|
| + if (!init_failed_)
|
| + rpc_handler_->ConnectToWhispernet(whispernet_client_.get());
|
| +
|
| + for (std::vector<PendingRequest>::iterator request =
|
| + pending_requests_queue_.begin();
|
| + request != pending_requests_queue_.end();
|
| + ++request) {
|
| + if (init_failed_) {
|
| + request->callback.Run(FAIL);
|
| + } else {
|
| + rpc_handler_->SendReportRequest(
|
| + make_scoped_ptr(new copresence::ReportRequest(request->report)),
|
| + request->app_id,
|
| + request->callback);
|
| + }
|
| + }
|
| + pending_requests_queue_.clear();
|
| +}
|
| +
|
| +void CopresenceClient::InitStepComplete(const std::string& step, bool success) {
|
| + if (!success) {
|
| + LOG(ERROR) << step << " failed!";
|
| + init_failed_ = true;
|
| + }
|
| +
|
| + DVLOG(3) << "Init step: " << step << " complete.";
|
| + pending_init_operations_--;
|
| + CompleteInitialization();
|
| +}
|
| +
|
| +} // namespace copresence
|
|
|