OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chrome_to_mobile_service.h" | 5 #include "chrome/browser/chrome_to_mobile_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/guid.h" | 10 #include "base/guid.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "chrome/app/chrome_command_ids.h" | 16 #include "chrome/app/chrome_command_ids.h" |
17 #include "chrome/browser/content_settings/cookie_settings.h" | 17 #include "chrome/browser/content_settings/cookie_settings.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" |
18 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" | 19 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/signin/token_service.h" | 21 #include "chrome/browser/signin/token_service.h" |
21 #include "chrome/browser/signin/token_service_factory.h" | 22 #include "chrome/browser/signin/token_service_factory.h" |
22 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
23 #include "chrome/browser/ui/browser_command_controller.h" | 24 #include "chrome/browser/ui/browser_command_controller.h" |
24 #include "chrome/browser/ui/browser_finder.h" | 25 #include "chrome/browser/ui/browser_finder.h" |
25 #include "chrome/browser/ui/browser_list.h" | 26 #include "chrome/browser/ui/browser_list.h" |
26 #include "chrome/browser/ui/browser_navigator.h" | 27 #include "chrome/browser/ui/browser_navigator.h" |
27 #include "chrome/browser/ui/browser_tabstrip.h" | 28 #include "chrome/browser/ui/browser_tabstrip.h" |
28 #include "chrome/common/chrome_notification_types.h" | 29 #include "chrome/common/chrome_notification_types.h" |
29 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
30 #include "chrome/common/cloud_print/cloud_print_helpers.h" | 31 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
31 #include "chrome/common/net/gaia/gaia_constants.h" | 32 #include "chrome/common/net/gaia/gaia_constants.h" |
32 #include "chrome/common/net/gaia/gaia_urls.h" | 33 #include "chrome/common/net/gaia/gaia_urls.h" |
33 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" | 34 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" |
| 35 #include "chrome/common/pref_names.h" |
34 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
35 #include "content/public/browser/browser_thread.h" | 37 #include "content/public/browser/browser_thread.h" |
36 #include "content/public/browser/notification_details.h" | 38 #include "content/public/browser/notification_details.h" |
37 #include "content/public/browser/notification_source.h" | 39 #include "content/public/browser/notification_source.h" |
38 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
39 #include "net/base/escape.h" | 41 #include "net/base/escape.h" |
40 #include "net/base/load_flags.h" | 42 #include "net/base/load_flags.h" |
41 #include "net/url_request/url_fetcher.h" | 43 #include "net/url_request/url_fetcher.h" |
42 #include "net/url_request/url_request_context_getter.h" | 44 #include "net/url_request/url_request_context_getter.h" |
43 | 45 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 167 |
166 if (command_line->HasSwitch(switches::kDisableChromeToMobile)) | 168 if (command_line->HasSwitch(switches::kDisableChromeToMobile)) |
167 return false; | 169 return false; |
168 | 170 |
169 if (command_line->HasSwitch(switches::kEnableChromeToMobile)) | 171 if (command_line->HasSwitch(switches::kEnableChromeToMobile)) |
170 return true; | 172 return true; |
171 | 173 |
172 return kChromeToMobileEnabled; | 174 return kChromeToMobileEnabled; |
173 } | 175 } |
174 | 176 |
| 177 // static |
| 178 void ChromeToMobileService::RegisterUserPrefs(PrefService* prefs) { |
| 179 prefs->RegisterListPref(prefs::kChromeToMobileDeviceList, |
| 180 PrefService::UNSYNCABLE_PREF); |
| 181 prefs->RegisterInt64Pref(prefs::kChromeToMobileTimestamp, 0, |
| 182 PrefService::UNSYNCABLE_PREF); |
| 183 } |
| 184 |
175 ChromeToMobileService::ChromeToMobileService(Profile* profile) | 185 ChromeToMobileService::ChromeToMobileService(Profile* profile) |
176 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 186 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
177 profile_(profile), | 187 profile_(profile), |
178 cloud_print_url_(new CloudPrintURL(profile)), | 188 cloud_print_url_(new CloudPrintURL(profile)), |
179 cloud_print_accessible_(false) { | 189 cloud_print_accessible_(false) { |
180 // Skip initialization if constructed without a profile. | 190 // Skip initialization if constructed without a profile. |
181 if (profile_) { | 191 if (profile_) { |
182 // Get an access token as soon as the Gaia login refresh token is available. | 192 // Get an access token as soon as the Gaia login refresh token is available. |
183 TokenService* service = TokenServiceFactory::GetForProfile(profile_); | 193 TokenService* service = TokenServiceFactory::GetForProfile(profile_); |
184 registrar_.Add(this, chrome::NOTIFICATION_TOKEN_AVAILABLE, | 194 registrar_.Add(this, chrome::NOTIFICATION_TOKEN_AVAILABLE, |
185 content::Source<TokenService>(service)); | 195 content::Source<TokenService>(service)); |
186 if (service->HasOAuthLoginToken()) | 196 if (service->HasOAuthLoginToken()) |
187 RefreshAccessToken(); | 197 RefreshAccessToken(); |
188 } | 198 } |
189 } | 199 } |
190 | 200 |
191 ChromeToMobileService::~ChromeToMobileService() { | 201 ChromeToMobileService::~ChromeToMobileService() { |
192 while (!snapshots_.empty()) | 202 while (!snapshots_.empty()) |
193 DeleteSnapshot(*snapshots_.begin()); | 203 DeleteSnapshot(*snapshots_.begin()); |
194 } | 204 } |
195 | 205 |
196 bool ChromeToMobileService::HasDevices() { | 206 bool ChromeToMobileService::HasMobiles() { |
197 return !mobiles().empty(); | 207 return !GetMobiles()->empty(); |
| 208 } |
| 209 |
| 210 const base::ListValue* ChromeToMobileService::GetMobiles() const { |
| 211 return profile_->GetPrefs()->GetList(prefs::kChromeToMobileDeviceList); |
198 } | 212 } |
199 | 213 |
200 void ChromeToMobileService::RequestMobileListUpdate() { | 214 void ChromeToMobileService::RequestMobileListUpdate() { |
201 if (access_token_.empty()) | 215 if (access_token_.empty()) |
202 RefreshAccessToken(); | 216 RefreshAccessToken(); |
203 else if (cloud_print_accessible_) | 217 else if (cloud_print_accessible_) |
204 RequestSearch(); | 218 RequestSearch(); |
205 } | 219 } |
206 | 220 |
207 void ChromeToMobileService::GenerateSnapshot(Browser* browser, | 221 void ChromeToMobileService::GenerateSnapshot(Browser* browser, |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 account_info_request_->Start(); | 458 account_info_request_->Start(); |
445 } | 459 } |
446 | 460 |
447 void ChromeToMobileService::RequestSearch() { | 461 void ChromeToMobileService::RequestSearch() { |
448 DCHECK(!access_token_.empty()); | 462 DCHECK(!access_token_.empty()); |
449 | 463 |
450 // Deny requests if cloud print is inaccessible, and deny concurrent requests. | 464 // Deny requests if cloud print is inaccessible, and deny concurrent requests. |
451 if (!cloud_print_accessible_ || search_request_.get()) | 465 if (!cloud_print_accessible_ || search_request_.get()) |
452 return; | 466 return; |
453 | 467 |
| 468 PrefService* prefs = profile_->GetPrefs(); |
| 469 base::TimeTicks previous_search_time = base::TimeTicks::FromInternalValue( |
| 470 prefs->GetInt64(prefs::kChromeToMobileTimestamp)); |
| 471 |
454 // Deny requests before the delay period has passed since the last request. | 472 // Deny requests before the delay period has passed since the last request. |
455 base::TimeDelta elapsed_time = base::TimeTicks::Now() - previous_search_time_; | 473 base::TimeDelta elapsed_time = base::TimeTicks::Now() - previous_search_time; |
456 if (!previous_search_time_.is_null() && | 474 if (!previous_search_time.is_null() && |
457 elapsed_time.InHours() < kSearchRequestDelayHours) | 475 elapsed_time.InHours() < kSearchRequestDelayHours) |
458 return; | 476 return; |
459 | 477 |
460 LogMetric(DEVICES_REQUESTED); | 478 LogMetric(DEVICES_REQUESTED); |
461 | 479 |
462 const GURL service_url(cloud_print_url_->GetCloudPrintServiceURL()); | 480 const GURL service_url(cloud_print_url_->GetCloudPrintServiceURL()); |
463 search_request_.reset(net::URLFetcher::Create(GetSearchURL(service_url), | 481 search_request_.reset(net::URLFetcher::Create(GetSearchURL(service_url), |
464 net::URLFetcher::GET, this)); | 482 net::URLFetcher::GET, this)); |
465 InitRequest(search_request_.get()); | 483 InitRequest(search_request_.get()); |
466 search_request_->Start(); | 484 search_request_->Start(); |
467 previous_search_time_ = base::TimeTicks::Now(); | |
468 } | 485 } |
469 | 486 |
470 void ChromeToMobileService::HandleAccountInfoResponse() { | 487 void ChromeToMobileService::HandleAccountInfoResponse() { |
471 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 488 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
472 | 489 |
473 std::string data; | 490 std::string data; |
474 account_info_request_->GetResponseAsString(&data); | 491 account_info_request_->GetResponseAsString(&data); |
475 account_info_request_.reset(); | 492 account_info_request_.reset(); |
476 | 493 |
477 ListValue* services = NULL; | 494 ListValue* services = NULL; |
(...skipping 13 matching lines...) Expand all Loading... |
491 | 508 |
492 std::string data; | 509 std::string data; |
493 search_request_->GetResponseAsString(&data); | 510 search_request_->GetResponseAsString(&data); |
494 search_request_.reset(); | 511 search_request_.reset(); |
495 | 512 |
496 ListValue* list = NULL; | 513 ListValue* list = NULL; |
497 DictionaryValue* dictionary = NULL; | 514 DictionaryValue* dictionary = NULL; |
498 scoped_ptr<Value> json(base::JSONReader::Read(data)); | 515 scoped_ptr<Value> json(base::JSONReader::Read(data)); |
499 if (json.get() && json->GetAsDictionary(&dictionary) && dictionary && | 516 if (json.get() && json->GetAsDictionary(&dictionary) && dictionary && |
500 dictionary->GetList(cloud_print::kPrinterListValue, &list)) { | 517 dictionary->GetList(cloud_print::kPrinterListValue, &list)) { |
501 mobiles_.Clear(); | 518 ListValue mobiles; |
502 | |
503 std::string type, name, id; | 519 std::string type, name, id; |
504 DictionaryValue* printer = NULL; | 520 DictionaryValue* printer = NULL; |
505 DictionaryValue* mobile = NULL; | 521 DictionaryValue* mobile = NULL; |
506 for (size_t index = 0; index < list->GetSize(); ++index) { | 522 for (size_t index = 0; index < list->GetSize(); ++index) { |
507 if (list->GetDictionary(index, &printer) && | 523 if (list->GetDictionary(index, &printer) && |
508 printer->GetString("type", &type) && | 524 printer->GetString("type", &type) && |
509 (type.compare(kTypeAndroid) == 0 || type.compare(kTypeIOS) == 0)) { | 525 (type.compare(kTypeAndroid) == 0 || type.compare(kTypeIOS) == 0)) { |
510 // Copy just the requisite values from the full |printer| definition. | 526 // Copy just the requisite values from the full |printer| definition. |
511 if (printer->GetString("name", &name) && | 527 if (printer->GetString("name", &name) && |
512 printer->GetString("id", &id)) { | 528 printer->GetString("id", &id)) { |
513 mobile = new DictionaryValue(); | 529 mobile = new DictionaryValue(); |
514 mobile->SetString("type", type); | 530 mobile->SetString("type", type); |
515 mobile->SetString("name", name); | 531 mobile->SetString("name", name); |
516 mobile->SetString("id", id); | 532 mobile->SetString("id", id); |
517 mobiles_.Append(mobile); | 533 mobiles.Append(mobile); |
518 } else { | 534 } else { |
519 NOTREACHED(); | 535 NOTREACHED(); |
520 } | 536 } |
521 } | 537 } |
522 } | 538 } |
523 | 539 |
524 const bool has_devices = HasDevices(); | 540 // Update the mobile list and timestamp in prefs. |
525 if (has_devices) | 541 PrefService* prefs = profile_->GetPrefs(); |
| 542 prefs->Set(prefs::kChromeToMobileDeviceList, mobiles); |
| 543 prefs->SetInt64(prefs::kChromeToMobileTimestamp, |
| 544 base::TimeTicks::Now().ToInternalValue()); |
| 545 |
| 546 const bool has_mobiles = HasMobiles(); |
| 547 if (has_mobiles) |
526 LogMetric(DEVICES_AVAILABLE); | 548 LogMetric(DEVICES_AVAILABLE); |
527 | 549 |
528 for (BrowserList::const_iterator i = BrowserList::begin(); | 550 for (BrowserList::const_iterator i = BrowserList::begin(); |
529 i != BrowserList::end(); ++i) { | 551 i != BrowserList::end(); ++i) { |
530 Browser* browser = *i; | 552 Browser* browser = *i; |
531 if (browser->profile() == profile_) | 553 if (browser->profile() == profile_) |
532 browser->command_controller()->SendToMobileStateChanged(has_devices); | 554 browser->command_controller()->SendToMobileStateChanged(has_mobiles); |
533 } | 555 } |
534 } | 556 } |
535 } | 557 } |
536 | 558 |
537 void ChromeToMobileService::HandleSubmitResponse( | 559 void ChromeToMobileService::HandleSubmitResponse( |
538 const net::URLFetcher* source) { | 560 const net::URLFetcher* source) { |
539 // Get the observer for this response; bail if there is none or it is NULL. | 561 // Get the observer for this response; bail if there is none or it is NULL. |
540 RequestObserverMap::iterator i = request_observer_map_.find(source); | 562 RequestObserverMap::iterator i = request_observer_map_.find(source); |
541 if (i == request_observer_map_.end()) | 563 if (i == request_observer_map_.end()) |
542 return; | 564 return; |
(...skipping 21 matching lines...) Expand all Loading... |
564 | 586 |
565 // Ensure a second response is not sent after reporting failure below. | 587 // Ensure a second response is not sent after reporting failure below. |
566 request_observer_map_.erase(other); | 588 request_observer_map_.erase(other); |
567 break; | 589 break; |
568 } | 590 } |
569 } | 591 } |
570 | 592 |
571 LogMetric(success ? SEND_SUCCESS : SEND_ERROR); | 593 LogMetric(success ? SEND_SUCCESS : SEND_ERROR); |
572 observer->OnSendComplete(success); | 594 observer->OnSendComplete(success); |
573 } | 595 } |
OLD | NEW |