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

Side by Side Diff: chrome/browser/search_engines/template_url_model.cc

Issue 7051003: Fix duplicate insertions of default search providers into the Web Data database. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/search_engines/template_url_model.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_model.h" 5 #include "chrome/browser/search_engines/template_url_model.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/environment.h" 8 #include "base/environment.h"
9 #include "base/i18n/case_conversion.h" 9 #include "base/i18n/case_conversion.h"
10 #include "base/stl_util-inl.h" 10 #include "base/stl_util-inl.h"
(...skipping 21 matching lines...) Expand all
32 #include "chrome/common/extensions/extension.h" 32 #include "chrome/common/extensions/extension.h"
33 #include "chrome/common/pref_names.h" 33 #include "chrome/common/pref_names.h"
34 #include "chrome/common/url_constants.h" 34 #include "chrome/common/url_constants.h"
35 #include "content/common/notification_service.h" 35 #include "content/common/notification_service.h"
36 #include "net/base/net_util.h" 36 #include "net/base/net_util.h"
37 #include "ui/base/l10n/l10n_util.h" 37 #include "ui/base/l10n/l10n_util.h"
38 38
39 using base::Time; 39 using base::Time;
40 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; 40 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
41 41
42 namespace {
43
42 // String in the URL that is replaced by the search term. 44 // String in the URL that is replaced by the search term.
43 static const char kSearchTermParameter[] = "{searchTerms}"; 45 const char kSearchTermParameter[] = "{searchTerms}";
44 46
45 // String in Initializer that is replaced with kSearchTermParameter. 47 // String in Initializer that is replaced with kSearchTermParameter.
46 static const char kTemplateParameter[] = "%s"; 48 const char kTemplateParameter[] = "%s";
47 49
48 // Term used when generating a search url. Use something obscure so that on 50 // Term used when generating a search url. Use something obscure so that on
49 // the rare case the term replaces the URL it's unlikely another keyword would 51 // the rare case the term replaces the URL it's unlikely another keyword would
50 // have the same url. 52 // have the same url.
51 static const char kReplacementTerm[] = "blah.blah.blah.blah.blah"; 53 const char kReplacementTerm[] = "blah.blah.blah.blah.blah";
52 54
55 bool TemplateURLsHaveSamePrefs(const TemplateURL* url1,
56 const TemplateURL* url2) {
57 if (url1 == url2)
58 return true;
59 return NULL != url1 &&
60 NULL != url2 &&
61 url1->short_name() == url2->short_name() &&
62 url1->keyword() == url2->keyword() &&
63 TemplateURLRef::SameUrlRefs(url1->url(), url2->url()) &&
64 TemplateURLRef::SameUrlRefs(url1->suggestions_url(),
65 url2->suggestions_url()) &&
66 url1->GetFaviconURL() == url2->GetFaviconURL() &&
67 url1->safe_for_autoreplace() == url2->safe_for_autoreplace() &&
68 url1->show_in_default_list() == url2->show_in_default_list() &&
69 url1->input_encodings() == url2->input_encodings();
70 }
53 71
54 // Removes from the vector any template URL that was created because of 72 } // namespace
55 // policy. These TemplateURLs are freed. 73
56 // Sets default_search_provider to NULL if it was one of them.
57 static void RemoveProvidersCreatedByPolicy(
58 std::vector<TemplateURL*>* template_urls,
59 const TemplateURL** default_search_provider) {
60 DCHECK(template_urls);
61 DCHECK(default_search_provider);
62 for (std::vector<TemplateURL*>::iterator i = template_urls->begin();
63 i != template_urls->end(); ) {
64 TemplateURL* template_url = *i;
65 if (template_url->created_by_policy()) {
66 if (*default_search_provider &&
67 (*default_search_provider)->id() == template_url->id())
68 *default_search_provider = NULL;
69 i = template_urls->erase(i);
70 delete template_url;
71 } else {
72 ++i;
73 }
74 }
75 }
76 74
77 class TemplateURLModel::LessWithPrefix { 75 class TemplateURLModel::LessWithPrefix {
78 public: 76 public:
79 // We want to find the set of keywords that begin with a prefix. The STL 77 // We want to find the set of keywords that begin with a prefix. The STL
80 // algorithms will return the set of elements that are "equal to" the 78 // algorithms will return the set of elements that are "equal to" the
81 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When 79 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When
82 // cmp() is the typical std::less<>, this results in lexicographic equality; 80 // cmp() is the typical std::less<>, this results in lexicographic equality;
83 // we need to extend this to mark a prefix as "not less than" a keyword it 81 // we need to extend this to mark a prefix as "not less than" a keyword it
84 // begins, which will cause the desired elements to be considered "equal to" 82 // begins, which will cause the desired elements to be considered "equal to"
85 // the prefix. Note: this is still a strict weak ordering, as required by 83 // the prefix. Note: this is still a strict weak ordering, as required by
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 int new_resource_keyword_version = 0; 476 int new_resource_keyword_version = 0;
479 GetSearchProvidersUsingKeywordResult(*result, 477 GetSearchProvidersUsingKeywordResult(*result,
480 service_.get(), 478 service_.get(),
481 GetPrefs(), 479 GetPrefs(),
482 &template_urls, 480 &template_urls,
483 &default_search_provider, 481 &default_search_provider,
484 &new_resource_keyword_version); 482 &new_resource_keyword_version);
485 483
486 bool database_specified_a_default = NULL != default_search_provider; 484 bool database_specified_a_default = NULL != default_search_provider;
487 485
488 // Remove entries that were created because of policy as they may have
489 // changed since the database was saved.
490 RemoveProvidersCreatedByPolicy(&template_urls, &default_search_provider);
491
492 // Check if default search provider is now managed. 486 // Check if default search provider is now managed.
493 scoped_ptr<TemplateURL> default_from_prefs; 487 scoped_ptr<TemplateURL> default_from_prefs;
494 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, 488 LoadDefaultSearchProviderFromPrefs(&default_from_prefs,
495 &is_default_search_managed_); 489 &is_default_search_managed_);
496 490
491 // Remove entries that were created because of policy as they may have
492 // changed since the database was saved.
493 RemoveProvidersCreatedByPolicy(&template_urls,
494 &default_search_provider,
495 default_from_prefs.get());
496
497 if (is_default_search_managed_) { 497 if (is_default_search_managed_) {
498 SetTemplateURLs(template_urls); 498 SetTemplateURLs(template_urls);
499 // Set the default. AddNoNotify will take ownership of default_from_prefs 499
500 // so it is safe to release. If it's null, there's no ownership to worry 500 if (TemplateURLsHaveSamePrefs(default_search_provider,
501 // about :-) 501 default_from_prefs.get())) {
502 TemplateURL* managed_default = default_from_prefs.release(); 502 // The value from the preferences was previously stored in the database.
503 if (managed_default) { 503 // Reuse it.
504 managed_default->set_created_by_policy(true); 504 } else {
505 managed_default->set_id(0); 505 // The value from the preferences takes over.
506 AddNoNotify(managed_default); 506 //
507 // AddNoNotify will take ownership of default_from_prefs so it is safe to
508 // release. If it's null, there's no ownership to worry about :-)
509 TemplateURL* managed_default = default_from_prefs.release();
510 if (managed_default) {
511 managed_default->set_created_by_policy(true);
512 managed_default->set_id(0);
513 AddNoNotify(managed_default);
514 default_search_provider = managed_default;
515 }
507 } 516 }
508 // Note that this saves the default search provider to prefs. 517 // Note that this saves the default search provider to prefs.
509 SetDefaultSearchProviderNoNotify(managed_default); 518 SetDefaultSearchProviderNoNotify(default_search_provider);
510 } else { 519 } else {
511 // If we had a managed default, replace it with the first provider of 520 // If we had a managed default, replace it with the first provider of
512 // the list. 521 // the list.
513 if (database_specified_a_default && 522 if (database_specified_a_default &&
514 NULL == default_search_provider && 523 NULL == default_search_provider &&
515 !template_urls.empty()) 524 !template_urls.empty())
516 default_search_provider = template_urls[0]; 525 default_search_provider = template_urls[0];
517 526
518 // If the default search provider existed previously, then just 527 // If the default search provider existed previously, then just
519 // set the member variable. Otherwise, we'll set it using the method 528 // set the member variable. Otherwise, we'll set it using the method
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 } 883 }
875 if (!prepopulate_id.empty() && !*is_managed) { 884 if (!prepopulate_id.empty() && !*is_managed) {
876 int value; 885 int value;
877 base::StringToInt(prepopulate_id, &value); 886 base::StringToInt(prepopulate_id, &value);
878 (*default_provider)->set_prepopulate_id(value); 887 (*default_provider)->set_prepopulate_id(value);
879 } 888 }
880 (*default_provider)->set_show_in_default_list(true); 889 (*default_provider)->set_show_in_default_list(true);
881 return true; 890 return true;
882 } 891 }
883 892
884 static bool TemplateURLsHaveSamePrefs(const TemplateURL* url1,
885 const TemplateURL* url2) {
886 if (url1 == url2)
887 return true;
888 return NULL != url1 &&
889 NULL != url2 &&
890 url1->short_name() == url2->short_name() &&
891 url1->keyword() == url2->keyword() &&
892 TemplateURLRef::SameUrlRefs(url1->url(), url2->url()) &&
893 TemplateURLRef::SameUrlRefs(url1->suggestions_url(),
894 url2->suggestions_url()) &&
895 url1->GetFaviconURL() == url2->GetFaviconURL() &&
896 url1->safe_for_autoreplace() == url2->safe_for_autoreplace() &&
897 url1->show_in_default_list() == url2->show_in_default_list() &&
898 url1->input_encodings() == url2->input_encodings();
899 }
900
901
902 bool TemplateURLModel::CanReplaceKeywordForHost( 893 bool TemplateURLModel::CanReplaceKeywordForHost(
903 const std::string& host, 894 const std::string& host,
904 const TemplateURL** to_replace) { 895 const TemplateURL** to_replace) {
905 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host); 896 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host);
906 if (urls) { 897 if (urls) {
907 for (TemplateURLSet::const_iterator i = urls->begin(); 898 for (TemplateURLSet::const_iterator i = urls->begin();
908 i != urls->end(); ++i) { 899 i != urls->end(); ++i) {
909 const TemplateURL* url = *i; 900 const TemplateURL* url = *i;
910 if (CanReplace(url)) { 901 if (CanReplace(url)) {
911 if (to_replace) 902 if (to_replace)
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
1275 delete template_url; 1266 delete template_url;
1276 } 1267 }
1277 1268
1278 void TemplateURLModel::NotifyObservers() { 1269 void TemplateURLModel::NotifyObservers() {
1279 if (!loaded_) 1270 if (!loaded_)
1280 return; 1271 return;
1281 1272
1282 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, 1273 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_,
1283 OnTemplateURLModelChanged()); 1274 OnTemplateURLModelChanged());
1284 } 1275 }
1276
1277 // |template_urls| are the TemplateURLs loaded from the database.
1278 // |default_search_provider| points to one of them, if it was set in the db.
1279 // |default_from_prefs| is the default search provider from the preferences.
1280 // Check |is_default_search_managed_| to determine if it was set by policy.
1281 //
1282 // This function removes from the vector and the database all the TemplateURLs
1283 // that were set by policy, unless it is the current default search provider
1284 // and matches what is set by a managed preference.
1285 void TemplateURLModel::RemoveProvidersCreatedByPolicy(
1286 std::vector<TemplateURL*>* template_urls,
1287 const TemplateURL** default_search_provider,
1288 const TemplateURL* default_from_prefs) {
1289 DCHECK(template_urls);
1290 DCHECK(default_search_provider);
1291 for (std::vector<TemplateURL*>::iterator i = template_urls->begin();
1292 i != template_urls->end(); ) {
1293 TemplateURL* template_url = *i;
1294 if (template_url->created_by_policy()) {
1295 if (template_url == *default_search_provider &&
1296 is_default_search_managed_ &&
1297 TemplateURLsHaveSamePrefs(template_url,
1298 default_from_prefs)) {
1299 // If the database specified a default search provider that was set
1300 // by policy, and the default search provider from the preferences
1301 // is also set by policy and they are the same, keep the entry in the
1302 // database and the |default_search_provider|.
1303 ++i;
1304 continue;
1305 }
1306
1307 // The database loaded a managed |default_search_provider|, but it has
1308 // been updated in the prefs. Remove it from the database, and update the
1309 // |default_search_provider| pointer here.
1310 if (*default_search_provider &&
1311 (*default_search_provider)->id() == template_url->id())
1312 *default_search_provider = NULL;
1313
1314 i = template_urls->erase(i);
1315 if (service_.get())
1316 service_->RemoveKeyword(*template_url);
1317 delete template_url;
1318 } else {
1319 ++i;
1320 }
1321 }
1322 }
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/template_url_model.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698