Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Unified Diff: components/copresence/copresence_client.cc

Issue 441103002: Tests for the Copresence API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@api
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/copresence/copresence_client.cc
diff --git a/components/copresence/copresence_client.cc b/components/copresence/copresence_client.cc
index 60e45e15d2156368eddf0e575f1812e6339e9dc6..aee95e3149414e8ef5c770ced34c345262dace15 100644
--- a/components/copresence/copresence_client.cc
+++ b/components/copresence/copresence_client.cc
@@ -22,42 +22,27 @@ PendingRequest::~PendingRequest() {
// Public methods
-CopresenceClient::CopresenceClient(CopresenceClientDelegate* delegate)
- : delegate_(delegate), init_failed_(false), pending_init_operations_(0) {
- DVLOG(3) << "Initializing client.";
- pending_init_operations_++;
- rpc_handler_.reset(
- new RpcHandler(delegate,
- base::Bind(&CopresenceClient::InitStepComplete,
- AsWeakPtr(),
- "Copresence device registration")));
-
- pending_init_operations_++;
- delegate_->GetWhispernetClient()->Initialize(
- base::Bind(&CopresenceClient::InitStepComplete,
- AsWeakPtr(),
- "Whispernet proxy initialization"));
-}
-
-CopresenceClient::~CopresenceClient() {
-}
+CopresenceClient::~CopresenceClient() {}
void CopresenceClient::Shutdown() {
DVLOG(3) << "Shutting down client.";
- delegate_->GetWhispernetClient()->Shutdown();
- rpc_handler_->DisconnectFromWhispernet();
+ if (delegate_ && delegate_->GetWhispernetClient())
+ delegate_->GetWhispernetClient()->Shutdown();
+ if (rpc_handler_.get())
+ rpc_handler_->DisconnectFromWhispernet();
}
// 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.
+ // Don't take on any more requests. We can't execute them since init failed.
if (init_failed_) {
callback.Run(FAIL);
return;
}
+ DCHECK(rpc_handler_.get());
if (pending_init_operations_) {
pending_requests_queue_.push_back(
PendingRequest(request, app_id, callback));
@@ -69,12 +54,46 @@ void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request,
}
}
+// static
+scoped_ptr<CopresenceClient> CopresenceClient::Create(
+ CopresenceClientDelegate* delegate) {
+ DVLOG(3) << "Initializing client.";
+ scoped_ptr<CopresenceClient> client(new CopresenceClient);
+ DCHECK(delegate);
+ client->delegate_ = delegate;
+
+ client->pending_init_operations_++;
+ client->rpc_handler_.reset(
+ new RpcHandler(delegate,
+ base::Bind(&CopresenceClient::InitStepComplete,
+ client->AsWeakPtr(),
+ "Copresence device registration")));
+
+ client->pending_init_operations_++;
+ DCHECK(delegate->GetWhispernetClient());
+ delegate->GetWhispernetClient()->Initialize(
+ base::Bind(&CopresenceClient::InitStepComplete,
+ client->AsWeakPtr(),
+ "Whispernet proxy initialization"));
+
+ return client.Pass();
+}
+
+// Protected methods
+
+CopresenceClient::CopresenceClient()
+ : delegate_(NULL),
+ init_failed_(false),
+ pending_init_operations_(0) {}
+
// Private methods
void CopresenceClient::CompleteInitialization() {
if (pending_init_operations_)
return;
+ DCHECK(delegate_);
+ DCHECK(rpc_handler_.get());
if (!init_failed_)
rpc_handler_->ConnectToWhispernet(delegate_->GetWhispernetClient());

Powered by Google App Engine
This is Rietveld 408576698