OLD | NEW |
---|---|
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/memory/scoped_vector.h" | |
10 #include "base/strings/stringprintf.h" | |
9 #include "chrome/browser/local_discovery/cloud_device_list.h" | 11 #include "chrome/browser/local_discovery/cloud_device_list.h" |
10 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" | 12 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" |
11 #include "chrome/browser/local_discovery/gcd_constants.h" | 13 #include "chrome/browser/local_discovery/gcd_constants.h" |
12 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" | 14 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" |
15 #include "chrome/browser/local_discovery/privet_http_impl.h" | |
13 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
15 #include "chrome/browser/signin/signin_manager_factory.h" | 18 #include "chrome/browser/signin/signin_manager_factory.h" |
16 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 19 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
17 #include "components/signin/core/browser/signin_manager.h" | 20 #include "components/signin/core/browser/signin_manager.h" |
18 #include "components/signin/core/browser/signin_manager_base.h" | 21 #include "components/signin/core/browser/signin_manager_base.h" |
19 | 22 |
20 namespace extensions { | 23 namespace extensions { |
21 | 24 |
22 namespace gcd_private = api::gcd_private; | 25 namespace gcd_private = api::gcd_private; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 if (!signin_manager) | 68 if (!signin_manager) |
66 return scoped_ptr<local_discovery::GCDApiFlow>(); | 69 return scoped_ptr<local_discovery::GCDApiFlow>(); |
67 return local_discovery::GCDApiFlow::Create( | 70 return local_discovery::GCDApiFlow::Create( |
68 profile->GetRequestContext(), | 71 profile->GetRequestContext(), |
69 token_service, | 72 token_service, |
70 signin_manager->GetAuthenticatedAccountId()); | 73 signin_manager->GetAuthenticatedAccountId()); |
71 } | 74 } |
72 | 75 |
73 } // namespace | 76 } // namespace |
74 | 77 |
78 class GcdPrivateRequest : public local_discovery::PrivetV3Session::Request { | |
79 public: | |
80 GcdPrivateRequest(const std::string& api, | |
81 const base::DictionaryValue& input, | |
82 const GcdPrivateAPI::MessageResponseCallback& callback, | |
83 GcdPrivateSessionHolder* session_holder); | |
84 virtual ~GcdPrivateRequest(); | |
85 | |
86 // local_discovery::PrivetV3Session::Request implementation. | |
87 virtual std::string GetName() OVERRIDE; | |
88 virtual const base::DictionaryValue& GetInput() OVERRIDE; | |
89 virtual void OnError( | |
90 local_discovery::PrivetURLFetcher::ErrorType error) OVERRIDE; | |
91 virtual void OnParsedJson(const base::DictionaryValue& value, | |
92 bool has_error) OVERRIDE; | |
93 | |
94 private: | |
95 std::string api_; | |
96 scoped_ptr<base::DictionaryValue> input_; | |
97 GcdPrivateAPI::MessageResponseCallback callback_; | |
98 GcdPrivateSessionHolder* session_holder_; | |
99 }; | |
100 | |
101 class GcdPrivateSessionHolder | |
102 : public local_discovery::PrivetV3Session::Delegate { | |
103 public: | |
104 typedef base::Callback<void(api::gcd_private::Status status, | |
105 const std::string& code, | |
106 api::gcd_private::ConfirmationType type)> | |
107 ConfirmationCodeCallback; | |
108 | |
109 GcdPrivateSessionHolder(const std::string& ip_address, | |
110 int port, | |
111 net::URLRequestContextGetter* request_context); | |
112 virtual ~GcdPrivateSessionHolder(); | |
113 | |
114 void Start(const ConfirmationCodeCallback& callback); | |
115 | |
116 void ConfirmCode(const GcdPrivateAPI::SessionEstablishedCallback& callback); | |
117 | |
118 void SendMessage(const std::string& api, | |
119 const base::DictionaryValue& input, | |
120 GcdPrivateAPI::MessageResponseCallback callback); | |
121 | |
122 void DeleteRequest(GcdPrivateRequest* request); | |
123 | |
124 private: | |
125 // local_discovery::PrivetV3Session::Delegate implementation. | |
126 virtual void OnSetupConfirmationNeeded( | |
127 const std::string& confirmation_code) OVERRIDE; | |
128 virtual void OnSessionEstablished() OVERRIDE; | |
129 virtual void OnCannotEstablishSession() OVERRIDE; | |
130 | |
131 scoped_ptr<local_discovery::PrivetHTTPClient> http_client_; | |
132 scoped_ptr<local_discovery::PrivetV3Session> privet_session_; | |
133 typedef ScopedVector<GcdPrivateRequest> RequestVector; | |
134 RequestVector requests_; | |
135 | |
136 ConfirmationCodeCallback confirm_callback_; | |
137 GcdPrivateAPI::SessionEstablishedCallback session_established_callback_; | |
138 }; | |
139 | |
75 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) | 140 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) |
76 : num_device_listeners_(0), browser_context_(context) { | 141 : num_device_listeners_(0), last_session_id_(0), browser_context_(context) { |
77 DCHECK(browser_context_); | 142 DCHECK(browser_context_); |
78 if (EventRouter::Get(context)) { | 143 if (EventRouter::Get(context)) { |
79 EventRouter::Get(context) | 144 EventRouter::Get(context) |
80 ->RegisterObserver(this, gcd_private::OnDeviceStateChanged::kEventName); | 145 ->RegisterObserver(this, gcd_private::OnDeviceStateChanged::kEventName); |
81 EventRouter::Get(context) | 146 EventRouter::Get(context) |
82 ->RegisterObserver(this, gcd_private::OnDeviceRemoved::kEventName); | 147 ->RegisterObserver(this, gcd_private::OnDeviceRemoved::kEventName); |
83 } | 148 } |
84 } | 149 } |
85 | 150 |
86 GcdPrivateAPI::~GcdPrivateAPI() { | 151 GcdPrivateAPI::~GcdPrivateAPI() { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 | 235 |
171 bool GcdPrivateAPI::QueryForDevices() { | 236 bool GcdPrivateAPI::QueryForDevices() { |
172 if (!privet_device_lister_) | 237 if (!privet_device_lister_) |
173 return false; | 238 return false; |
174 | 239 |
175 privet_device_lister_->DiscoverNewDevices(true); | 240 privet_device_lister_->DiscoverNewDevices(true); |
176 | 241 |
177 return true; | 242 return true; |
178 } | 243 } |
179 | 244 |
245 void GcdPrivateAPI::EstablishSession(std::string ip_address, | |
246 int port, | |
247 ConfirmationCodeCallback callback) { | |
248 int session_id = last_session_id_++; | |
249 linked_ptr<GcdPrivateSessionHolder> session_handler( | |
250 new GcdPrivateSessionHolder( | |
251 ip_address, port, browser_context_->GetRequestContext())); | |
252 sessions_[session_id] = session_handler; | |
253 session_handler->Start(base::Bind(callback, session_id)); | |
254 } | |
255 | |
256 void GcdPrivateAPI::ConfirmCode(int session_id, | |
257 SessionEstablishedCallback callback) { | |
258 GCDSessionMap::iterator found = sessions_.find(session_id); | |
259 | |
260 if (found == sessions_.end()) { | |
261 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR); | |
262 return; | |
263 } | |
264 | |
265 found->second->ConfirmCode(callback); | |
266 } | |
267 | |
268 void GcdPrivateAPI::SendMessage(int session_id, | |
269 const std::string& api, | |
270 const base::DictionaryValue& input, | |
271 MessageResponseCallback callback) { | |
272 GCDSessionMap::iterator found = sessions_.find(session_id); | |
273 | |
274 if (found == sessions_.end()) { | |
275 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR, | |
276 base::DictionaryValue()); | |
277 return; | |
278 } | |
279 | |
280 found->second->SendMessage(api, input, callback); | |
281 } | |
282 | |
283 void GcdPrivateAPI::RemoveSession(int session_id) { | |
284 sessions_.erase(session_id); | |
285 } | |
286 | |
180 // static | 287 // static |
181 void GcdPrivateAPI::SetGCDApiFlowFactoryForTests( | 288 void GcdPrivateAPI::SetGCDApiFlowFactoryForTests( |
182 GCDApiFlowFactoryForTests* factory) { | 289 GCDApiFlowFactoryForTests* factory) { |
183 g_gcd_api_flow_factory = factory; | 290 g_gcd_api_flow_factory = factory; |
184 } | 291 } |
185 | 292 |
293 GcdPrivateRequest::GcdPrivateRequest( | |
294 const std::string& api, | |
295 const base::DictionaryValue& input, | |
296 const GcdPrivateAPI::MessageResponseCallback& callback, | |
297 GcdPrivateSessionHolder* session_holder) | |
298 : api_(api), | |
299 input_(input.DeepCopy()), | |
300 callback_(callback), | |
301 session_holder_(session_holder) { | |
302 } | |
303 | |
304 GcdPrivateRequest::~GcdPrivateRequest() { | |
305 } | |
306 | |
307 std::string GcdPrivateRequest::GetName() { | |
308 return api_; | |
309 } | |
310 | |
311 const base::DictionaryValue& GcdPrivateRequest::GetInput() { | |
312 return *input_; | |
313 } | |
314 | |
315 void GcdPrivateRequest::OnError( | |
316 local_discovery::PrivetURLFetcher::ErrorType error) { | |
317 callback_.Run(gcd_private::STATUS_CONNECTIONERROR, base::DictionaryValue()); | |
318 | |
319 session_holder_->DeleteRequest(this); | |
320 } | |
321 | |
322 void GcdPrivateRequest::OnParsedJson(const base::DictionaryValue& value, | |
323 bool has_error) { | |
324 callback_.Run(gcd_private::STATUS_SUCCESS, value); | |
325 | |
326 session_holder_->DeleteRequest(this); | |
327 } | |
328 | |
329 GcdPrivateSessionHolder::GcdPrivateSessionHolder( | |
330 const std::string& ip_address, | |
331 int port, | |
332 net::URLRequestContextGetter* request_context) { | |
333 std::string host_string; | |
334 | |
335 // HACK: Check if the IP address is an IPv6 address | |
asargent_no_longer_on_chrome
2014/07/25 20:50:10
FYI it looks like there are some helper methods in
Noam Samuel
2014/07/25 21:15:56
Done.
| |
336 if (ip_address.find(':') != std::string::npos) { | |
337 host_string = base::StringPrintf("[%s]", ip_address.c_str()); | |
338 } else { | |
339 host_string = ip_address; | |
340 } | |
341 | |
342 http_client_.reset(new local_discovery::PrivetHTTPClientImpl( | |
343 "", net::HostPortPair(host_string, port), request_context)); | |
344 } | |
345 | |
346 GcdPrivateSessionHolder::~GcdPrivateSessionHolder() { | |
347 } | |
348 | |
349 void GcdPrivateSessionHolder::Start(const ConfirmationCodeCallback& callback) { | |
350 confirm_callback_ = callback; | |
351 | |
352 privet_session_.reset( | |
353 new local_discovery::PrivetV3Session(http_client_.Pass(), this)); | |
354 privet_session_->Start(); | |
355 } | |
356 | |
357 void GcdPrivateSessionHolder::ConfirmCode( | |
358 const GcdPrivateAPI::SessionEstablishedCallback& callback) { | |
359 session_established_callback_ = callback; | |
360 privet_session_->ConfirmCode(); | |
361 } | |
362 | |
363 void GcdPrivateSessionHolder::SendMessage( | |
364 const std::string& api, | |
365 const base::DictionaryValue& input, | |
366 GcdPrivateAPI::MessageResponseCallback callback) { | |
367 GcdPrivateRequest* request = | |
368 new GcdPrivateRequest(api, input, callback, this); | |
369 requests_.push_back(request); | |
370 privet_session_->StartRequest(request); | |
371 } | |
372 | |
373 void GcdPrivateSessionHolder::DeleteRequest(GcdPrivateRequest* request) { | |
374 // TODO(noamsml): Does this need to be optimized? | |
375 for (RequestVector::iterator i = requests_.begin(); i != requests_.end(); | |
376 i++) { | |
377 if (*i == request) { | |
378 requests_.erase(i); | |
379 break; | |
380 } | |
381 } | |
382 } | |
383 | |
384 void GcdPrivateSessionHolder::OnSetupConfirmationNeeded( | |
385 const std::string& confirmation_code) { | |
386 confirm_callback_.Run(gcd_private::STATUS_SUCCESS, | |
387 confirmation_code, | |
388 gcd_private::CONFIRMATION_TYPE_DISPLAYCODE); | |
389 | |
390 confirm_callback_.Reset(); | |
391 } | |
392 | |
393 void GcdPrivateSessionHolder::OnSessionEstablished() { | |
394 session_established_callback_.Run(gcd_private::STATUS_SUCCESS); | |
395 | |
396 session_established_callback_.Reset(); | |
397 } | |
398 | |
399 void GcdPrivateSessionHolder::OnCannotEstablishSession() { | |
400 session_established_callback_.Run(gcd_private::STATUS_SESSIONERROR); | |
401 | |
402 session_established_callback_.Reset(); | |
403 } | |
404 | |
186 GcdPrivateGetCloudDeviceListFunction::GcdPrivateGetCloudDeviceListFunction() { | 405 GcdPrivateGetCloudDeviceListFunction::GcdPrivateGetCloudDeviceListFunction() { |
187 } | 406 } |
188 | 407 |
189 GcdPrivateGetCloudDeviceListFunction::~GcdPrivateGetCloudDeviceListFunction() { | 408 GcdPrivateGetCloudDeviceListFunction::~GcdPrivateGetCloudDeviceListFunction() { |
190 } | 409 } |
191 | 410 |
192 bool GcdPrivateGetCloudDeviceListFunction::RunAsync() { | 411 bool GcdPrivateGetCloudDeviceListFunction::RunAsync() { |
193 requests_succeeded_ = 0; | 412 requests_succeeded_ = 0; |
194 requests_failed_ = 0; | 413 requests_failed_ = 0; |
195 | 414 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 return false; | 515 return false; |
297 } | 516 } |
298 | 517 |
299 GcdPrivateEstablishSessionFunction::GcdPrivateEstablishSessionFunction() { | 518 GcdPrivateEstablishSessionFunction::GcdPrivateEstablishSessionFunction() { |
300 } | 519 } |
301 | 520 |
302 GcdPrivateEstablishSessionFunction::~GcdPrivateEstablishSessionFunction() { | 521 GcdPrivateEstablishSessionFunction::~GcdPrivateEstablishSessionFunction() { |
303 } | 522 } |
304 | 523 |
305 bool GcdPrivateEstablishSessionFunction::RunAsync() { | 524 bool GcdPrivateEstablishSessionFunction::RunAsync() { |
306 return false; | 525 scoped_ptr<gcd_private::EstablishSession::Params> params = |
526 gcd_private::EstablishSession::Params::Create(*args_); | |
527 | |
528 if (!params) | |
529 return false; | |
530 | |
531 GcdPrivateAPI* gcd_api = | |
532 BrowserContextKeyedAPIFactory<GcdPrivateAPI>::Get(GetProfile()); | |
533 | |
534 if (!gcd_api) | |
535 return false; | |
536 | |
537 gcd_api->EstablishSession( | |
538 params->ip_address, | |
539 params->port, | |
540 base::Bind(&GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback, | |
541 this)); | |
542 | |
543 return true; | |
544 } | |
545 | |
546 void GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback( | |
547 int session_id, | |
548 gcd_private::Status status, | |
549 const std::string& confirm_code, | |
550 gcd_private::ConfirmationType confirmation_type) { | |
551 results_ = gcd_private::EstablishSession::Results::Create( | |
552 session_id, status, confirm_code, confirmation_type); | |
553 SendResponse(true); | |
307 } | 554 } |
308 | 555 |
309 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { | 556 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { |
310 } | 557 } |
311 | 558 |
312 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { | 559 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { |
313 } | 560 } |
314 | 561 |
315 bool GcdPrivateConfirmCodeFunction::RunAsync() { | 562 bool GcdPrivateConfirmCodeFunction::RunAsync() { |
316 return false; | 563 scoped_ptr<gcd_private::ConfirmCode::Params> params = |
564 gcd_private::ConfirmCode::Params::Create(*args_); | |
565 | |
566 if (!params) | |
567 return false; | |
568 | |
569 GcdPrivateAPI* gcd_api = | |
570 BrowserContextKeyedAPIFactory<GcdPrivateAPI>::Get(GetProfile()); | |
571 | |
572 if (!gcd_api) | |
573 return false; | |
574 | |
575 gcd_api->ConfirmCode( | |
576 params->session_id, | |
577 base::Bind(&GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback, | |
578 this)); | |
579 | |
580 return true; | |
581 } | |
582 | |
583 void GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback( | |
584 api::gcd_private::Status status) { | |
585 results_ = gcd_private::ConfirmCode::Results::Create(status); | |
586 SendResponse(true); | |
317 } | 587 } |
318 | 588 |
319 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { | 589 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { |
320 } | 590 } |
321 | 591 |
322 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { | 592 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { |
323 } | 593 } |
324 | 594 |
325 bool GcdPrivateSendMessageFunction::RunAsync() { | 595 bool GcdPrivateSendMessageFunction::RunAsync() { |
326 return false; | 596 scoped_ptr<gcd_private::PassMessage::Params> params = |
597 gcd_private::PassMessage::Params::Create(*args_); | |
598 | |
599 if (!params) | |
600 return false; | |
601 | |
602 GcdPrivateAPI* gcd_api = | |
603 BrowserContextKeyedAPIFactory<GcdPrivateAPI>::Get(GetProfile()); | |
604 | |
605 if (!gcd_api) | |
606 return false; | |
607 | |
608 gcd_api->SendMessage( | |
609 params->session_id, | |
610 params->api, | |
611 params->input.additional_properties, | |
612 base::Bind(&GcdPrivateSendMessageFunction::OnMessageSentCallback, this)); | |
613 | |
614 return true; | |
615 } | |
616 | |
617 void GcdPrivateSendMessageFunction::OnMessageSentCallback( | |
618 api::gcd_private::Status status, | |
619 const base::DictionaryValue& value) { | |
620 gcd_private::PassMessage::Results::Response response; | |
621 response.additional_properties.MergeDictionary(&value); | |
622 | |
623 results_ = gcd_private::PassMessage::Results::Create(status, response); | |
624 SendResponse(true); | |
327 } | 625 } |
328 | 626 |
329 GcdPrivateTerminateSessionFunction::GcdPrivateTerminateSessionFunction() { | 627 GcdPrivateTerminateSessionFunction::GcdPrivateTerminateSessionFunction() { |
330 } | 628 } |
331 | 629 |
332 GcdPrivateTerminateSessionFunction::~GcdPrivateTerminateSessionFunction() { | 630 GcdPrivateTerminateSessionFunction::~GcdPrivateTerminateSessionFunction() { |
333 } | 631 } |
334 | 632 |
335 bool GcdPrivateTerminateSessionFunction::RunAsync() { | 633 bool GcdPrivateTerminateSessionFunction::RunAsync() { |
336 return false; | 634 scoped_ptr<gcd_private::TerminateSession::Params> params = |
635 gcd_private::TerminateSession::Params::Create(*args_); | |
636 | |
637 if (!params) | |
638 return false; | |
639 | |
640 GcdPrivateAPI* gcd_api = | |
641 BrowserContextKeyedAPIFactory<GcdPrivateAPI>::Get(GetProfile()); | |
642 | |
643 if (!gcd_api) | |
644 return false; | |
645 | |
646 gcd_api->RemoveSession(params->session_id); | |
647 | |
648 SendResponse(true); | |
649 return true; | |
337 } | 650 } |
338 | 651 |
339 GcdPrivateGetCommandDefinitionsFunction:: | 652 GcdPrivateGetCommandDefinitionsFunction:: |
340 GcdPrivateGetCommandDefinitionsFunction() { | 653 GcdPrivateGetCommandDefinitionsFunction() { |
341 } | 654 } |
342 | 655 |
343 GcdPrivateGetCommandDefinitionsFunction:: | 656 GcdPrivateGetCommandDefinitionsFunction:: |
344 ~GcdPrivateGetCommandDefinitionsFunction() { | 657 ~GcdPrivateGetCommandDefinitionsFunction() { |
345 } | 658 } |
346 | 659 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
382 } | 695 } |
383 | 696 |
384 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { | 697 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { |
385 } | 698 } |
386 | 699 |
387 bool GcdPrivateGetCommandsListFunction::RunAsync() { | 700 bool GcdPrivateGetCommandsListFunction::RunAsync() { |
388 return false; | 701 return false; |
389 } | 702 } |
390 | 703 |
391 } // namespace extensions | 704 } // namespace extensions |
OLD | NEW |