| 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/search_engines/template_url_service.h" | 5 #include "chrome/browser/search_engines/template_url_service.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/environment.h" | 9 #include "base/environment.h" |
| 10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 // Term used when generating a search url. Use something obscure so that on | 62 // Term used when generating a search url. Use something obscure so that on |
| 63 // the rare case the term replaces the URL it's unlikely another keyword would | 63 // the rare case the term replaces the URL it's unlikely another keyword would |
| 64 // have the same url. | 64 // have the same url. |
| 65 const char kReplacementTerm[] = "blah.blah.blah.blah.blah"; | 65 const char kReplacementTerm[] = "blah.blah.blah.blah.blah"; |
| 66 | 66 |
| 67 bool TemplateURLsHaveSamePrefs(const TemplateURL* url1, | 67 bool TemplateURLsHaveSamePrefs(const TemplateURL* url1, |
| 68 const TemplateURL* url2) { | 68 const TemplateURL* url2) { |
| 69 if (url1 == url2) | 69 if (url1 == url2) |
| 70 return true; | 70 return true; |
| 71 return NULL != url1 && | 71 return (url1 != NULL) && (url2 != NULL) && |
| 72 NULL != url2 && | 72 (url1->short_name() == url2->short_name()) && |
| 73 url1->short_name() == url2->short_name() && | 73 (url1->keyword() == url2->keyword()) && |
| 74 url1->keyword() == url2->keyword() && | 74 (url1->url() == url2->url()) && |
| 75 url1->url() == url2->url() && | 75 (url1->suggestions_url() == url2->suggestions_url()) && |
| 76 url1->suggestions_url() == url2->suggestions_url() && | 76 (url1->instant_url() == url2->instant_url()) && |
| 77 url1->instant_url() == url2->instant_url() && | 77 (url1->favicon_url() == url2->favicon_url()) && |
| 78 url1->favicon_url() == url2->favicon_url() && | 78 (url1->safe_for_autoreplace() == url2->safe_for_autoreplace()) && |
| 79 url1->safe_for_autoreplace() == url2->safe_for_autoreplace() && | 79 (url1->show_in_default_list() == url2->show_in_default_list()) && |
| 80 url1->show_in_default_list() == url2->show_in_default_list() && | 80 (url1->input_encodings() == url2->input_encodings()); |
| 81 url1->input_encodings() == url2->input_encodings(); | |
| 82 } | 81 } |
| 83 | 82 |
| 84 } // namespace | 83 } // namespace |
| 85 | 84 |
| 86 | 85 |
| 87 class TemplateURLService::LessWithPrefix { | 86 class TemplateURLService::LessWithPrefix { |
| 88 public: | 87 public: |
| 89 // We want to find the set of keywords that begin with a prefix. The STL | 88 // We want to find the set of keywords that begin with a prefix. The STL |
| 90 // algorithms will return the set of elements that are "equal to" the | 89 // algorithms will return the set of elements that are "equal to" the |
| 91 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When | 90 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 const TemplateURL* TemplateURLService::GetTemplateURLForHost( | 308 const TemplateURL* TemplateURLService::GetTemplateURLForHost( |
| 310 const std::string& host) const { | 309 const std::string& host) const { |
| 311 return provider_map_.GetTemplateURLForHost(host); | 310 return provider_map_.GetTemplateURLForHost(host); |
| 312 } | 311 } |
| 313 | 312 |
| 314 void TemplateURLService::Add(TemplateURL* template_url) { | 313 void TemplateURLService::Add(TemplateURL* template_url) { |
| 315 AddNoNotify(template_url); | 314 AddNoNotify(template_url); |
| 316 NotifyObservers(); | 315 NotifyObservers(); |
| 317 } | 316 } |
| 318 | 317 |
| 318 void TemplateURLService::AddWithOverrides(const TemplateURL* template_url, |
| 319 const string16& short_name, |
| 320 const string16& keyword, |
| 321 const std::string& url) { |
| 322 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url); |
| 323 modifiable_url->data_.short_name = short_name; |
| 324 modifiable_url->data_.SetKeyword(keyword); |
| 325 modifiable_url->SetURL(url); |
| 326 Add(modifiable_url); |
| 327 } |
| 328 |
| 319 void TemplateURLService::Remove(const TemplateURL* template_url) { | 329 void TemplateURLService::Remove(const TemplateURL* template_url) { |
| 320 RemoveNoNotify(template_url); | 330 RemoveNoNotify(template_url); |
| 321 NotifyObservers(); | 331 NotifyObservers(); |
| 322 } | 332 } |
| 323 | 333 |
| 324 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { | 334 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { |
| 325 RemoveAutoGeneratedBetween(created_after, base::Time()); | 335 RemoveAutoGeneratedBetween(created_after, base::Time()); |
| 326 } | 336 } |
| 327 | 337 |
| 328 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, | 338 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 371 |
| 362 Load(); | 372 Load(); |
| 363 if (!loaded_) { | 373 if (!loaded_) { |
| 364 pending_extension_ids_.push_back(extension->id()); | 374 pending_extension_ids_.push_back(extension->id()); |
| 365 return; | 375 return; |
| 366 } | 376 } |
| 367 | 377 |
| 368 const TemplateURL* existing_url = GetTemplateURLForExtension(extension); | 378 const TemplateURL* existing_url = GetTemplateURLForExtension(extension); |
| 369 string16 keyword = UTF8ToUTF16(extension->omnibox_keyword()); | 379 string16 keyword = UTF8ToUTF16(extension->omnibox_keyword()); |
| 370 | 380 |
| 371 scoped_ptr<TemplateURL> template_url(new TemplateURL); | 381 TemplateURLData data; |
| 372 template_url->set_short_name(UTF8ToUTF16(extension->name())); | 382 data.short_name = UTF8ToUTF16(extension->name()); |
| 373 template_url->set_keyword(keyword); | 383 data.SetKeyword(UTF8ToUTF16(extension->omnibox_keyword())); |
| 374 // This URL is not actually used for navigation. It holds the extension's | 384 // This URL is not actually used for navigation. It holds the extension's |
| 375 // ID, as well as forcing the TemplateURL to be treated as a search keyword. | 385 // ID, as well as forcing the TemplateURL to be treated as a search keyword. |
| 376 template_url->SetURL( | 386 data.SetURL(std::string(chrome::kExtensionScheme) + "://" + extension->id() + |
| 377 std::string(chrome::kExtensionScheme) + "://" + | 387 "/?q={searchTerms}"); |
| 378 extension->id() + "/?q={searchTerms}"); | 388 scoped_ptr<TemplateURL> template_url(new TemplateURL(data)); |
| 379 template_url->set_safe_for_autoreplace(false); | |
| 380 | 389 |
| 381 if (existing_url) { | 390 if (existing_url) { |
| 382 // TODO(mpcomplete): only replace if the user hasn't changed the keyword. | 391 // TODO(mpcomplete): only replace if the user hasn't changed the keyword. |
| 383 // (We don't have UI for that yet). | 392 // (We don't have UI for that yet). |
| 384 UpdateNoNotify(existing_url, *template_url); | 393 UpdateNoNotify(existing_url, *template_url); |
| 385 } else { | 394 } else { |
| 386 AddNoNotify(template_url.release()); | 395 AddNoNotify(template_url.release()); |
| 387 } | 396 } |
| 388 NotifyObservers(); | 397 NotifyObservers(); |
| 389 } | 398 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 406 | 415 |
| 407 return NULL; | 416 return NULL; |
| 408 } | 417 } |
| 409 | 418 |
| 410 std::vector<const TemplateURL*> TemplateURLService::GetTemplateURLs() const { | 419 std::vector<const TemplateURL*> TemplateURLService::GetTemplateURLs() const { |
| 411 return template_urls_; | 420 return template_urls_; |
| 412 } | 421 } |
| 413 | 422 |
| 414 void TemplateURLService::IncrementUsageCount(const TemplateURL* url) { | 423 void TemplateURLService::IncrementUsageCount(const TemplateURL* url) { |
| 415 DCHECK(url && std::find(template_urls_.begin(), template_urls_.end(), url) != | 424 DCHECK(url && std::find(template_urls_.begin(), template_urls_.end(), url) != |
| 416 template_urls_.end()); | 425 template_urls_.end()); |
| 417 const_cast<TemplateURL*>(url)->set_usage_count(url->usage_count() + 1); | 426 ++const_cast<TemplateURL*>(url)->data_.usage_count; |
| 418 if (service_.get()) | 427 if (service_.get()) |
| 419 service_.get()->UpdateKeyword(*url); | 428 service_->UpdateKeyword(*url); |
| 420 } | 429 } |
| 421 | 430 |
| 422 void TemplateURLService::ResetTemplateURL(const TemplateURL* url, | 431 void TemplateURLService::ResetTemplateURL(const TemplateURL* url, |
| 423 const string16& title, | 432 const string16& title, |
| 424 const string16& keyword, | 433 const string16& keyword, |
| 425 const std::string& search_url) { | 434 const std::string& search_url) { |
| 426 TemplateURL new_url(*url); | 435 TemplateURLData data(url->data()); |
| 427 new_url.set_short_name(title); | 436 data.short_name = title; |
| 428 new_url.set_keyword(keyword); | 437 data.SetKeyword(keyword); |
| 429 if (new_url.url() != search_url) { | 438 if (search_url != data.url()) { |
| 430 new_url.SetURL(search_url); | 439 data.SetURL(search_url); |
| 431 // The urls have changed, reset the favicon url. | 440 // The urls have changed, reset the favicon url. |
| 432 new_url.set_favicon_url(GURL()); | 441 data.favicon_url = GURL(); |
| 433 } | 442 } |
| 434 new_url.set_safe_for_autoreplace(false); | 443 data.safe_for_autoreplace = false; |
| 435 new_url.set_last_modified(time_provider_()); | 444 data.last_modified = time_provider_(); |
| 445 TemplateURL new_url(data); |
| 436 UpdateNoNotify(url, new_url); | 446 UpdateNoNotify(url, new_url); |
| 437 NotifyObservers(); | 447 NotifyObservers(); |
| 438 } | 448 } |
| 439 | 449 |
| 440 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { | 450 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { |
| 441 return url != GetDefaultSearchProvider() && | 451 return url != GetDefaultSearchProvider() && |
| 442 url->url_ref().SupportsReplacement() && !is_default_search_managed(); | 452 url->url_ref().SupportsReplacement() && !is_default_search_managed(); |
| 443 } | 453 } |
| 444 | 454 |
| 445 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { | 455 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 std::vector<TemplateURL*> template_urls; | 531 std::vector<TemplateURL*> template_urls; |
| 522 const TemplateURL* default_search_provider = NULL; | 532 const TemplateURL* default_search_provider = NULL; |
| 523 int new_resource_keyword_version = 0; | 533 int new_resource_keyword_version = 0; |
| 524 GetSearchProvidersUsingKeywordResult(*result, | 534 GetSearchProvidersUsingKeywordResult(*result, |
| 525 service_.get(), | 535 service_.get(), |
| 526 GetPrefs(), | 536 GetPrefs(), |
| 527 &template_urls, | 537 &template_urls, |
| 528 &default_search_provider, | 538 &default_search_provider, |
| 529 &new_resource_keyword_version); | 539 &new_resource_keyword_version); |
| 530 | 540 |
| 531 bool database_specified_a_default = NULL != default_search_provider; | 541 bool database_specified_a_default = (default_search_provider != NULL); |
| 532 | 542 |
| 533 // Check if default search provider is now managed. | 543 // Check if default search provider is now managed. |
| 534 scoped_ptr<TemplateURL> default_from_prefs; | 544 scoped_ptr<TemplateURL> default_from_prefs; |
| 535 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, | 545 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, |
| 536 &is_default_search_managed_); | 546 &is_default_search_managed_); |
| 537 | 547 |
| 538 // Check if the default search provider has been changed in Web Data by | 548 // Check if the default search provider has been changed in Web Data by |
| 539 // another program. No immediate action is performed because the default | 549 // another program. No immediate action is performed because the default |
| 540 // search may be changed below by Sync which effectively undoes the hijacking. | 550 // search may be changed below by Sync which effectively undoes the hijacking. |
| 541 bool is_default_search_hijacked = false; | 551 bool is_default_search_hijacked = false; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 561 | 571 |
| 562 if (is_default_search_managed_) { | 572 if (is_default_search_managed_) { |
| 563 SetTemplateURLs(template_urls); | 573 SetTemplateURLs(template_urls); |
| 564 | 574 |
| 565 if (TemplateURLsHaveSamePrefs(default_search_provider, | 575 if (TemplateURLsHaveSamePrefs(default_search_provider, |
| 566 default_from_prefs.get())) { | 576 default_from_prefs.get())) { |
| 567 // The value from the preferences was previously stored in the database. | 577 // The value from the preferences was previously stored in the database. |
| 568 // Reuse it. | 578 // Reuse it. |
| 569 } else { | 579 } else { |
| 570 // The value from the preferences takes over. | 580 // The value from the preferences takes over. |
| 571 // | 581 default_search_provider = NULL; |
| 572 // AddNoNotify will take ownership of default_from_prefs so it is safe to | 582 if (default_from_prefs.get()) { |
| 573 // release. If it's null, there's no ownership to worry about :-) | 583 TemplateURLData data(default_from_prefs->data()); |
| 574 TemplateURL* managed_default = default_from_prefs.release(); | 584 data.created_by_policy = true; |
| 575 if (managed_default) { | 585 data.id = kInvalidTemplateURLID; |
| 576 managed_default->set_created_by_policy(true); | 586 TemplateURL* managed_default = new TemplateURL(data); |
| 577 managed_default->set_id(kInvalidTemplateURLID); | |
| 578 AddNoNotify(managed_default); | 587 AddNoNotify(managed_default); |
| 588 default_search_provider = managed_default; |
| 579 } | 589 } |
| 580 default_search_provider = managed_default; | |
| 581 } | 590 } |
| 582 // Note that this saves the default search provider to prefs. | 591 // Note that this saves the default search provider to prefs. |
| 583 SetDefaultSearchProviderNoNotify(default_search_provider); | 592 SetDefaultSearchProviderNoNotify(default_search_provider); |
| 584 } else { | 593 } else { |
| 585 // If we had a managed default, replace it with the synced default if | 594 // If we had a managed default, replace it with the synced default if |
| 586 // applicable, or the first provider of the list. | 595 // applicable, or the first provider of the list. |
| 587 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); | 596 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); |
| 588 if (synced_default) { | 597 if (synced_default) { |
| 589 default_search_provider = synced_default; | 598 default_search_provider = synced_default; |
| 590 pending_synced_default_search_ = false; | 599 pending_synced_default_search_ = false; |
| 591 } else if (database_specified_a_default && | 600 } else if (database_specified_a_default && |
| 592 NULL == default_search_provider && | 601 default_search_provider == NULL && |
| 593 !template_urls.empty()) { | 602 !template_urls.empty()) { |
| 594 default_search_provider = template_urls[0]; | 603 default_search_provider = template_urls[0]; |
| 595 } | 604 } |
| 596 | 605 |
| 597 // If the default search provider existed previously, then just | 606 // If the default search provider existed previously, then just |
| 598 // set the member variable. Otherwise, we'll set it using the method | 607 // set the member variable. Otherwise, we'll set it using the method |
| 599 // to ensure that it is saved properly after its id is set. | 608 // to ensure that it is saved properly after its id is set. |
| 600 if (default_search_provider && | 609 if (default_search_provider && |
| 601 (default_search_provider->id() != kInvalidTemplateURLID)) { | 610 (default_search_provider->id() != kInvalidTemplateURLID)) { |
| 602 default_search_provider_ = default_search_provider; | 611 default_search_provider_ = default_search_provider; |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 } | 761 } |
| 753 | 762 |
| 754 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); | 763 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); |
| 755 | 764 |
| 756 SyncChangeList new_changes; | 765 SyncChangeList new_changes; |
| 757 SyncError error; | 766 SyncError error; |
| 758 for (SyncChangeList::const_iterator iter = change_list.begin(); | 767 for (SyncChangeList::const_iterator iter = change_list.begin(); |
| 759 iter != change_list.end(); ++iter) { | 768 iter != change_list.end(); ++iter) { |
| 760 DCHECK_EQ(syncable::SEARCH_ENGINES, iter->sync_data().GetDataType()); | 769 DCHECK_EQ(syncable::SEARCH_ENGINES, iter->sync_data().GetDataType()); |
| 761 | 770 |
| 762 scoped_ptr<TemplateURL> turl( | 771 std::string guid = |
| 763 CreateTemplateURLFromSyncData(iter->sync_data())); | 772 iter->sync_data().GetSpecifics().search_engine().sync_guid(); |
| 764 if (!turl.get()) { | 773 const TemplateURL* existing_turl = GetTemplateURLForGUID(guid); |
| 765 NOTREACHED() << "Failed to read search engine."; | 774 scoped_ptr<TemplateURL> turl(CreateTemplateURLFromTemplateURLAndSyncData( |
| 766 continue; | 775 existing_turl, iter->sync_data())); |
| 767 } | |
| 768 | 776 |
| 769 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid()); | |
| 770 const TemplateURL* existing_keyword_turl = | 777 const TemplateURL* existing_keyword_turl = |
| 771 GetTemplateURLForKeyword(turl->keyword()); | 778 GetTemplateURLForKeyword(turl->keyword()); |
| 772 | 779 |
| 773 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { | 780 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { |
| 774 bool delete_default = (existing_turl == GetDefaultSearchProvider()); | 781 bool delete_default = (existing_turl == GetDefaultSearchProvider()); |
| 775 | 782 |
| 776 if (delete_default && is_default_search_managed_) { | 783 if (delete_default && is_default_search_managed_) { |
| 777 NOTREACHED() << "Tried to delete managed default search provider"; | 784 NOTREACHED() << "Tried to delete managed default search provider"; |
| 778 } else { | 785 } else { |
| 779 if (delete_default) | 786 if (delete_default) |
| 780 default_search_provider_ = NULL; | 787 default_search_provider_ = NULL; |
| 781 | 788 |
| 782 Remove(existing_turl); | 789 Remove(existing_turl); |
| 783 | 790 |
| 784 if (delete_default) | 791 if (delete_default) |
| 785 SetDefaultSearchProvider(FindNewDefaultSearchProvider()); | 792 SetDefaultSearchProvider(FindNewDefaultSearchProvider()); |
| 786 } | 793 } |
| 787 } else if (iter->change_type() == SyncChange::ACTION_ADD && | 794 } else if (iter->change_type() == SyncChange::ACTION_ADD && |
| 788 !existing_turl) { | 795 !existing_turl) { |
| 789 std::string guid = turl->sync_guid(); | 796 std::string guid = turl->sync_guid(); |
| 790 if (existing_keyword_turl) | 797 if (existing_keyword_turl) |
| 791 ResolveSyncKeywordConflict(turl.get(), &new_changes); | 798 ResolveSyncKeywordConflict(turl.get(), &new_changes); |
| 792 // Force the local ID to kInvalidTemplateURLID so we can add it. | 799 // Force the local ID to kInvalidTemplateURLID so we can add it. |
| 793 turl->set_id(kInvalidTemplateURLID); | 800 TemplateURLData data(turl->data()); |
| 794 Add(turl.release()); | 801 data.id = kInvalidTemplateURLID; |
| 802 Add(new TemplateURL(data)); |
| 795 | 803 |
| 796 // Possibly set the newly added |turl| as the default search provider. | 804 // Possibly set the newly added |turl| as the default search provider. |
| 797 SetDefaultSearchProviderIfNewlySynced(guid); | 805 SetDefaultSearchProviderIfNewlySynced(guid); |
| 798 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && | 806 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && |
| 799 existing_turl) { | 807 existing_turl) { |
| 800 // Possibly resolve a keyword conflict if they have the same keywords but | 808 // Possibly resolve a keyword conflict if they have the same keywords but |
| 801 // are not the same entry. | 809 // are not the same entry. |
| 802 TemplateURL updated_turl(*existing_turl); | |
| 803 UpdateTemplateURLWithSyncData(&updated_turl, iter->sync_data()); | |
| 804 if (existing_keyword_turl && existing_keyword_turl != existing_turl) | 810 if (existing_keyword_turl && existing_keyword_turl != existing_turl) |
| 805 ResolveSyncKeywordConflict(&updated_turl, &new_changes); | 811 ResolveSyncKeywordConflict(turl.get(), &new_changes); |
| 806 UpdateNoNotify(existing_turl, updated_turl); | 812 UpdateNoNotify(existing_turl, *turl); |
| 807 NotifyObservers(); | 813 NotifyObservers(); |
| 808 } else { | 814 } else { |
| 809 // Something really unexpected happened. Either we received an | 815 // Something really unexpected happened. Either we received an |
| 810 // ACTION_INVALID, or Sync is in a crazy state: | 816 // ACTION_INVALID, or Sync is in a crazy state: |
| 811 // . Trying to DELETE or UPDATE a non-existent search engine. | 817 // . Trying to DELETE or UPDATE a non-existent search engine. |
| 812 // . Trying to ADD a search engine that already exists. | 818 // . Trying to ADD a search engine that already exists. |
| 813 NOTREACHED() << "Unexpected sync change state."; | 819 NOTREACHED() << "Unexpected sync change state."; |
| 814 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " + | 820 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " + |
| 815 SyncChange::ChangeTypeToString(iter->change_type()), | 821 SyncChange::ChangeTypeToString(iter->change_type()), |
| 816 syncable::SEARCH_ENGINES); | 822 syncable::SEARCH_ENGINES); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 | 861 |
| 856 SyncChangeList new_changes; | 862 SyncChangeList new_changes; |
| 857 | 863 |
| 858 // Build maps of our sync GUIDs to SyncData. | 864 // Build maps of our sync GUIDs to SyncData. |
| 859 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( | 865 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( |
| 860 GetAllSyncData(syncable::SEARCH_ENGINES)); | 866 GetAllSyncData(syncable::SEARCH_ENGINES)); |
| 861 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); | 867 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); |
| 862 | 868 |
| 863 for (SyncDataMap::const_iterator iter = sync_data_map.begin(); | 869 for (SyncDataMap::const_iterator iter = sync_data_map.begin(); |
| 864 iter != sync_data_map.end(); ++iter) { | 870 iter != sync_data_map.end(); ++iter) { |
| 871 const TemplateURL* local_turl = GetTemplateURLForGUID(iter->first); |
| 865 scoped_ptr<TemplateURL> sync_turl( | 872 scoped_ptr<TemplateURL> sync_turl( |
| 866 CreateTemplateURLFromSyncData(iter->second)); | 873 CreateTemplateURLFromTemplateURLAndSyncData(local_turl, iter->second)); |
| 867 DCHECK(sync_turl.get()); | |
| 868 const TemplateURL* local_turl = GetTemplateURLForGUID(iter->first); | |
| 869 | 874 |
| 870 if (sync_turl->sync_guid().empty()) { | 875 if (sync_turl->sync_guid().empty()) { |
| 871 // Due to a bug, older search engine entries with no sync GUID | 876 // Due to a bug, older search engine entries with no sync GUID |
| 872 // may have been uploaded to the server. This is bad data, so | 877 // may have been uploaded to the server. This is bad data, so |
| 873 // just delete it. | 878 // just delete it. |
| 874 new_changes.push_back( | 879 new_changes.push_back( |
| 875 SyncChange(SyncChange::ACTION_DELETE, iter->second)); | 880 SyncChange(SyncChange::ACTION_DELETE, iter->second)); |
| 876 } else if (local_turl) { | 881 } else if (local_turl) { |
| 877 // This local search engine is already synced. If the timestamp differs | 882 // This local search engine is already synced. If the timestamp differs |
| 878 // from Sync, we need to update locally or to the cloud. Note that if the | 883 // from Sync, we need to update locally or to the cloud. Note that if the |
| 879 // timestamps are equal, we touch neither. | 884 // timestamps are equal, we touch neither. |
| 880 if (sync_turl->last_modified() > local_turl->last_modified()) { | 885 if (sync_turl->last_modified() > local_turl->last_modified()) { |
| 881 // We've received an update from Sync. We should replace all synced | 886 // We've received an update from Sync. We should replace all synced |
| 882 // fields in the local TemplateURL. Note that this includes the | 887 // fields in the local TemplateURL. Note that this includes the |
| 883 // TemplateURLID and the TemplateURL may have to be reparsed. This | 888 // TemplateURLID and the TemplateURL may have to be reparsed. This |
| 884 // also makes the local data's last_modified timestamp equal to Sync's, | 889 // also makes the local data's last_modified timestamp equal to Sync's, |
| 885 // avoiding an Update on the next MergeData call. | 890 // avoiding an Update on the next MergeData call. |
| 886 TemplateURL updated_turl(*local_turl); | 891 UpdateNoNotify(local_turl, *sync_turl); |
| 887 UpdateTemplateURLWithSyncData(&updated_turl, iter->second); | |
| 888 UpdateNoNotify(local_turl, updated_turl); | |
| 889 NotifyObservers(); | 892 NotifyObservers(); |
| 890 } else if (sync_turl->last_modified() < local_turl->last_modified()) { | 893 } else if (sync_turl->last_modified() < local_turl->last_modified()) { |
| 891 // Otherwise, we know we have newer data, so update Sync with our | 894 // Otherwise, we know we have newer data, so update Sync with our |
| 892 // data fields. | 895 // data fields. |
| 893 new_changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, | 896 new_changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, |
| 894 local_data_map[local_turl->sync_guid()])); | 897 local_data_map[local_turl->sync_guid()])); |
| 895 } | 898 } |
| 896 local_data_map.erase(iter->first); | 899 local_data_map.erase(iter->first); |
| 897 } else { | 900 } else { |
| 898 // The search engine from the cloud has not been synced locally, but there | 901 // The search engine from the cloud has not been synced locally, but there |
| (...skipping 10 matching lines...) Expand all Loading... |
| 909 &new_changes); | 912 &new_changes); |
| 910 local_data_map.erase(old_guid); | 913 local_data_map.erase(old_guid); |
| 911 } else { | 914 } else { |
| 912 std::string guid = sync_turl->sync_guid(); | 915 std::string guid = sync_turl->sync_guid(); |
| 913 // Keyword conflict is possible in this case. Resolve it first before | 916 // Keyword conflict is possible in this case. Resolve it first before |
| 914 // adding the new TemplateURL. Note that we don't remove the local TURL | 917 // adding the new TemplateURL. Note that we don't remove the local TURL |
| 915 // from local_data_map in this case as it may still need to be pushed to | 918 // from local_data_map in this case as it may still need to be pushed to |
| 916 // the cloud. | 919 // the cloud. |
| 917 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); | 920 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); |
| 918 // Force the local ID to kInvalidTemplateURLID so we can add it. | 921 // Force the local ID to kInvalidTemplateURLID so we can add it. |
| 919 sync_turl->set_id(kInvalidTemplateURLID); | 922 TemplateURLData data(sync_turl->data()); |
| 920 Add(sync_turl.release()); | 923 data.id = kInvalidTemplateURLID; |
| 924 Add(new TemplateURL(data)); |
| 921 | 925 |
| 922 // Possibly set the newly added |turl| as the default search provider. | 926 // Possibly set the newly added |turl| as the default search provider. |
| 923 SetDefaultSearchProviderIfNewlySynced(guid); | 927 SetDefaultSearchProviderIfNewlySynced(guid); |
| 924 } | 928 } |
| 925 } | 929 } |
| 926 } // for | 930 } // for |
| 927 | 931 |
| 928 // The remaining SyncData in local_data_map should be everything that needs to | 932 // The remaining SyncData in local_data_map should be everything that needs to |
| 929 // be pushed as ADDs to sync. | 933 // be pushed as ADDs to sync. |
| 930 for (SyncDataMap::const_iterator iter = local_data_map.begin(); | 934 for (SyncDataMap::const_iterator iter = local_data_map.begin(); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 994 se_specifics->set_autogenerate_keyword(turl.autogenerate_keyword()); | 998 se_specifics->set_autogenerate_keyword(turl.autogenerate_keyword()); |
| 995 se_specifics->set_instant_url(turl.instant_url()); | 999 se_specifics->set_instant_url(turl.instant_url()); |
| 996 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); | 1000 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); |
| 997 se_specifics->set_sync_guid(turl.sync_guid()); | 1001 se_specifics->set_sync_guid(turl.sync_guid()); |
| 998 return SyncData::CreateLocalData(se_specifics->sync_guid(), | 1002 return SyncData::CreateLocalData(se_specifics->sync_guid(), |
| 999 se_specifics->keyword(), | 1003 se_specifics->keyword(), |
| 1000 specifics); | 1004 specifics); |
| 1001 } | 1005 } |
| 1002 | 1006 |
| 1003 // static | 1007 // static |
| 1004 TemplateURL* TemplateURLService::CreateTemplateURLFromSyncData( | 1008 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( |
| 1009 const TemplateURL* existing_turl, |
| 1005 const SyncData& sync_data) { | 1010 const SyncData& sync_data) { |
| 1006 TemplateURL* turl = new TemplateURL(); | 1011 sync_pb::SearchEngineSpecifics specifics = |
| 1007 UpdateTemplateURLWithSyncData(turl, sync_data); | 1012 sync_data.GetSpecifics().search_engine(); |
| 1008 return turl; | 1013 |
| 1014 TemplateURLData data; |
| 1015 data.short_name = UTF8ToUTF16(specifics.short_name()); |
| 1016 data.originating_url = GURL(specifics.originating_url()); |
| 1017 data.SetKeyword(UTF8ToUTF16(specifics.keyword())); |
| 1018 data.SetAutogenerateKeyword(specifics.autogenerate_keyword()); |
| 1019 data.SetURL(specifics.url()); |
| 1020 data.suggestions_url = specifics.suggestions_url(); |
| 1021 data.instant_url = specifics.instant_url(); |
| 1022 data.favicon_url = GURL(specifics.favicon_url()); |
| 1023 data.show_in_default_list = specifics.show_in_default_list(); |
| 1024 data.safe_for_autoreplace = specifics.safe_for_autoreplace(); |
| 1025 base::SplitString(specifics.input_encodings(), ';', &data.input_encodings); |
| 1026 data.date_created = base::Time::FromInternalValue(specifics.date_created()); |
| 1027 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); |
| 1028 data.prepopulate_id = specifics.prepopulate_id(); |
| 1029 data.sync_guid = specifics.sync_guid(); |
| 1030 if (existing_turl) { |
| 1031 data.id = existing_turl->id(); |
| 1032 data.created_by_policy = existing_turl->created_by_policy(); |
| 1033 data.usage_count = existing_turl->usage_count(); |
| 1034 } |
| 1035 return new TemplateURL(data); |
| 1009 } | 1036 } |
| 1010 | 1037 |
| 1011 // static | 1038 // static |
| 1012 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( | 1039 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( |
| 1013 const SyncDataList& sync_data) { | 1040 const SyncDataList& sync_data) { |
| 1014 SyncDataMap data_map; | 1041 SyncDataMap data_map; |
| 1015 SyncDataList::const_iterator iter; | 1042 SyncDataList::const_iterator iter; |
| 1016 for (iter = sync_data.begin(); iter != sync_data.end(); ++iter) { | 1043 for (iter = sync_data.begin(); iter != sync_data.end(); ++iter) { |
| 1017 data_map[iter->GetSpecifics().search_engine().sync_guid()] = *iter; | 1044 data_map[iter->GetSpecifics().search_engine().sync_guid()] = *iter; |
| 1018 } | 1045 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 | 1086 |
| 1060 size_t template_position = | 1087 size_t template_position = |
| 1061 std::string(initializers[i].url).find(kTemplateParameter); | 1088 std::string(initializers[i].url).find(kTemplateParameter); |
| 1062 DCHECK(template_position != std::string::npos); | 1089 DCHECK(template_position != std::string::npos); |
| 1063 std::string osd_url(initializers[i].url); | 1090 std::string osd_url(initializers[i].url); |
| 1064 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, | 1091 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, |
| 1065 kSearchTermParameter); | 1092 kSearchTermParameter); |
| 1066 | 1093 |
| 1067 // TemplateURLService ends up owning the TemplateURL, don't try and free | 1094 // TemplateURLService ends up owning the TemplateURL, don't try and free |
| 1068 // it. | 1095 // it. |
| 1069 TemplateURL* template_url = new TemplateURL(); | 1096 TemplateURLData data; |
| 1070 template_url->set_keyword(UTF8ToUTF16(initializers[i].keyword)); | 1097 data.short_name = UTF8ToUTF16(initializers[i].content); |
| 1071 template_url->set_short_name(UTF8ToUTF16(initializers[i].content)); | 1098 data.SetKeyword(UTF8ToUTF16(initializers[i].keyword)); |
| 1072 template_url->SetURL(osd_url); | 1099 data.SetURL(osd_url); |
| 1073 AddNoNotify(template_url); | 1100 AddNoNotify(new TemplateURL(data)); |
| 1074 } | 1101 } |
| 1075 } | 1102 } |
| 1076 | 1103 |
| 1077 // Initialize default search. | 1104 // Initialize default search. |
| 1078 UpdateDefaultSearch(); | 1105 UpdateDefaultSearch(); |
| 1079 | 1106 |
| 1080 // Request a server check for the correct Google URL if Google is the | 1107 // Request a server check for the correct Google URL if Google is the |
| 1081 // default search engine, not in headless mode and not in Chrome Frame. | 1108 // default search engine, not in headless mode and not in Chrome Frame. |
| 1082 if (initial_default_search_provider_.get() && | 1109 if (initial_default_search_provider_.get() && |
| 1083 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { | 1110 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1244 string16 keyword = | 1271 string16 keyword = |
| 1245 UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderKeyword)); | 1272 UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderKeyword)); |
| 1246 std::string icon_url = | 1273 std::string icon_url = |
| 1247 prefs->GetString(prefs::kDefaultSearchProviderIconURL); | 1274 prefs->GetString(prefs::kDefaultSearchProviderIconURL); |
| 1248 std::string encodings = | 1275 std::string encodings = |
| 1249 prefs->GetString(prefs::kDefaultSearchProviderEncodings); | 1276 prefs->GetString(prefs::kDefaultSearchProviderEncodings); |
| 1250 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID); | 1277 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID); |
| 1251 std::string prepopulate_id = | 1278 std::string prepopulate_id = |
| 1252 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID); | 1279 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID); |
| 1253 | 1280 |
| 1254 default_provider->reset(new TemplateURL()); | 1281 TemplateURLData data; |
| 1255 (*default_provider)->set_short_name(name); | 1282 data.short_name = name; |
| 1256 (*default_provider)->SetURL(search_url); | 1283 data.SetKeyword(keyword); |
| 1257 (*default_provider)->SetSuggestionsURL(suggest_url); | 1284 data.SetURL(search_url); |
| 1258 (*default_provider)->SetInstantURL(instant_url); | 1285 data.suggestions_url = suggest_url; |
| 1259 (*default_provider)->set_keyword(keyword); | 1286 data.instant_url = instant_url; |
| 1260 (*default_provider)->set_favicon_url(GURL(icon_url)); | 1287 data.favicon_url = GURL(icon_url); |
| 1261 std::vector<std::string> encodings_vector; | 1288 data.show_in_default_list = true; |
| 1262 base::SplitString(encodings, ';', &encodings_vector); | 1289 base::SplitString(encodings, ';', &data.input_encodings); |
| 1263 (*default_provider)->set_input_encodings(encodings_vector); | |
| 1264 if (!id_string.empty() && !*is_managed) { | 1290 if (!id_string.empty() && !*is_managed) { |
| 1265 int64 value; | 1291 int64 value; |
| 1266 base::StringToInt64(id_string, &value); | 1292 base::StringToInt64(id_string, &value); |
| 1267 (*default_provider)->set_id(value); | 1293 data.id = value; |
| 1268 } | 1294 } |
| 1269 if (!prepopulate_id.empty() && !*is_managed) { | 1295 if (!prepopulate_id.empty() && !*is_managed) { |
| 1270 int value; | 1296 int value; |
| 1271 base::StringToInt(prepopulate_id, &value); | 1297 base::StringToInt(prepopulate_id, &value); |
| 1272 (*default_provider)->SetPrepopulateId(value); | 1298 data.prepopulate_id = value; |
| 1273 } | 1299 } |
| 1274 (*default_provider)->set_show_in_default_list(true); | 1300 default_provider->reset(new TemplateURL(data)); |
| 1275 return true; | 1301 return true; |
| 1276 } | 1302 } |
| 1277 | 1303 |
| 1278 bool TemplateURLService::CanReplaceKeywordForHost( | 1304 bool TemplateURLService::CanReplaceKeywordForHost( |
| 1279 const std::string& host, | 1305 const std::string& host, |
| 1280 const TemplateURL** to_replace) { | 1306 const TemplateURL** to_replace) { |
| 1281 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host); | 1307 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host); |
| 1282 if (urls) { | 1308 if (urls) { |
| 1283 for (TemplateURLSet::const_iterator i = urls->begin(); | 1309 for (TemplateURLSet::const_iterator i = urls->begin(); |
| 1284 i != urls->end(); ++i) { | 1310 i != urls->end(); ++i) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1298 | 1324 |
| 1299 bool TemplateURLService::CanReplace(const TemplateURL* t_url) { | 1325 bool TemplateURLService::CanReplace(const TemplateURL* t_url) { |
| 1300 return (t_url != default_search_provider_ && !t_url->show_in_default_list() && | 1326 return (t_url != default_search_provider_ && !t_url->show_in_default_list() && |
| 1301 t_url->safe_for_autoreplace()); | 1327 t_url->safe_for_autoreplace()); |
| 1302 } | 1328 } |
| 1303 | 1329 |
| 1304 void TemplateURLService::UpdateNoNotify(const TemplateURL* existing_turl, | 1330 void TemplateURLService::UpdateNoNotify(const TemplateURL* existing_turl, |
| 1305 const TemplateURL& new_values) { | 1331 const TemplateURL& new_values) { |
| 1306 DCHECK(loaded_); | 1332 DCHECK(loaded_); |
| 1307 DCHECK(existing_turl); | 1333 DCHECK(existing_turl); |
| 1308 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), existing_turl) | 1334 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), |
| 1309 != template_urls_.end()); | 1335 existing_turl) != template_urls_.end()); |
| 1310 | 1336 |
| 1311 if (!existing_turl->keyword().empty()) | 1337 if (!existing_turl->keyword().empty()) |
| 1312 keyword_to_template_map_.erase(existing_turl->keyword()); | 1338 keyword_to_template_map_.erase(existing_turl->keyword()); |
| 1313 if (!existing_turl->sync_guid().empty()) | 1339 if (!existing_turl->sync_guid().empty()) |
| 1314 guid_to_template_map_.erase(existing_turl->sync_guid()); | 1340 guid_to_template_map_.erase(existing_turl->sync_guid()); |
| 1315 | 1341 |
| 1316 // This call handles copying over the values (while retaining the id). | 1342 provider_map_.Remove(existing_turl); |
| 1343 TemplateURLID previous_id = existing_turl->id(); |
| 1344 TemplateURL* modifiable_turl = const_cast<TemplateURL*>(existing_turl); |
| 1345 *modifiable_turl = new_values; |
| 1346 modifiable_turl->data_.id = previous_id; |
| 1317 UIThreadSearchTermsData search_terms_data; | 1347 UIThreadSearchTermsData search_terms_data; |
| 1318 provider_map_.Update(existing_turl, new_values, search_terms_data); | 1348 provider_map_.Add(existing_turl, search_terms_data); |
| 1319 | 1349 |
| 1320 if (!existing_turl->keyword().empty()) | 1350 if (!existing_turl->keyword().empty()) |
| 1321 keyword_to_template_map_[existing_turl->keyword()] = existing_turl; | 1351 keyword_to_template_map_[existing_turl->keyword()] = existing_turl; |
| 1322 if (!existing_turl->sync_guid().empty()) | 1352 if (!existing_turl->sync_guid().empty()) |
| 1323 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; | 1353 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; |
| 1324 | 1354 |
| 1325 if (service_.get()) | 1355 if (service_.get()) |
| 1326 service_->UpdateKeyword(*existing_turl); | 1356 service_->UpdateKeyword(*existing_turl); |
| 1327 | 1357 |
| 1328 // Inform sync of the update. | 1358 // Inform sync of the update. |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1513 new_default_from_prefs.get())) | 1543 new_default_from_prefs.get())) |
| 1514 return; | 1544 return; |
| 1515 if (new_default_from_prefs.get() == NULL) { | 1545 if (new_default_from_prefs.get() == NULL) { |
| 1516 // default_search_provider_ can't be NULL otherwise | 1546 // default_search_provider_ can't be NULL otherwise |
| 1517 // TemplateURLsHaveSamePrefs would have returned true. Remove this now | 1547 // TemplateURLsHaveSamePrefs would have returned true. Remove this now |
| 1518 // invalid value. | 1548 // invalid value. |
| 1519 const TemplateURL* old_default = default_search_provider_; | 1549 const TemplateURL* old_default = default_search_provider_; |
| 1520 SetDefaultSearchProviderNoNotify(NULL); | 1550 SetDefaultSearchProviderNoNotify(NULL); |
| 1521 RemoveNoNotify(old_default); | 1551 RemoveNoNotify(old_default); |
| 1522 } else if (default_search_provider_) { | 1552 } else if (default_search_provider_) { |
| 1523 new_default_from_prefs->set_created_by_policy(true); | 1553 TemplateURLData data(new_default_from_prefs->data()); |
| 1524 UpdateNoNotify(default_search_provider_, *new_default_from_prefs.get()); | 1554 data.created_by_policy = true; |
| 1555 TemplateURL new_values(data); |
| 1556 UpdateNoNotify(default_search_provider_, new_values); |
| 1525 } else { | 1557 } else { |
| 1526 // AddNoNotify will take ownership of new_template, so it's safe to | 1558 // AddNoNotify will take ownership of new_template, so it's safe to |
| 1527 // release. | 1559 // release. |
| 1528 TemplateURL* new_template = new_default_from_prefs.release(); | 1560 TemplateURL* new_template = NULL; |
| 1529 if (new_template) { | 1561 if (new_default_from_prefs.get()) { |
| 1530 new_template->set_created_by_policy(true); | 1562 TemplateURLData data(new_default_from_prefs->data()); |
| 1563 data.created_by_policy = true; |
| 1564 new_template = new TemplateURL(data); |
| 1531 AddNoNotify(new_template); | 1565 AddNoNotify(new_template); |
| 1532 } | 1566 } |
| 1533 SetDefaultSearchProviderNoNotify(new_template); | 1567 SetDefaultSearchProviderNoNotify(new_template); |
| 1534 } | 1568 } |
| 1535 } else if (!is_default_search_managed_ && new_is_default_managed) { | 1569 } else if (!is_default_search_managed_ && new_is_default_managed) { |
| 1536 // The default used to be unmanaged and is now managed. Add the new | 1570 // The default used to be unmanaged and is now managed. Add the new |
| 1537 // managed default to the list of URLs and set it as default. | 1571 // managed default to the list of URLs and set it as default. |
| 1538 is_default_search_managed_ = new_is_default_managed; | 1572 is_default_search_managed_ = new_is_default_managed; |
| 1539 // AddNoNotify will take ownership of new_template, so it's safe to | 1573 // AddNoNotify will take ownership of new_template, so it's safe to |
| 1540 // release. | 1574 // release. |
| 1541 TemplateURL* new_template = new_default_from_prefs.release(); | 1575 TemplateURL* new_template = NULL; |
| 1542 if (new_template) { | 1576 if (new_default_from_prefs.get()) { |
| 1543 new_template->set_created_by_policy(true); | 1577 TemplateURLData data(new_default_from_prefs->data()); |
| 1578 data.created_by_policy = true; |
| 1579 new_template = new TemplateURL(data); |
| 1544 AddNoNotify(new_template); | 1580 AddNoNotify(new_template); |
| 1545 } | 1581 } |
| 1546 SetDefaultSearchProviderNoNotify(new_template); | 1582 SetDefaultSearchProviderNoNotify(new_template); |
| 1547 } else { | 1583 } else { |
| 1548 // The default was managed and is no longer. | 1584 // The default was managed and is no longer. |
| 1549 DCHECK(is_default_search_managed_ && !new_is_default_managed); | 1585 DCHECK(is_default_search_managed_ && !new_is_default_managed); |
| 1550 is_default_search_managed_ = new_is_default_managed; | 1586 is_default_search_managed_ = new_is_default_managed; |
| 1551 // If we had a default, delete the previous default if created by policy | 1587 // If we had a default, delete the previous default if created by policy |
| 1552 // and set a likely default. | 1588 // and set a likely default. |
| 1553 if (NULL != default_search_provider_ && | 1589 if ((default_search_provider_ != NULL) && |
| 1554 default_search_provider_->created_by_policy()) { | 1590 default_search_provider_->created_by_policy()) { |
| 1555 const TemplateURL* old_default = default_search_provider_; | 1591 const TemplateURL* old_default = default_search_provider_; |
| 1556 default_search_provider_ = NULL; | 1592 default_search_provider_ = NULL; |
| 1557 RemoveNoNotify(old_default); | 1593 RemoveNoNotify(old_default); |
| 1558 } | 1594 } |
| 1559 | 1595 |
| 1560 // The likely default should be from Sync if we were waiting on Sync. | 1596 // The likely default should be from Sync if we were waiting on Sync. |
| 1561 // Otherwise, it should be FindNewDefaultSearchProvider. | 1597 // Otherwise, it should be FindNewDefaultSearchProvider. |
| 1562 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); | 1598 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); |
| 1563 if (synced_default) | 1599 if (synced_default) |
| 1564 pending_synced_default_search_ = false; | 1600 pending_synced_default_search_ = false; |
| 1565 SetDefaultSearchProviderNoNotify(synced_default ? synced_default : | 1601 SetDefaultSearchProviderNoNotify(synced_default ? synced_default : |
| 1566 FindNewDefaultSearchProvider()); | 1602 FindNewDefaultSearchProvider()); |
| 1567 } | 1603 } |
| 1568 NotifyObservers(); | 1604 NotifyObservers(); |
| 1569 } | 1605 } |
| 1570 | 1606 |
| 1571 void TemplateURLService::SetDefaultSearchProviderNoNotify( | 1607 void TemplateURLService::SetDefaultSearchProviderNoNotify( |
| 1572 const TemplateURL* url) { | 1608 const TemplateURL* url) { |
| 1573 DCHECK(!url || std::find(template_urls_.begin(), template_urls_.end(), url) | 1609 DCHECK(!url || std::find(template_urls_.begin(), template_urls_.end(), url) != |
| 1574 != template_urls_.end()); | 1610 template_urls_.end()); |
| 1575 default_search_provider_ = url; | 1611 default_search_provider_ = url; |
| 1576 | 1612 |
| 1577 if (url) { | 1613 if (url) { |
| 1578 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); | 1614 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); |
| 1579 // Don't mark the url as edited, otherwise we won't be able to rev the | 1615 // Don't mark the url as edited, otherwise we won't be able to rev the |
| 1580 // template urls we ship with. | 1616 // template urls we ship with. |
| 1581 modifiable_url->set_show_in_default_list(true); | 1617 modifiable_url->data_.show_in_default_list = true; |
| 1582 if (service_.get()) | 1618 if (service_.get()) |
| 1583 service_.get()->UpdateKeyword(*url); | 1619 service_->UpdateKeyword(*url); |
| 1584 | 1620 |
| 1585 if (url->url_ref().HasGoogleBaseURLs()) { | 1621 if (url->url_ref().HasGoogleBaseURLs()) { |
| 1586 GoogleURLTracker::RequestServerCheck(); | 1622 GoogleURLTracker::RequestServerCheck(); |
| 1587 #if defined(ENABLE_RLZ) | 1623 #if defined(ENABLE_RLZ) |
| 1588 // Needs to be evaluated. See http://crbug.com/62328. | 1624 // Needs to be evaluated. See http://crbug.com/62328. |
| 1589 base::ThreadRestrictions::ScopedAllowIO allow_io; | 1625 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 1590 RLZTracker::RecordProductEvent(rlz_lib::CHROME, | 1626 RLZTracker::RecordProductEvent(rlz_lib::CHROME, |
| 1591 rlz_lib::CHROME_OMNIBOX, | 1627 rlz_lib::CHROME_OMNIBOX, |
| 1592 rlz_lib::SET_TO_GOOGLE); | 1628 rlz_lib::SET_TO_GOOGLE); |
| 1593 #endif | 1629 #endif |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1610 service_->SetDefaultSearchProvider(url); | 1646 service_->SetDefaultSearchProvider(url); |
| 1611 | 1647 |
| 1612 // Inform sync the change to the show_in_default_list flag. | 1648 // Inform sync the change to the show_in_default_list flag. |
| 1613 if (url) | 1649 if (url) |
| 1614 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); | 1650 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); |
| 1615 } | 1651 } |
| 1616 | 1652 |
| 1617 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { | 1653 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { |
| 1618 DCHECK(template_url); | 1654 DCHECK(template_url); |
| 1619 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 1655 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
| 1620 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), template_url) | 1656 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), |
| 1621 == template_urls_.end()); | 1657 template_url) == template_urls_.end()); |
| 1622 template_url->set_id(++next_id_); | 1658 template_url->data_.id = ++next_id_; |
| 1623 template_urls_.push_back(template_url); | 1659 template_urls_.push_back(template_url); |
| 1624 AddToMaps(template_url); | 1660 AddToMaps(template_url); |
| 1625 | 1661 |
| 1626 if (service_.get()) | 1662 if (service_.get()) |
| 1627 service_->AddKeyword(*template_url); | 1663 service_->AddKeyword(*template_url); |
| 1628 | 1664 |
| 1629 // Inform sync of the addition. Note that this will assign a GUID to | 1665 // Inform sync of the addition. Note that this will assign a GUID to |
| 1630 // template_url and add it to the guid_to_template_map_. | 1666 // template_url and add it to the guid_to_template_map_. |
| 1631 ProcessTemplateURLChange(template_url, SyncChange::ACTION_ADD); | 1667 ProcessTemplateURLChange(template_url, SyncChange::ACTION_ADD); |
| 1632 } | 1668 } |
| 1633 | 1669 |
| 1634 void TemplateURLService::RemoveNoNotify(const TemplateURL* template_url) { | 1670 void TemplateURLService::RemoveNoNotify(const TemplateURL* template_url) { |
| 1635 TemplateURLVector::iterator i = std::find(template_urls_.begin(), | 1671 TemplateURLVector::iterator i = |
| 1636 template_urls_.end(), | 1672 std::find(template_urls_.begin(), template_urls_.end(), template_url); |
| 1637 template_url); | |
| 1638 if (i == template_urls_.end()) | 1673 if (i == template_urls_.end()) |
| 1639 return; | 1674 return; |
| 1640 | 1675 |
| 1641 if (template_url == default_search_provider_) { | 1676 if (template_url == default_search_provider_) { |
| 1642 // Should never delete the default search provider. | 1677 // Should never delete the default search provider. |
| 1643 NOTREACHED(); | 1678 NOTREACHED(); |
| 1644 return; | 1679 return; |
| 1645 } | 1680 } |
| 1646 | 1681 |
| 1647 RemoveFromMaps(template_url); | 1682 RemoveFromMaps(template_url); |
| 1648 | 1683 |
| 1649 // Remove it from the vector containing all TemplateURLs. | 1684 // Remove it from the vector containing all TemplateURLs. |
| 1650 template_urls_.erase(i); | 1685 template_urls_.erase(i); |
| 1651 | 1686 |
| 1652 if (service_.get()) | 1687 if (service_.get()) |
| 1653 service_->RemoveKeyword(*template_url); | 1688 service_->RemoveKeyword(template_url->id()); |
| 1654 | 1689 |
| 1655 // Inform sync of the deletion. | 1690 // Inform sync of the deletion. |
| 1656 ProcessTemplateURLChange(template_url, SyncChange::ACTION_DELETE); | 1691 ProcessTemplateURLChange(template_url, SyncChange::ACTION_DELETE); |
| 1657 | 1692 |
| 1658 if (profile_) { | 1693 if (profile_) { |
| 1659 content::Source<Profile> source(profile_); | 1694 content::Source<Profile> source(profile_); |
| 1660 TemplateURLID id = template_url->id(); | 1695 TemplateURLID id = template_url->id(); |
| 1661 content::NotificationService::current()->Notify( | 1696 content::NotificationService::current()->Notify( |
| 1662 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, | 1697 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, |
| 1663 source, | 1698 source, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1708 | 1743 |
| 1709 // The database loaded a managed |default_search_provider|, but it has | 1744 // The database loaded a managed |default_search_provider|, but it has |
| 1710 // been updated in the prefs. Remove it from the database, and update the | 1745 // been updated in the prefs. Remove it from the database, and update the |
| 1711 // |default_search_provider| pointer here. | 1746 // |default_search_provider| pointer here. |
| 1712 if (*default_search_provider && | 1747 if (*default_search_provider && |
| 1713 (*default_search_provider)->id() == template_url->id()) | 1748 (*default_search_provider)->id() == template_url->id()) |
| 1714 *default_search_provider = NULL; | 1749 *default_search_provider = NULL; |
| 1715 | 1750 |
| 1716 i = template_urls->erase(i); | 1751 i = template_urls->erase(i); |
| 1717 if (service_.get()) | 1752 if (service_.get()) |
| 1718 service_->RemoveKeyword(*template_url); | 1753 service_->RemoveKeyword(template_url->id()); |
| 1719 delete template_url; | 1754 delete template_url; |
| 1720 } else { | 1755 } else { |
| 1721 ++i; | 1756 ++i; |
| 1722 } | 1757 } |
| 1723 } | 1758 } |
| 1724 } | 1759 } |
| 1725 | 1760 |
| 1726 void TemplateURLService::ResetTemplateURLGUID(const TemplateURL* url, | 1761 void TemplateURLService::ResetTemplateURLGUID(const TemplateURL* url, |
| 1727 const std::string& guid) { | 1762 const std::string& guid) { |
| 1728 DCHECK(!guid.empty()); | 1763 DCHECK(!guid.empty()); |
| 1729 | 1764 |
| 1730 TemplateURL new_url(*url); | 1765 TemplateURLData data(url->data()); |
| 1731 new_url.set_sync_guid(guid); | 1766 data.sync_guid = guid; |
| 1767 TemplateURL new_url(data); |
| 1732 UpdateNoNotify(url, new_url); | 1768 UpdateNoNotify(url, new_url); |
| 1733 } | 1769 } |
| 1734 | 1770 |
| 1735 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { | 1771 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { |
| 1736 // Already unique. | 1772 // Already unique. |
| 1737 if (!GetTemplateURLForKeyword(turl.keyword())) | 1773 if (!GetTemplateURLForKeyword(turl.keyword())) |
| 1738 return turl.keyword(); | 1774 return turl.keyword(); |
| 1739 | 1775 |
| 1740 // First, try to return the generated keyword for the TemplateURL. | 1776 // First, try to return the generated keyword for the TemplateURL. |
| 1741 GURL gurl(turl.url()); | 1777 GURL gurl(turl.url()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1765 const TemplateURL* existing_turl = | 1801 const TemplateURL* existing_turl = |
| 1766 GetTemplateURLForKeyword(sync_turl->keyword()); | 1802 GetTemplateURLForKeyword(sync_turl->keyword()); |
| 1767 // If there is no conflict, or it's just conflicting with itself, return. | 1803 // If there is no conflict, or it's just conflicting with itself, return. |
| 1768 if (!existing_turl || existing_turl->sync_guid() == sync_turl->sync_guid()) | 1804 if (!existing_turl || existing_turl->sync_guid() == sync_turl->sync_guid()) |
| 1769 return false; | 1805 return false; |
| 1770 | 1806 |
| 1771 if (existing_turl->last_modified() > sync_turl->last_modified() || | 1807 if (existing_turl->last_modified() > sync_turl->last_modified() || |
| 1772 existing_turl->created_by_policy()) { | 1808 existing_turl->created_by_policy()) { |
| 1773 string16 new_keyword = UniquifyKeyword(*sync_turl); | 1809 string16 new_keyword = UniquifyKeyword(*sync_turl); |
| 1774 DCHECK(!GetTemplateURLForKeyword(new_keyword)); | 1810 DCHECK(!GetTemplateURLForKeyword(new_keyword)); |
| 1775 sync_turl->set_keyword(new_keyword); | 1811 sync_turl->data_.SetKeyword(new_keyword); |
| 1776 // If we update the cloud TURL, we need to push an update back to sync | 1812 // If we update the cloud TURL, we need to push an update back to sync |
| 1777 // informing it that something has changed. | 1813 // informing it that something has changed. |
| 1778 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 1814 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
| 1779 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 1815 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
| 1780 } else { | 1816 } else { |
| 1781 string16 new_keyword = UniquifyKeyword(*existing_turl); | 1817 string16 new_keyword = UniquifyKeyword(*existing_turl); |
| 1782 TemplateURL new_turl(*existing_turl); | 1818 TemplateURLData data(existing_turl->data()); |
| 1783 new_turl.set_keyword(new_keyword); | 1819 data.SetKeyword(new_keyword); |
| 1820 TemplateURL new_turl(data); |
| 1784 UpdateNoNotify(existing_turl, new_turl); | 1821 UpdateNoNotify(existing_turl, new_turl); |
| 1785 NotifyObservers(); | 1822 NotifyObservers(); |
| 1786 } | 1823 } |
| 1787 return true; | 1824 return true; |
| 1788 } | 1825 } |
| 1789 | 1826 |
| 1790 const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( | 1827 const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( |
| 1791 const TemplateURL& sync_turl) { | 1828 const TemplateURL& sync_turl) { |
| 1792 const TemplateURL* existing_turl = | 1829 const TemplateURL* existing_turl = |
| 1793 GetTemplateURLForKeyword(sync_turl.keyword()); | 1830 GetTemplateURLForKeyword(sync_turl.keyword()); |
| 1794 if (!existing_turl) | 1831 if (!existing_turl) |
| 1795 return NULL; | 1832 return NULL; |
| 1796 | 1833 |
| 1797 if (!existing_turl->url().empty() && | 1834 if (!existing_turl->url().empty() && |
| 1798 existing_turl->url() == sync_turl.url()) { | 1835 existing_turl->url() == sync_turl.url()) { |
| 1799 return existing_turl; | 1836 return existing_turl; |
| 1800 } | 1837 } |
| 1801 return NULL; | 1838 return NULL; |
| 1802 } | 1839 } |
| 1803 | 1840 |
| 1804 void TemplateURLService::MergeSyncAndLocalURLDuplicates( | 1841 void TemplateURLService::MergeSyncAndLocalURLDuplicates( |
| 1805 TemplateURL* sync_turl, | 1842 TemplateURL* sync_turl, |
| 1806 TemplateURL* local_turl, | 1843 TemplateURL* local_turl, |
| 1807 SyncChangeList* change_list) { | 1844 SyncChangeList* change_list) { |
| 1808 DCHECK(sync_turl); | 1845 DCHECK(sync_turl); |
| 1809 DCHECK(local_turl); | 1846 DCHECK(local_turl); |
| 1810 DCHECK(change_list); | 1847 DCHECK(change_list); |
| 1811 | |
| 1812 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl); | 1848 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl); |
| 1813 | 1849 if (sync_turl->last_modified() > local_turl->last_modified()) { |
| 1814 if (scoped_sync_turl->last_modified() > local_turl->last_modified()) { | |
| 1815 // Fully replace local_url with Sync's copy. Note that because use Add | 1850 // Fully replace local_url with Sync's copy. Note that because use Add |
| 1816 // rather than ResetTemplateURL, |sync_url| is added with a fresh | 1851 // rather than ResetTemplateURL, |sync_url| is added with a fresh |
| 1817 // TemplateURLID. We don't need to sync the new ID back to the server since | 1852 // TemplateURLID. We don't need to sync the new ID back to the server since |
| 1818 // it's only relevant locally. | 1853 // it's only relevant locally. |
| 1819 bool delete_default = (local_turl == GetDefaultSearchProvider()); | 1854 bool delete_default = (local_turl == GetDefaultSearchProvider()); |
| 1820 if (delete_default && is_default_search_managed_) { | 1855 DCHECK(!delete_default || !is_default_search_managed_); |
| 1821 NOTREACHED() << "Tried to delete managed default search provider"; | 1856 if (delete_default) |
| 1822 } else { | 1857 default_search_provider_ = NULL; |
| 1823 if (delete_default) | |
| 1824 default_search_provider_ = NULL; | |
| 1825 | 1858 |
| 1826 Remove(local_turl); | 1859 Remove(local_turl); |
| 1827 | 1860 |
| 1828 // Force the local ID to kInvalidTemplateURLID so we can add it. | 1861 // Force the local ID to kInvalidTemplateURLID so we can add it. |
| 1829 scoped_sync_turl->set_id(kInvalidTemplateURLID); | 1862 sync_turl->data_.id = kInvalidTemplateURLID; |
| 1830 TemplateURL* temp = scoped_sync_turl.release(); | 1863 Add(scoped_sync_turl.release()); |
| 1831 Add(temp); | 1864 if (delete_default) |
| 1832 if (delete_default) | 1865 SetDefaultSearchProvider(sync_turl); |
| 1833 SetDefaultSearchProvider(temp); | |
| 1834 } | |
| 1835 } else { | 1866 } else { |
| 1836 // Change the local TURL's GUID to the server's GUID and push an update to | 1867 // Change the local TURL's GUID to the server's GUID and push an update to |
| 1837 // Sync. This ensures that the rest of local_url's fields are sync'd up to | 1868 // Sync. This ensures that the rest of local_url's fields are sync'd up to |
| 1838 // the server, and the next time local_url is synced, it is recognized by | 1869 // the server, and the next time local_url is synced, it is recognized by |
| 1839 // having the same GUID. | 1870 // having the same GUID. |
| 1840 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); | 1871 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); |
| 1841 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 1872 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 1842 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 1873 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
| 1843 } | 1874 } |
| 1844 } | 1875 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1872 } | 1903 } |
| 1873 | 1904 |
| 1874 void TemplateURLService::PatchMissingSyncGUIDs( | 1905 void TemplateURLService::PatchMissingSyncGUIDs( |
| 1875 std::vector<TemplateURL*>* template_urls) { | 1906 std::vector<TemplateURL*>* template_urls) { |
| 1876 DCHECK(template_urls); | 1907 DCHECK(template_urls); |
| 1877 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); | 1908 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); |
| 1878 i != template_urls->end(); ++i) { | 1909 i != template_urls->end(); ++i) { |
| 1879 TemplateURL* template_url = *i; | 1910 TemplateURL* template_url = *i; |
| 1880 DCHECK(template_url); | 1911 DCHECK(template_url); |
| 1881 if (template_url->sync_guid().empty()) { | 1912 if (template_url->sync_guid().empty()) { |
| 1882 template_url->set_sync_guid(guid::GenerateGUID()); | 1913 template_url->data_.sync_guid = guid::GenerateGUID(); |
| 1883 if (service_.get()) | 1914 if (service_.get()) |
| 1884 service_->UpdateKeyword(*template_url); | 1915 service_->UpdateKeyword(*template_url); |
| 1885 } | 1916 } |
| 1886 } | 1917 } |
| 1887 } | 1918 } |
| 1888 | |
| 1889 // static | |
| 1890 void TemplateURLService::UpdateTemplateURLWithSyncData( | |
| 1891 TemplateURL* dst, | |
| 1892 const SyncData& sync_data) { | |
| 1893 sync_pb::SearchEngineSpecifics specifics = | |
| 1894 sync_data.GetSpecifics().search_engine(); | |
| 1895 dst->set_short_name(UTF8ToUTF16(specifics.short_name())); | |
| 1896 dst->set_keyword(UTF8ToUTF16(specifics.keyword())); | |
| 1897 dst->set_favicon_url(GURL(specifics.favicon_url())); | |
| 1898 dst->SetURL(specifics.url()); | |
| 1899 dst->set_safe_for_autoreplace(specifics.safe_for_autoreplace()); | |
| 1900 dst->set_originating_url(GURL(specifics.originating_url())); | |
| 1901 dst->set_date_created( | |
| 1902 base::Time::FromInternalValue(specifics.date_created())); | |
| 1903 std::vector<std::string> input_encodings; | |
| 1904 base::SplitString(specifics.input_encodings(), ';', &input_encodings); | |
| 1905 dst->set_input_encodings(input_encodings); | |
| 1906 dst->set_show_in_default_list(specifics.show_in_default_list()); | |
| 1907 dst->SetSuggestionsURL(specifics.suggestions_url()); | |
| 1908 dst->SetPrepopulateId(specifics.prepopulate_id()); | |
| 1909 dst->set_autogenerate_keyword(specifics.autogenerate_keyword()); | |
| 1910 dst->SetInstantURL(specifics.instant_url()); | |
| 1911 dst->set_last_modified( | |
| 1912 base::Time::FromInternalValue(specifics.last_modified())); | |
| 1913 dst->set_sync_guid(specifics.sync_guid()); | |
| 1914 } | |
| OLD | NEW |