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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/copresence/public/copresence_client.h" 5 #include "components/copresence/public/copresence_client.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "components/copresence/public/copresence_client_delegate.h" 8 #include "components/copresence/public/copresence_client_delegate.h"
9 #include "components/copresence/public/whispernet_client.h" 9 #include "components/copresence/public/whispernet_client.h"
10 #include "components/copresence/rpc/rpc_handler.h" 10 #include "components/copresence/rpc/rpc_handler.h"
11 11
12 namespace copresence { 12 namespace copresence {
13 13
14 PendingRequest::PendingRequest(const copresence::ReportRequest& report, 14 PendingRequest::PendingRequest(const copresence::ReportRequest& report,
15 const std::string app_id, 15 const std::string app_id,
16 const StatusCallback& callback) 16 const StatusCallback& callback)
17 : report(report), app_id(app_id), callback(callback) { 17 : report(report), app_id(app_id), callback(callback) {
18 } 18 }
19 19
20 PendingRequest::~PendingRequest() { 20 PendingRequest::~PendingRequest() {
21 } 21 }
22 22
23 // Public methods 23 // Public methods
24 24
25 CopresenceClient::CopresenceClient(CopresenceClientDelegate* delegate) 25 CopresenceClient::~CopresenceClient() {}
26 : delegate_(delegate), init_failed_(false), pending_init_operations_(0) {
27 DVLOG(3) << "Initializing client.";
28 pending_init_operations_++;
29 rpc_handler_.reset(
30 new RpcHandler(delegate,
31 base::Bind(&CopresenceClient::InitStepComplete,
32 AsWeakPtr(),
33 "Copresence device registration")));
34
35 pending_init_operations_++;
36 delegate_->GetWhispernetClient()->Initialize(
37 base::Bind(&CopresenceClient::InitStepComplete,
38 AsWeakPtr(),
39 "Whispernet proxy initialization"));
40 }
41
42 CopresenceClient::~CopresenceClient() {
43 }
44 26
45 void CopresenceClient::Shutdown() { 27 void CopresenceClient::Shutdown() {
46 DVLOG(3) << "Shutting down client."; 28 DVLOG(3) << "Shutting down client.";
47 delegate_->GetWhispernetClient()->Shutdown(); 29 if (delegate_ && delegate_->GetWhispernetClient())
48 rpc_handler_->DisconnectFromWhispernet(); 30 delegate_->GetWhispernetClient()->Shutdown();
31 if (rpc_handler_.get())
32 rpc_handler_->DisconnectFromWhispernet();
49 } 33 }
50 34
51 // Returns false if any operations were malformed. 35 // Returns false if any operations were malformed.
52 void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request, 36 void CopresenceClient::ExecuteReportRequest(copresence::ReportRequest request,
53 const std::string& app_id, 37 const std::string& app_id,
54 const StatusCallback& callback) { 38 const StatusCallback& callback) {
55 // Don't take on any more requests, we can't execute any, init failed. 39 // Don't take on any more requests. We can't execute them since init failed.
56 if (init_failed_) { 40 if (init_failed_) {
57 callback.Run(FAIL); 41 callback.Run(FAIL);
58 return; 42 return;
59 } 43 }
60 44
45 DCHECK(rpc_handler_.get());
61 if (pending_init_operations_) { 46 if (pending_init_operations_) {
62 pending_requests_queue_.push_back( 47 pending_requests_queue_.push_back(
63 PendingRequest(request, app_id, callback)); 48 PendingRequest(request, app_id, callback));
64 } else { 49 } else {
65 rpc_handler_->SendReportRequest( 50 rpc_handler_->SendReportRequest(
66 make_scoped_ptr(new copresence::ReportRequest(request)), 51 make_scoped_ptr(new copresence::ReportRequest(request)),
67 app_id, 52 app_id,
68 callback); 53 callback);
69 } 54 }
70 } 55 }
71 56
57 // static
58 scoped_ptr<CopresenceClient> CopresenceClient::Create(
59 CopresenceClientDelegate* delegate) {
60 DVLOG(3) << "Initializing client.";
61 scoped_ptr<CopresenceClient> client(new CopresenceClient);
62 DCHECK(delegate);
63 client->delegate_ = delegate;
64
65 client->pending_init_operations_++;
66 client->rpc_handler_.reset(
67 new RpcHandler(delegate,
68 base::Bind(&CopresenceClient::InitStepComplete,
69 client->AsWeakPtr(),
70 "Copresence device registration")));
71
72 client->pending_init_operations_++;
73 DCHECK(delegate->GetWhispernetClient());
74 delegate->GetWhispernetClient()->Initialize(
75 base::Bind(&CopresenceClient::InitStepComplete,
76 client->AsWeakPtr(),
77 "Whispernet proxy initialization"));
78
79 return client.Pass();
80 }
81
82 // Protected methods
83
84 CopresenceClient::CopresenceClient()
85 : delegate_(NULL),
86 init_failed_(false),
87 pending_init_operations_(0) {}
88
72 // Private methods 89 // Private methods
73 90
74 void CopresenceClient::CompleteInitialization() { 91 void CopresenceClient::CompleteInitialization() {
75 if (pending_init_operations_) 92 if (pending_init_operations_)
76 return; 93 return;
77 94
95 DCHECK(delegate_);
96 DCHECK(rpc_handler_.get());
78 if (!init_failed_) 97 if (!init_failed_)
79 rpc_handler_->ConnectToWhispernet(delegate_->GetWhispernetClient()); 98 rpc_handler_->ConnectToWhispernet(delegate_->GetWhispernetClient());
80 99
81 for (std::vector<PendingRequest>::iterator request = 100 for (std::vector<PendingRequest>::iterator request =
82 pending_requests_queue_.begin(); 101 pending_requests_queue_.begin();
83 request != pending_requests_queue_.end(); 102 request != pending_requests_queue_.end();
84 ++request) { 103 ++request) {
85 if (init_failed_) { 104 if (init_failed_) {
86 request->callback.Run(FAIL); 105 request->callback.Run(FAIL);
87 } else { 106 } else {
(...skipping 11 matching lines...) Expand all
99 LOG(ERROR) << step << " failed!"; 118 LOG(ERROR) << step << " failed!";
100 init_failed_ = true; 119 init_failed_ = true;
101 } 120 }
102 121
103 DVLOG(3) << "Init step: " << step << " complete."; 122 DVLOG(3) << "Init step: " << step << " complete.";
104 pending_init_operations_--; 123 pending_init_operations_--;
105 CompleteInitialization(); 124 CompleteInitialization();
106 } 125 }
107 126
108 } // namespace copresence 127 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698