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

Side by Side Diff: chrome/browser/extensions/api/gcd_private/gcd_private_api.cc

Issue 411783002: Initial implementation of session APIs, sans WiFi component (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
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 "chrome/browser/extensions/api/gcd_private/gcd_private_api.h" 5 #include "chrome/browser/extensions/api/gcd_private/gcd_private_api.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/local_discovery/cloud_device_list.h" 10 #include "chrome/browser/local_discovery/cloud_device_list.h"
10 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" 11 #include "chrome/browser/local_discovery/cloud_print_printer_list.h"
11 #include "chrome/browser/local_discovery/gcd_constants.h" 12 #include "chrome/browser/local_discovery/gcd_constants.h"
12 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" 13 #include "chrome/browser/local_discovery/privet_device_lister_impl.h"
14 #include "chrome/browser/local_discovery/privet_http_impl.h"
13 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/browser/signin/signin_manager_factory.h" 17 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "components/signin/core/browser/profile_oauth2_token_service.h" 18 #include "components/signin/core/browser/profile_oauth2_token_service.h"
17 #include "components/signin/core/browser/signin_manager.h" 19 #include "components/signin/core/browser/signin_manager.h"
18 #include "components/signin/core/browser/signin_manager_base.h" 20 #include "components/signin/core/browser/signin_manager_base.h"
19 21
20 namespace extensions { 22 namespace extensions {
21 23
22 namespace gcd_private = api::gcd_private; 24 namespace gcd_private = api::gcd_private;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 return scoped_ptr<local_discovery::GCDApiFlow>(); 68 return scoped_ptr<local_discovery::GCDApiFlow>();
67 return local_discovery::GCDApiFlow::Create( 69 return local_discovery::GCDApiFlow::Create(
68 profile->GetRequestContext(), 70 profile->GetRequestContext(),
69 token_service, 71 token_service,
70 signin_manager->GetAuthenticatedAccountId()); 72 signin_manager->GetAuthenticatedAccountId());
71 } 73 }
72 74
73 } // namespace 75 } // namespace
74 76
75 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) 77 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context)
76 : num_device_listeners_(0), browser_context_(context) { 78 : num_device_listeners_(0), last_session_id_(0), browser_context_(context) {
77 DCHECK(browser_context_); 79 DCHECK(browser_context_);
78 if (EventRouter::Get(context)) { 80 if (EventRouter::Get(context)) {
79 EventRouter::Get(context) 81 EventRouter::Get(context)
80 ->RegisterObserver(this, gcd_private::OnDeviceStateChanged::kEventName); 82 ->RegisterObserver(this, gcd_private::OnDeviceStateChanged::kEventName);
81 EventRouter::Get(context) 83 EventRouter::Get(context)
82 ->RegisterObserver(this, gcd_private::OnDeviceRemoved::kEventName); 84 ->RegisterObserver(this, gcd_private::OnDeviceRemoved::kEventName);
83 } 85 }
84 } 86 }
85 87
86 GcdPrivateAPI::~GcdPrivateAPI() { 88 GcdPrivateAPI::~GcdPrivateAPI() {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 172
171 bool GcdPrivateAPI::QueryForDevices() { 173 bool GcdPrivateAPI::QueryForDevices() {
172 if (!privet_device_lister_) 174 if (!privet_device_lister_)
173 return false; 175 return false;
174 176
175 privet_device_lister_->DiscoverNewDevices(true); 177 privet_device_lister_->DiscoverNewDevices(true);
176 178
177 return true; 179 return true;
178 } 180 }
179 181
182 void GcdPrivateAPI::EstablishSession(std::string ip_address,
183 int port,
184 ConfirmationCodeCallback callback) {
185 int session_id = last_session_id_++;
186 linked_ptr<GcdPrivateSessionHolder> session_handler(
187 new GcdPrivateSessionHolder(
188 ip_address, port, browser_context_->GetRequestContext()));
189 sessions_[session_id] = session_handler;
190 session_handler->Start(base::Bind(callback, session_id));
191 }
192
193 void GcdPrivateAPI::ConfirmCode(int session_id,
194 SessionEstablishedCallback callback) {
195 GCDSessionMap::iterator found = sessions_.find(session_id);
196
197 if (found == sessions_.end()) {
198 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR);
199 return;
200 }
201
202 found->second->ConfirmCode(callback);
203 }
204
205 void GcdPrivateAPI::SendMessage(int session_id,
206 const std::string& api,
207 const base::DictionaryValue& input,
208 MessageResponseCallback callback) {
209 GCDSessionMap::iterator found = sessions_.find(session_id);
210
211 if (found == sessions_.end()) {
212 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR,
213 base::DictionaryValue());
214 return;
215 }
216
217 found->second->SendMessage(api, input, callback);
218 }
219
180 // static 220 // static
181 void GcdPrivateAPI::SetGCDApiFlowFactoryForTests( 221 void GcdPrivateAPI::SetGCDApiFlowFactoryForTests(
182 GCDApiFlowFactoryForTests* factory) { 222 GCDApiFlowFactoryForTests* factory) {
183 g_gcd_api_flow_factory = factory; 223 g_gcd_api_flow_factory = factory;
184 } 224 }
185 225
226 GcdPrivateRequest::GcdPrivateRequest(
227 const std::string& api,
228 const base::DictionaryValue& input,
229 const GcdPrivateAPI::MessageResponseCallback& callback,
230 GcdPrivateSessionHolder* session_holder)
231 : api_(api),
232 input_(input.DeepCopy()),
233 callback_(callback),
234 session_holder_(session_holder) {
235 }
236
237 GcdPrivateRequest::~GcdPrivateRequest() {
238 }
239
240 std::string GcdPrivateRequest::GetName() {
241 return api_;
242 }
243
244 const base::DictionaryValue& GcdPrivateRequest::GetInput() {
245 return *input_;
246 }
247
248 void GcdPrivateRequest::OnError(
249 local_discovery::PrivetURLFetcher::ErrorType error) {
250 callback_.Run(gcd_private::STATUS_CONNECTIONERROR, base::DictionaryValue());
251
252 session_holder_->DeleteRequest(this);
253 }
254
255 void GcdPrivateRequest::OnParsedJson(const base::DictionaryValue& value,
256 bool has_error) {
257 callback_.Run(gcd_private::STATUS_SUCCESS, value);
258
259 session_holder_->DeleteRequest(this);
260 }
261
262 GcdPrivateSessionHolder::GcdPrivateSessionHolder(
263 const std::string& ip_address,
264 int port,
265 net::URLRequestContextGetter* request_context) {
266 std::string host_string;
267
268 // HACK: Check if the IP address is an IPv6 address
269 if (ip_address.find(':') != std::string::npos) {
270 host_string = base::StringPrintf("[%s]", ip_address.c_str());
271 } else {
272 host_string = ip_address;
273 }
274
275 http_client_.reset(new local_discovery::PrivetHTTPClientImpl(
276 "", net::HostPortPair(host_string, port), request_context));
277 }
278
279 GcdPrivateSessionHolder::~GcdPrivateSessionHolder() {
280 }
281
282 void GcdPrivateSessionHolder::Start(const ConfirmationCodeCallback& callback) {
283 confirm_callback_ = callback;
284
285 privet_session_.reset(
286 new local_discovery::PrivetV3Session(http_client_.Pass(), this));
287 privet_session_->Start();
288 }
289
290 void GcdPrivateSessionHolder::ConfirmCode(
291 const GcdPrivateAPI::SessionEstablishedCallback& callback) {
292 session_established_callback_ = callback;
293 privet_session_->ConfirmCode();
294 }
295
296 void GcdPrivateSessionHolder::SendMessage(
297 const std::string& api,
298 const base::DictionaryValue& input,
299 GcdPrivateAPI::MessageResponseCallback callback) {
300 linked_ptr<GcdPrivateRequest> request(
301 new GcdPrivateRequest(api, input, callback, this));
302 requests_.push_back(request);
303 privet_session_->StartRequest(request.get());
304 }
305
306 void GcdPrivateSessionHolder::DeleteRequest(GcdPrivateRequest* request) {
307 // TODO(noamsml): Does this need to be optimized?
308 for (RequestVector::iterator i = requests_.begin(); i != requests_.end();
309 i++) {
310 if (i->get() == request) {
311 requests_.erase(i);
312 break;
313 }
314 }
315 }
316
317 void GcdPrivateSessionHolder::OnSetupConfirmationNeeded(
318 const std::string& confirmation_code) {
319 confirm_callback_.Run(gcd_private::STATUS_SUCCESS,
320 confirmation_code,
321 gcd_private::CONFIRMATION_TYPE_DISPLAYCODE);
322
323 confirm_callback_.Reset();
324 }
325
326 void GcdPrivateSessionHolder::OnSessionEstablished() {
327 session_established_callback_.Run(gcd_private::STATUS_SUCCESS);
328
329 session_established_callback_.Reset();
330 }
331
332 void GcdPrivateSessionHolder::OnCannotEstablishSession() {
333 session_established_callback_.Run(gcd_private::STATUS_SESSIONERROR);
334
335 session_established_callback_.Reset();
336 }
337
186 GcdPrivateGetCloudDeviceListFunction::GcdPrivateGetCloudDeviceListFunction() { 338 GcdPrivateGetCloudDeviceListFunction::GcdPrivateGetCloudDeviceListFunction() {
187 } 339 }
188 340
189 GcdPrivateGetCloudDeviceListFunction::~GcdPrivateGetCloudDeviceListFunction() { 341 GcdPrivateGetCloudDeviceListFunction::~GcdPrivateGetCloudDeviceListFunction() {
190 } 342 }
191 343
192 bool GcdPrivateGetCloudDeviceListFunction::RunAsync() { 344 bool GcdPrivateGetCloudDeviceListFunction::RunAsync() {
193 requests_succeeded_ = 0; 345 requests_succeeded_ = 0;
194 requests_failed_ = 0; 346 requests_failed_ = 0;
195 347
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 return false; 448 return false;
297 } 449 }
298 450
299 GcdPrivateEstablishSessionFunction::GcdPrivateEstablishSessionFunction() { 451 GcdPrivateEstablishSessionFunction::GcdPrivateEstablishSessionFunction() {
300 } 452 }
301 453
302 GcdPrivateEstablishSessionFunction::~GcdPrivateEstablishSessionFunction() { 454 GcdPrivateEstablishSessionFunction::~GcdPrivateEstablishSessionFunction() {
303 } 455 }
304 456
305 bool GcdPrivateEstablishSessionFunction::RunAsync() { 457 bool GcdPrivateEstablishSessionFunction::RunAsync() {
306 return false; 458 scoped_ptr<gcd_private::EstablishSession::Params> params =
459 gcd_private::EstablishSession::Params::Create(*args_);
460
461 if (!params)
462 return false;
463
464 GcdPrivateAPI* gcd_api =
465 BrowserContextKeyedAPIFactory<GcdPrivateAPI>::Get(GetProfile());
466
467 if (!gcd_api)
468 return false;
469
470 gcd_api->EstablishSession(
471 params->ip_address,
472 params->port,
473 base::Bind(&GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback,
474 this));
475
476 return true;
477 }
478
479 void GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback(
480 int session_id,
481 gcd_private::Status status,
482 const std::string& confirm_code,
483 gcd_private::ConfirmationType confirmation_type) {
484 results_ = gcd_private::EstablishSession::Results::Create(
485 session_id, status, confirm_code, confirmation_type);
486 SendResponse(true);
307 } 487 }
308 488
309 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { 489 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() {
310 } 490 }
311 491
312 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { 492 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() {
313 } 493 }
314 494
315 bool GcdPrivateConfirmCodeFunction::RunAsync() { 495 bool GcdPrivateConfirmCodeFunction::RunAsync() {
316 return false; 496 scoped_ptr<gcd_private::ConfirmCode::Params> params =
497 gcd_private::ConfirmCode::Params::Create(*args_);
498
499 if (!params)
500 return false;
501
502 GcdPrivateAPI* gcd_api =
503 BrowserContextKeyedAPIFactory<GcdPrivateAPI>::Get(GetProfile());
504
505 if (!gcd_api)
506 return false;
507
508 gcd_api->ConfirmCode(
509 params->session_id,
510 base::Bind(&GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback,
511 this));
512
513 return true;
514 }
515
516 void GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback(
517 api::gcd_private::Status status) {
518 results_ = gcd_private::ConfirmCode::Results::Create(status);
519 SendResponse(true);
317 } 520 }
318 521
319 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { 522 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() {
320 } 523 }
321 524
322 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { 525 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() {
323 } 526 }
324 527
325 bool GcdPrivateSendMessageFunction::RunAsync() { 528 bool GcdPrivateSendMessageFunction::RunAsync() {
326 return false; 529 scoped_ptr<gcd_private::PassMessage::Params> params =
530 gcd_private::PassMessage::Params::Create(*args_);
531
532 if (!params)
533 return false;
534
535 GcdPrivateAPI* gcd_api =
536 BrowserContextKeyedAPIFactory<GcdPrivateAPI>::Get(GetProfile());
537
538 if (!gcd_api)
539 return false;
540
541 gcd_api->SendMessage(
542 params->session_id,
543 params->api,
544 params->input.additional_properties,
545 base::Bind(&GcdPrivateSendMessageFunction::OnMessageSentCallback, this));
546
547 return true;
548 }
549
550 void GcdPrivateSendMessageFunction::OnMessageSentCallback(
551 api::gcd_private::Status status,
552 const base::DictionaryValue& value) {
553 gcd_private::PassMessage::Results::Response response;
554 response.additional_properties.MergeDictionary(&value);
555
556 results_ = gcd_private::PassMessage::Results::Create(status, response);
557 SendResponse(true);
327 } 558 }
328 559
329 GcdPrivateTerminateSessionFunction::GcdPrivateTerminateSessionFunction() { 560 GcdPrivateTerminateSessionFunction::GcdPrivateTerminateSessionFunction() {
330 } 561 }
331 562
332 GcdPrivateTerminateSessionFunction::~GcdPrivateTerminateSessionFunction() { 563 GcdPrivateTerminateSessionFunction::~GcdPrivateTerminateSessionFunction() {
333 } 564 }
334 565
335 bool GcdPrivateTerminateSessionFunction::RunAsync() { 566 bool GcdPrivateTerminateSessionFunction::RunAsync() {
336 return false; 567 return false;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 } 613 }
383 614
384 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { 615 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() {
385 } 616 }
386 617
387 bool GcdPrivateGetCommandsListFunction::RunAsync() { 618 bool GcdPrivateGetCommandsListFunction::RunAsync() {
388 return false; 619 return false;
389 } 620 }
390 621
391 } // namespace extensions 622 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698