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
3 // found in the LICENSE file.
5 #include "chrome/browser/search_engines/template_url_service.h"
10 #include "base/auto_reset.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/environment.h"
14 #include "base/guid.h"
15 #include "base/i18n/case_conversion.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/metrics/histogram.h"
18 #include "base/prefs/pref_service.h"
19 #include "base/stl_util.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_split.h"
22 #include "base/strings/string_util.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/time/time.h"
25 #include "chrome/browser/chrome_notification_types.h"
26 #include "chrome/browser/extensions/extension_service.h"
27 #include "chrome/browser/google/google_url_tracker.h"
28 #include "chrome/browser/history/history_notifications.h"
29 #include "chrome/browser/history/history_service.h"
30 #include "chrome/browser/history/history_service_factory.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/rlz/rlz.h"
33 #include "chrome/browser/search_engines/search_host_to_urls_map.h"
34 #include "chrome/browser/search_engines/search_terms_data.h"
35 #include "chrome/browser/search_engines/template_url.h"
36 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
37 #include "chrome/browser/search_engines/template_url_service_observer.h"
38 #include "chrome/browser/search_engines/util.h"
39 #include "chrome/browser/webdata/web_data_service.h"
40 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/env_vars.h"
42 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h"
43 #include "chrome/common/net/url_fixer_upper.h"
44 #include "chrome/common/pref_names.h"
45 #include "chrome/common/url_constants.h"
46 #include "content/public/browser/notification_service.h"
47 #include "extensions/common/constants.h"
48 #include "net/base/net_util.h"
49 #include "sync/api/sync_change.h"
50 #include "sync/api/sync_error_factory.h"
51 #include "sync/protocol/search_engine_specifics.pb.h"
52 #include "sync/protocol/sync.pb.h"
53 #include "ui/base/l10n/l10n_util.h"
55 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet
;
56 typedef TemplateURLService::SyncDataMap SyncDataMap
;
60 bool TemplateURLsHaveSamePrefs(const TemplateURL
* url1
,
61 const TemplateURL
* url2
) {
64 return (url1
!= NULL
) && (url2
!= NULL
) &&
65 (url1
->short_name() == url2
->short_name()) &&
66 url1
->HasSameKeywordAs(*url2
) &&
67 (url1
->url() == url2
->url()) &&
68 (url1
->suggestions_url() == url2
->suggestions_url()) &&
69 (url1
->instant_url() == url2
->instant_url()) &&
70 (url1
->image_url() == url2
->image_url()) &&
71 (url1
->new_tab_url() == url2
->new_tab_url()) &&
72 (url1
->search_url_post_params() == url2
->search_url_post_params()) &&
73 (url1
->suggestions_url_post_params() ==
74 url2
->suggestions_url_post_params()) &&
75 (url1
->instant_url_post_params() == url2
->instant_url_post_params()) &&
76 (url1
->image_url_post_params() == url2
->image_url_post_params()) &&
77 (url1
->favicon_url() == url2
->favicon_url()) &&
78 (url1
->safe_for_autoreplace() == url2
->safe_for_autoreplace()) &&
79 (url1
->show_in_default_list() == url2
->show_in_default_list()) &&
80 (url1
->input_encodings() == url2
->input_encodings()) &&
81 (url1
->alternate_urls() == url2
->alternate_urls()) &&
82 (url1
->search_terms_replacement_key() ==
83 url2
->search_terms_replacement_key());
86 const char kFirstPotentialEngineHistogramName
[] =
87 "Search.FirstPotentialEngineCalled";
89 // Values for an enumerated histogram used to track whenever
90 // FirstPotentialDefaultEngine is called, and from where.
91 enum FirstPotentialEngineCaller
{
92 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP
,
93 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_PROCESSING_SYNC_CHANGES
,
94 FIRST_POTENTIAL_CALLSITE_ON_LOAD
,
95 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_SYNCING
,
96 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_NOT_SYNCING
,
97 FIRST_POTENTIAL_CALLSITE_MAX
,
100 const char kDeleteSyncedEngineHistogramName
[] =
101 "Search.DeleteSyncedSearchEngine";
103 // Values for an enumerated histogram used to track whenever an ACTION_DELETE is
104 // sent to the server for search engines.
105 enum DeleteSyncedSearchEngineEvent
{
106 DELETE_ENGINE_USER_ACTION
,
107 DELETE_ENGINE_PRE_SYNC
,
108 DELETE_ENGINE_EMPTY_FIELD
,
112 TemplateURL
* FirstPotentialDefaultEngine(
113 const TemplateURLService::TemplateURLVector
& template_urls
) {
114 for (TemplateURLService::TemplateURLVector::const_iterator
i(
115 template_urls
.begin()); i
!= template_urls
.end(); ++i
) {
116 if ((*i
)->ShowInDefaultList() &&
117 ((*i
)->GetType() == TemplateURL::NORMAL
))
123 // Returns true iff the change in |change_list| at index |i| should not be sent
124 // up to the server based on its GUIDs presence in |sync_data| or when compared
125 // to changes after it in |change_list|.
127 // 1) It is an ACTION_UPDATE or ACTION_DELETE and the sync_guid associated
128 // with it is NOT found in |sync_data|. We can only update and remove
129 // entries that were originally from the Sync server.
130 // 2) It is an ACTION_ADD and the sync_guid associated with it is found in
131 // |sync_data|. We cannot re-add entries that Sync already knew about.
132 // 3) There is an update after an update for the same GUID. We prune earlier
133 // ones just to save bandwidth (Sync would normally coalesce them).
134 bool ShouldRemoveSyncChange(size_t index
,
135 syncer::SyncChangeList
* change_list
,
136 const SyncDataMap
* sync_data
) {
137 DCHECK(index
< change_list
->size());
138 const syncer::SyncChange
& change_i
= (*change_list
)[index
];
139 const std::string guid
= change_i
.sync_data().GetSpecifics()
140 .search_engine().sync_guid();
141 syncer::SyncChange::SyncChangeType type
= change_i
.change_type();
142 if ((type
== syncer::SyncChange::ACTION_UPDATE
||
143 type
== syncer::SyncChange::ACTION_DELETE
) &&
144 sync_data
->find(guid
) == sync_data
->end())
146 if (type
== syncer::SyncChange::ACTION_ADD
&&
147 sync_data
->find(guid
) != sync_data
->end())
149 if (type
== syncer::SyncChange::ACTION_UPDATE
) {
150 for (size_t j
= index
+ 1; j
< change_list
->size(); j
++) {
151 const syncer::SyncChange
& change_j
= (*change_list
)[j
];
152 if ((syncer::SyncChange::ACTION_UPDATE
== change_j
.change_type()) &&
153 (change_j
.sync_data().GetSpecifics().search_engine().sync_guid() ==
161 // Remove SyncChanges that should not be sent to the server from |change_list|.
162 // This is done to eliminate incorrect SyncChanges added by the merge and
163 // conflict resolution logic when it is unsure of whether or not an entry is new
164 // from Sync or originally from the local model. This also removes changes that
165 // would be otherwise be coalesced by Sync in order to save bandwidth.
166 void PruneSyncChanges(const SyncDataMap
* sync_data
,
167 syncer::SyncChangeList
* change_list
) {
168 for (size_t i
= 0; i
< change_list
->size(); ) {
169 if (ShouldRemoveSyncChange(i
, change_list
, sync_data
))
170 change_list
->erase(change_list
->begin() + i
);
176 // Helper class that reports a specific string as the Google base URL. We use
177 // this so that code can calculate a TemplateURL's previous search URL after a
178 // change to the global base URL.
179 class OldBaseURLSearchTermsData
: public UIThreadSearchTermsData
{
181 OldBaseURLSearchTermsData(Profile
* profile
, const std::string
& old_base_url
);
182 virtual ~OldBaseURLSearchTermsData();
184 virtual std::string
GoogleBaseURLValue() const OVERRIDE
;
187 std::string old_base_url
;
190 OldBaseURLSearchTermsData::OldBaseURLSearchTermsData(
192 const std::string
& old_base_url
)
193 : UIThreadSearchTermsData(profile
),
194 old_base_url(old_base_url
) {
197 OldBaseURLSearchTermsData::~OldBaseURLSearchTermsData() {
200 std::string
OldBaseURLSearchTermsData::GoogleBaseURLValue() const {
204 // Returns true if |turl|'s GUID is not found inside |sync_data|. This is to be
205 // used in MergeDataAndStartSyncing to differentiate between TemplateURLs from
206 // Sync and TemplateURLs that were initially local, assuming |sync_data| is the
207 // |initial_sync_data| parameter.
208 bool IsFromSync(const TemplateURL
* turl
, const SyncDataMap
& sync_data
) {
209 return !!sync_data
.count(turl
->sync_guid());
212 // Log the number of instances of a keyword that exist, with zero or more
213 // underscores, which could occur as the result of conflict resolution.
214 void LogDuplicatesHistogram(
215 const TemplateURLService::TemplateURLVector
& template_urls
) {
216 std::map
<std::string
, int> duplicates
;
217 for (TemplateURLService::TemplateURLVector::const_iterator it
=
218 template_urls
.begin(); it
!= template_urls
.end(); ++it
) {
219 std::string keyword
= base::UTF16ToASCII((*it
)->keyword());
220 base::TrimString(keyword
, "_", &keyword
);
221 duplicates
[keyword
]++;
224 // Count the keywords with duplicates.
226 for (std::map
<std::string
, int>::const_iterator it
= duplicates
.begin();
227 it
!= duplicates
.end(); ++it
) {
232 UMA_HISTOGRAM_COUNTS_100("Search.SearchEngineDuplicateCounts", num_dupes
);
238 // TemplateURLService::ExtensionKeyword ---------------------------------------
240 TemplateURLService::ExtensionKeyword::ExtensionKeyword(
241 const std::string
& id
,
242 const std::string
& name
,
243 const std::string
& keyword
)
245 extension_name(name
),
246 extension_keyword(keyword
) {
249 TemplateURLService::ExtensionKeyword::~ExtensionKeyword() {}
252 // TemplateURLService::LessWithPrefix -----------------------------------------
254 class TemplateURLService::LessWithPrefix
{
256 // We want to find the set of keywords that begin with a prefix. The STL
257 // algorithms will return the set of elements that are "equal to" the
258 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When
259 // cmp() is the typical std::less<>, this results in lexicographic equality;
260 // we need to extend this to mark a prefix as "not less than" a keyword it
261 // begins, which will cause the desired elements to be considered "equal to"
262 // the prefix. Note: this is still a strict weak ordering, as required by
263 // equal_range() (though I will not prove that here).
265 // Unfortunately the calling convention is not "prefix and element" but
266 // rather "two elements", so we pass the prefix as a fake "element" which has
267 // a NULL KeywordDataElement pointer.
268 bool operator()(const KeywordToTemplateMap::value_type
& elem1
,
269 const KeywordToTemplateMap::value_type
& elem2
) const {
270 return (elem1
.second
== NULL
) ?
271 (elem2
.first
.compare(0, elem1
.first
.length(), elem1
.first
) > 0) :
272 (elem1
.first
< elem2
.first
);
277 // TemplateURLService ---------------------------------------------------------
279 TemplateURLService::TemplateURLService(Profile
* profile
)
280 : provider_map_(new SearchHostToURLsMap
),
285 default_search_provider_(NULL
),
286 is_default_search_managed_(false),
287 next_id_(kInvalidTemplateURLID
+ 1),
288 time_provider_(&base::Time::Now
),
289 models_associated_(false),
290 processing_syncer_changes_(false),
291 pending_synced_default_search_(false),
292 dsp_change_origin_(DSP_CHANGE_OTHER
),
293 default_search_manager_(new DefaultSearchManager(GetPrefs())) {
298 TemplateURLService::TemplateURLService(const Initializer
* initializers
,
300 : provider_map_(new SearchHostToURLsMap
),
306 default_search_provider_(NULL
),
307 is_default_search_managed_(false),
308 next_id_(kInvalidTemplateURLID
+ 1),
309 time_provider_(&base::Time::Now
),
310 models_associated_(false),
311 processing_syncer_changes_(false),
312 pending_synced_default_search_(false),
313 dsp_change_origin_(DSP_CHANGE_OTHER
) {
314 Init(initializers
, count
);
317 TemplateURLService::~TemplateURLService() {
318 // |service_| should be deleted during Shutdown().
320 STLDeleteElements(&template_urls_
);
324 base::string16
TemplateURLService::GenerateKeyword(const GURL
& url
) {
325 DCHECK(url
.is_valid());
326 // Strip "www." off the front of the keyword; otherwise the keyword won't work
327 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
328 // Special case: if the host was exactly "www." (not sure this can happen but
329 // perhaps with some weird intranet and custom DNS server?), ensure we at
330 // least don't return the empty string.
331 base::string16
keyword(net::StripWWWFromHost(url
));
332 return keyword
.empty() ? base::ASCIIToUTF16("www") : keyword
;
336 base::string16
TemplateURLService::CleanUserInputKeyword(
337 const base::string16
& keyword
) {
338 // Remove the scheme.
339 base::string16
result(base::i18n::ToLower(keyword
));
340 base::TrimWhitespace(result
, base::TRIM_ALL
, &result
);
341 url_parse::Component scheme_component
;
342 if (url_parse::ExtractScheme(base::UTF16ToUTF8(keyword
).c_str(),
343 static_cast<int>(keyword
.length()),
344 &scheme_component
)) {
345 // If the scheme isn't "http" or "https", bail. The user isn't trying to
346 // type a web address, but rather an FTP, file:, or other scheme URL, or a
347 // search query with some sort of initial operator (e.g. "site:").
348 if (result
.compare(0, scheme_component
.end(),
349 base::ASCIIToUTF16(content::kHttpScheme
)) &&
350 result
.compare(0, scheme_component
.end(),
351 base::ASCIIToUTF16(content::kHttpsScheme
)))
352 return base::string16();
354 // Include trailing ':'.
355 result
.erase(0, scheme_component
.end() + 1);
356 // Many schemes usually have "//" after them, so strip it too.
357 const base::string16
after_scheme(base::ASCIIToUTF16("//"));
358 if (result
.compare(0, after_scheme
.length(), after_scheme
) == 0)
359 result
.erase(0, after_scheme
.length());
362 // Remove leading "www.".
363 result
= net::StripWWW(result
);
365 // Remove trailing "/".
366 return (result
.length() > 0 && result
[result
.length() - 1] == '/') ?
367 result
.substr(0, result
.length() - 1) : result
;
371 GURL
TemplateURLService::GenerateSearchURL(TemplateURL
* t_url
) {
373 UIThreadSearchTermsData
search_terms_data(t_url
->profile());
374 return GenerateSearchURLUsingTermsData(t_url
, search_terms_data
);
378 GURL
TemplateURLService::GenerateSearchURLUsingTermsData(
379 const TemplateURL
* t_url
,
380 const SearchTermsData
& search_terms_data
) {
383 const TemplateURLRef
& search_ref
= t_url
->url_ref();
384 if (!search_ref
.IsValidUsingTermsData(search_terms_data
))
387 if (!search_ref
.SupportsReplacementUsingTermsData(search_terms_data
))
388 return GURL(t_url
->url());
390 // Use something obscure for the search terms argument so that in the rare
391 // case the term replaces the URL it's unlikely another keyword would have the
393 // TODO(jnd): Add additional parameters to get post data when the search URL
394 // has post parameters.
395 return GURL(search_ref
.ReplaceSearchTermsUsingTermsData(
396 TemplateURLRef::SearchTermsArgs(
397 base::ASCIIToUTF16("blah.blah.blah.blah.blah")),
398 search_terms_data
, NULL
));
401 bool TemplateURLService::CanReplaceKeyword(
402 const base::string16
& keyword
,
404 TemplateURL
** template_url_to_replace
) {
405 DCHECK(!keyword
.empty()); // This should only be called for non-empty
406 // keywords. If we need to support empty kewords
407 // the code needs to change slightly.
408 TemplateURL
* existing_url
= GetTemplateURLForKeyword(keyword
);
409 if (template_url_to_replace
)
410 *template_url_to_replace
= existing_url
;
412 // We already have a TemplateURL for this keyword. Only allow it to be
413 // replaced if the TemplateURL can be replaced.
414 return CanReplace(existing_url
);
417 // We don't have a TemplateURL with keyword. Only allow a new one if there
418 // isn't a TemplateURL for the specified host, or there is one but it can
419 // be replaced. We do this to ensure that if the user assigns a different
420 // keyword to a generated TemplateURL, we won't regenerate another keyword for
422 return !url
.is_valid() || url
.host().empty() ||
423 CanReplaceKeywordForHost(url
.host(), template_url_to_replace
);
426 void TemplateURLService::FindMatchingKeywords(
427 const base::string16
& prefix
,
428 bool support_replacement_only
,
429 TemplateURLVector
* matches
) const {
430 // Sanity check args.
433 DCHECK(matches
!= NULL
);
434 DCHECK(matches
->empty()); // The code for exact matches assumes this.
436 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
437 TemplateURL
* const kNullTemplateURL
= NULL
;
439 // Find matching keyword range. Searches the element map for keywords
440 // beginning with |prefix| and stores the endpoints of the resulting set in
442 const std::pair
<KeywordToTemplateMap::const_iterator
,
443 KeywordToTemplateMap::const_iterator
> match_range(
445 keyword_to_template_map_
.begin(), keyword_to_template_map_
.end(),
446 KeywordToTemplateMap::value_type(prefix
, kNullTemplateURL
),
449 // Return vector of matching keywords.
450 for (KeywordToTemplateMap::const_iterator
i(match_range
.first
);
451 i
!= match_range
.second
; ++i
) {
452 if (!support_replacement_only
|| i
->second
->url_ref().SupportsReplacement())
453 matches
->push_back(i
->second
);
457 TemplateURL
* TemplateURLService::GetTemplateURLForKeyword(
458 const base::string16
& keyword
) {
459 KeywordToTemplateMap::const_iterator
elem(
460 keyword_to_template_map_
.find(keyword
));
461 if (elem
!= keyword_to_template_map_
.end())
463 return ((!loaded_
|| load_failed_
) &&
464 initial_default_search_provider_
.get() &&
465 (initial_default_search_provider_
->keyword() == keyword
)) ?
466 initial_default_search_provider_
.get() : NULL
;
469 TemplateURL
* TemplateURLService::GetTemplateURLForGUID(
470 const std::string
& sync_guid
) {
471 GUIDToTemplateMap::const_iterator
elem(guid_to_template_map_
.find(sync_guid
));
472 if (elem
!= guid_to_template_map_
.end())
474 return ((!loaded_
|| load_failed_
) &&
475 initial_default_search_provider_
.get() &&
476 (initial_default_search_provider_
->sync_guid() == sync_guid
)) ?
477 initial_default_search_provider_
.get() : NULL
;
480 TemplateURL
* TemplateURLService::GetTemplateURLForHost(
481 const std::string
& host
) {
483 TemplateURL
* t_url
= provider_map_
->GetTemplateURLForHost(host
);
487 return ((!loaded_
|| load_failed_
) &&
488 initial_default_search_provider_
.get() &&
489 (GenerateSearchURL(initial_default_search_provider_
.get()).host() ==
490 host
)) ? initial_default_search_provider_
.get() : NULL
;
493 void TemplateURLService::Add(TemplateURL
* template_url
) {
494 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
495 if (AddNoNotify(template_url
, true))
499 void TemplateURLService::AddAndSetProfile(TemplateURL
* template_url
,
501 template_url
->profile_
= profile
;
505 void TemplateURLService::AddWithOverrides(TemplateURL
* template_url
,
506 const base::string16
& short_name
,
507 const base::string16
& keyword
,
508 const std::string
& url
) {
509 DCHECK(!keyword
.empty());
510 DCHECK(!url
.empty());
511 template_url
->data_
.short_name
= short_name
;
512 template_url
->data_
.SetKeyword(keyword
);
513 template_url
->SetURL(url
);
517 void TemplateURLService::AddExtensionControlledTURL(
518 TemplateURL
* template_url
,
519 scoped_ptr
<AssociatedExtensionInfo
> info
) {
521 DCHECK(template_url
);
522 DCHECK_EQ(kInvalidTemplateURLID
, template_url
->id());
524 DCHECK_EQ(info
->wants_to_be_default_engine
,
525 template_url
->show_in_default_list());
526 template_url
->extension_info_
.swap(info
);
527 DCHECK(!FindTemplateURLForExtension(
528 template_url
->GetExtensionId(),
529 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
));
531 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
532 if (AddNoNotify(template_url
, true)) {
533 // Note that we can't call CanMakeDefault() here, since it would return
534 // false when another extension is already controlling the default search
535 // engine, and we want to allow new extensions to take over.
536 if (template_url
->extension_info_
->wants_to_be_default_engine
&&
537 !is_default_search_managed()) {
538 TemplateURL
* default_candidate
= FindExtensionDefaultSearchEngine();
539 if (default_candidate
== template_url
) {
540 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(
541 &dsp_change_origin_
, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION
);
542 SetDefaultSearchProviderNoNotify(template_url
);
549 void TemplateURLService::Remove(TemplateURL
* template_url
) {
550 RemoveNoNotify(template_url
);
554 void TemplateURLService::RemoveExtensionControlledTURL(
555 const std::string
& extension_id
) {
557 TemplateURL
* url
= FindTemplateURLForExtension(
558 extension_id
, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
);
561 bool restore_dse
= (url
== GetDefaultSearchProvider());
563 DCHECK(!is_default_search_managed());
564 default_search_provider_
= NULL
;
566 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
569 SetDefaultSearchProviderAfterRemovingDefaultExtension();
573 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after
) {
574 RemoveAutoGeneratedBetween(created_after
, base::Time());
577 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after
,
578 base::Time created_before
) {
579 RemoveAutoGeneratedForOriginBetween(GURL(), created_after
, created_before
);
582 void TemplateURLService::RemoveAutoGeneratedForOriginBetween(
584 base::Time created_after
,
585 base::Time created_before
) {
586 GURL
o(origin
.GetOrigin());
587 bool should_notify
= false;
588 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
589 for (size_t i
= 0; i
< template_urls_
.size();) {
590 if (template_urls_
[i
]->date_created() >= created_after
&&
591 (created_before
.is_null() ||
592 template_urls_
[i
]->date_created() < created_before
) &&
593 CanReplace(template_urls_
[i
]) &&
595 GenerateSearchURL(template_urls_
[i
]).GetOrigin() == o
)) {
596 RemoveNoNotify(template_urls_
[i
]);
597 should_notify
= true;
607 void TemplateURLService::RegisterOmniboxKeyword(
608 const std::string
& extension_id
,
609 const std::string
& extension_name
,
610 const std::string
& keyword
) {
613 if (!FindTemplateURLForExtension(extension_id
,
614 TemplateURL::OMNIBOX_API_EXTENSION
)) {
615 ExtensionKeyword
extension_url(extension_id
, extension_name
, keyword
);
616 Add(CreateTemplateURLForExtension(extension_url
));
620 void TemplateURLService::UnregisterOmniboxKeyword(
621 const std::string
& extension_id
) {
623 TemplateURL
* url
= FindTemplateURLForExtension(
624 extension_id
, TemplateURL::OMNIBOX_API_EXTENSION
);
629 TemplateURLService::TemplateURLVector
TemplateURLService::GetTemplateURLs() {
630 return template_urls_
;
633 void TemplateURLService::IncrementUsageCount(TemplateURL
* url
) {
635 // Extension-controlled search engines are not persisted.
636 if (url
->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
)
638 if (std::find(template_urls_
.begin(), template_urls_
.end(), url
) ==
639 template_urls_
.end())
641 ++url
->data_
.usage_count
;
644 service_
.get()->UpdateKeyword(url
->data());
647 void TemplateURLService::ResetTemplateURL(TemplateURL
* url
,
648 const base::string16
& title
,
649 const base::string16
& keyword
,
650 const std::string
& search_url
) {
653 DCHECK(!keyword
.empty());
654 DCHECK(!search_url
.empty());
655 TemplateURLData
data(url
->data());
656 data
.short_name
= title
;
657 data
.SetKeyword(keyword
);
658 if (search_url
!= data
.url()) {
659 data
.SetURL(search_url
);
660 // The urls have changed, reset the favicon url.
661 data
.favicon_url
= GURL();
663 data
.safe_for_autoreplace
= false;
664 data
.last_modified
= time_provider_();
665 TemplateURL
new_url(url
->profile(), data
);
666 UIThreadSearchTermsData
search_terms_data(url
->profile());
667 if (UpdateNoNotify(url
, new_url
, search_terms_data
))
671 bool TemplateURLService::CanMakeDefault(const TemplateURL
* url
) {
672 return !is_default_search_managed() &&
673 !IsExtensionControlledDefaultSearch() &&
674 (url
!= GetDefaultSearchProvider()) &&
675 url
->url_ref().SupportsReplacement() &&
676 (url
->GetType() == TemplateURL::NORMAL
);
679 void TemplateURLService::SetUserSelectedDefaultSearchProvider(
681 SetDefaultSearchProvider(url
);
682 if (default_search_manager_
) {
684 default_search_manager_
->SetUserSelectedDefaultSearchEngine(url
->data());
686 default_search_manager_
->ClearUserSelectedDefaultSearchEngine();
690 TemplateURL
* TemplateURLService::GetDefaultSearchProvider() {
691 if (loaded_
&& !load_failed_
)
692 return default_search_provider_
;
693 // We're not loaded, rely on the default search provider stored in prefs.
694 return initial_default_search_provider_
.get();
697 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider(
699 TemplateURL
* default_provider
= GetDefaultSearchProvider();
700 return default_provider
&& default_provider
->IsSearchURL(url
);
703 bool TemplateURLService::IsExtensionControlledDefaultSearch() {
704 const TemplateURL
* default_provider
= GetDefaultSearchProvider();
705 return default_provider
&& (default_provider
->GetType() ==
706 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
);
709 TemplateURL
* TemplateURLService::FindNewDefaultSearchProvider() {
710 // See if the prepopulated default still exists.
711 scoped_ptr
<TemplateURL
> prepopulated_default(
712 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_
));
713 for (TemplateURLVector::iterator i
= template_urls_
.begin();
714 i
!= template_urls_
.end(); ++i
) {
715 if ((*i
)->prepopulate_id() == prepopulated_default
->prepopulate_id())
718 // If not, use the first non-extension keyword of the templates that supports
719 // search term replacement.
720 if (processing_syncer_changes_
) {
721 UMA_HISTOGRAM_ENUMERATION(
722 kFirstPotentialEngineHistogramName
,
723 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_PROCESSING_SYNC_CHANGES
,
724 FIRST_POTENTIAL_CALLSITE_MAX
);
726 if (sync_processor_
.get()) {
727 // We're not currently in a sync cycle, but we're syncing.
728 UMA_HISTOGRAM_ENUMERATION(kFirstPotentialEngineHistogramName
,
729 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_SYNCING
,
730 FIRST_POTENTIAL_CALLSITE_MAX
);
732 // We're not syncing at all.
733 UMA_HISTOGRAM_ENUMERATION(
734 kFirstPotentialEngineHistogramName
,
735 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_NOT_SYNCING
,
736 FIRST_POTENTIAL_CALLSITE_MAX
);
739 return FirstPotentialDefaultEngine(template_urls_
);
742 void TemplateURLService::RepairPrepopulatedSearchEngines() {
743 // Can't clean DB if it hasn't been loaded.
746 size_t default_search_provider_index
= 0;
747 ScopedVector
<TemplateURL
> prepopulated_urls
=
748 TemplateURLPrepopulateData::GetPrepopulatedEngines(
749 profile_
, &default_search_provider_index
);
750 DCHECK(!prepopulated_urls
.empty());
751 int default_search_engine_id
=
752 prepopulated_urls
[default_search_provider_index
]->prepopulate_id();
753 TemplateURL
* current_dse
= default_search_provider_
;
754 ActionsFromPrepopulateData
actions(CreateActionsFromCurrentPrepopulateData(
755 &prepopulated_urls
, template_urls_
, current_dse
));
757 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
760 for (std::vector
<TemplateURL
*>::iterator i
= actions
.removed_engines
.begin();
761 i
< actions
.removed_engines
.end(); ++i
)
765 for (EditedEngines::iterator
i(actions
.edited_engines
.begin());
766 i
< actions
.edited_engines
.end(); ++i
) {
767 UIThreadSearchTermsData
search_terms_data(profile());
768 TemplateURL
new_values(profile(), i
->second
);
769 UpdateNoNotify(i
->first
, new_values
, search_terms_data
);
773 for (std::vector
<TemplateURL
*>::iterator i
= actions
.added_engines
.begin();
774 i
< actions
.added_engines
.end(); ++i
)
775 AddNoNotify(*i
, true);
778 TemplateURL
* new_dse
= FindURLByPrepopulateID(template_urls_
,
779 default_search_engine_id
);
781 if (CanMakeDefault(new_dse
)) {
782 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(
783 &dsp_change_origin_
, DSP_CHANGE_PROFILE_RESET
);
784 SetDefaultSearchProviderNoNotify(new_dse
);
789 void TemplateURLService::AddObserver(TemplateURLServiceObserver
* observer
) {
790 model_observers_
.AddObserver(observer
);
793 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver
* observer
) {
794 model_observers_
.RemoveObserver(observer
);
797 void TemplateURLService::Load() {
798 if (loaded_
|| load_handle_
)
801 if (!service_
.get()) {
802 service_
= WebDataService::FromBrowserContext(profile_
);
805 if (service_
.get()) {
806 load_handle_
= service_
->GetKeywords(this);
808 ChangeToLoadedState();
809 on_loaded_callbacks_
.Notify();
813 scoped_ptr
<TemplateURLService::Subscription
>
814 TemplateURLService::RegisterOnLoadedCallback(
815 const base::Closure
& callback
) {
817 scoped_ptr
<TemplateURLService::Subscription
>() :
818 on_loaded_callbacks_
.Add(callback
);
821 void TemplateURLService::OnWebDataServiceRequestDone(
822 WebDataService::Handle h
,
823 const WDTypedResult
* result
) {
824 // Reset the load_handle so that we don't try and cancel the load in
829 // Results are null if the database went away or (most likely) wasn't
832 ChangeToLoadedState();
833 on_loaded_callbacks_
.Notify();
837 // initial_default_search_provider_ is only needed before we've finished
838 // loading. Now that we've loaded we can nuke it.
839 initial_default_search_provider_
.reset();
841 TemplateURLVector template_urls
;
842 TemplateURL
* default_search_provider
= NULL
;
843 int new_resource_keyword_version
= 0;
844 GetSearchProvidersUsingKeywordResult(*result
, service_
.get(), profile_
,
845 &template_urls
, &default_search_provider
, &new_resource_keyword_version
,
848 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
850 AddTemplateURLsAndSetupDefaultEngine(&template_urls
, default_search_provider
);
852 // This initializes provider_map_ which should be done before
853 // calling UpdateKeywordSearchTermsForURL.
854 ChangeToLoadedState();
856 // Index any visits that occurred before we finished loading.
857 for (size_t i
= 0; i
< visits_to_add_
.size(); ++i
)
858 UpdateKeywordSearchTermsForURL(visits_to_add_
[i
]);
859 visits_to_add_
.clear();
861 if (new_resource_keyword_version
)
862 service_
->SetBuiltinKeywordVersion(new_resource_keyword_version
);
864 EnsureDefaultSearchProviderExists();
867 on_loaded_callbacks_
.Notify();
870 base::string16
TemplateURLService::GetKeywordShortName(
871 const base::string16
& keyword
,
872 bool* is_omnibox_api_extension_keyword
) {
873 const TemplateURL
* template_url
= GetTemplateURLForKeyword(keyword
);
875 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService
876 // to track changes to the model, this should become a DCHECK.
878 *is_omnibox_api_extension_keyword
=
879 template_url
->GetType() == TemplateURL::OMNIBOX_API_EXTENSION
;
880 return template_url
->AdjustedShortNameForLocaleDirection();
882 *is_omnibox_api_extension_keyword
= false;
883 return base::string16();
886 void TemplateURLService::Observe(int type
,
887 const content::NotificationSource
& source
,
888 const content::NotificationDetails
& details
) {
889 if (type
== chrome::NOTIFICATION_HISTORY_URL_VISITED
) {
890 content::Details
<history::URLVisitedDetails
> visit_details(details
);
892 visits_to_add_
.push_back(*visit_details
.ptr());
894 UpdateKeywordSearchTermsForURL(*visit_details
.ptr());
895 } else if (type
== chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED
) {
896 // Policy has been updated, so the default search prefs may be different.
897 // Reload the default search provider from them.
898 // TODO(pkasting): Rather than communicating via prefs, we should eventually
899 // observe policy changes directly.
900 UpdateDefaultSearch();
902 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED
, type
);
904 GoogleBaseURLChanged(
905 content::Details
<GoogleURLTracker::UpdatedDetails
>(details
)->first
);
910 void TemplateURLService::Shutdown() {
911 // This check has to be done at Shutdown() instead of in the dtor to ensure
912 // that no clients of WebDataService are holding ptrs to it after the first
913 // phase of the KeyedService Shutdown() process.
915 DCHECK(service_
.get());
916 service_
->CancelRequest(load_handle_
);
921 void TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged() {
922 // Listen for changes to the default search from Sync.
923 PrefService
* prefs
= GetPrefs();
924 TemplateURL
* new_default_search
= GetTemplateURLForGUID(
925 prefs
->GetString(prefs::kSyncedDefaultSearchProviderGUID
));
926 if (new_default_search
&& !is_default_search_managed_
) {
927 if (new_default_search
!= GetDefaultSearchProvider()) {
928 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(
929 &dsp_change_origin_
, DSP_CHANGE_SYNC_PREF
);
930 SetUserSelectedDefaultSearchProvider(new_default_search
);
931 pending_synced_default_search_
= false;
934 // If it's not there, or if default search is currently managed, set a
935 // flag to indicate that we waiting on the search engine entry to come
937 pending_synced_default_search_
= true;
939 UpdateDefaultSearch();
942 syncer::SyncDataList
TemplateURLService::GetAllSyncData(
943 syncer::ModelType type
) const {
944 DCHECK_EQ(syncer::SEARCH_ENGINES
, type
);
946 syncer::SyncDataList current_data
;
947 for (TemplateURLVector::const_iterator iter
= template_urls_
.begin();
948 iter
!= template_urls_
.end(); ++iter
) {
949 // We don't sync keywords managed by policy.
950 if ((*iter
)->created_by_policy())
952 // We don't sync extension-controlled search engines.
953 if ((*iter
)->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
)
955 current_data
.push_back(CreateSyncDataFromTemplateURL(**iter
));
961 syncer::SyncError
TemplateURLService::ProcessSyncChanges(
962 const tracked_objects::Location
& from_here
,
963 const syncer::SyncChangeList
& change_list
) {
964 if (!models_associated_
) {
965 syncer::SyncError
error(FROM_HERE
,
966 syncer::SyncError::DATATYPE_ERROR
,
967 "Models not yet associated.",
968 syncer::SEARCH_ENGINES
);
973 base::AutoReset
<bool> processing_changes(&processing_syncer_changes_
, true);
975 // We've started syncing, so set our origin member to the base Sync value.
976 // As we move through Sync Code, we may set this to increasingly specific
977 // origins so we can tell what exactly caused a DSP change.
978 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(&dsp_change_origin_
,
979 DSP_CHANGE_SYNC_UNINTENTIONAL
);
981 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
983 syncer::SyncChangeList new_changes
;
984 syncer::SyncError error
;
985 for (syncer::SyncChangeList::const_iterator iter
= change_list
.begin();
986 iter
!= change_list
.end(); ++iter
) {
987 DCHECK_EQ(syncer::SEARCH_ENGINES
, iter
->sync_data().GetDataType());
990 iter
->sync_data().GetSpecifics().search_engine().sync_guid();
991 TemplateURL
* existing_turl
= GetTemplateURLForGUID(guid
);
992 scoped_ptr
<TemplateURL
> turl(CreateTemplateURLFromTemplateURLAndSyncData(
993 profile_
, existing_turl
, iter
->sync_data(), &new_changes
));
997 // Explicitly don't check for conflicts against extension keywords; in this
998 // case the functions which modify the keyword map know how to handle the
1000 // TODO(mpcomplete): If we allow editing extension keywords, then those will
1001 // need to undergo conflict resolution.
1002 TemplateURL
* existing_keyword_turl
=
1003 FindNonExtensionTemplateURLForKeyword(turl
->keyword());
1004 if (iter
->change_type() == syncer::SyncChange::ACTION_DELETE
) {
1005 if (!existing_turl
) {
1006 error
= sync_error_factory_
->CreateAndUploadError(
1008 "ProcessSyncChanges failed on ChangeType ACTION_DELETE");
1011 if (existing_turl
== GetDefaultSearchProvider()) {
1012 // The only way Sync can attempt to delete the default search provider
1013 // is if we had changed the kSyncedDefaultSearchProviderGUID
1014 // preference, but perhaps it has not yet been received. To avoid
1015 // situations where this has come in erroneously, we will un-delete
1016 // the current default search from the Sync data. If the pref really
1017 // does arrive later, then default search will change to the correct
1018 // entry, but we'll have this extra entry sitting around. The result is
1019 // not ideal, but it prevents a far more severe bug where the default is
1020 // unexpectedly swapped to something else. The user can safely delete
1021 // the extra entry again later, if they choose. Most users who do not
1022 // look at the search engines UI will not notice this.
1023 // Note that we append a special character to the end of the keyword in
1024 // an attempt to avoid a ping-poinging situation where receiving clients
1025 // may try to continually delete the resurrected entry.
1026 base::string16 updated_keyword
= UniquifyKeyword(*existing_turl
, true);
1027 TemplateURLData
data(existing_turl
->data());
1028 data
.SetKeyword(updated_keyword
);
1029 TemplateURL
new_turl(existing_turl
->profile(), data
);
1030 UIThreadSearchTermsData
search_terms_data(existing_turl
->profile());
1031 if (UpdateNoNotify(existing_turl
, new_turl
, search_terms_data
))
1034 syncer::SyncData sync_data
= CreateSyncDataFromTemplateURL(new_turl
);
1035 new_changes
.push_back(syncer::SyncChange(FROM_HERE
,
1036 syncer::SyncChange::ACTION_ADD
,
1038 // Ignore the delete attempt. This means we never end up reseting the
1039 // default search provider due to an ACTION_DELETE from sync.
1043 Remove(existing_turl
);
1044 } else if (iter
->change_type() == syncer::SyncChange::ACTION_ADD
) {
1045 if (existing_turl
) {
1046 error
= sync_error_factory_
->CreateAndUploadError(
1048 "ProcessSyncChanges failed on ChangeType ACTION_ADD");
1051 const std::string guid
= turl
->sync_guid();
1052 if (existing_keyword_turl
) {
1053 // Resolve any conflicts so we can safely add the new entry.
1054 ResolveSyncKeywordConflict(turl
.get(), existing_keyword_turl
,
1057 // Force the local ID to kInvalidTemplateURLID so we can add it.
1058 TemplateURLData
data(turl
->data());
1059 data
.id
= kInvalidTemplateURLID
;
1060 Add(new TemplateURL(profile_
, data
));
1062 // Possibly set the newly added |turl| as the default search provider.
1063 SetDefaultSearchProviderIfNewlySynced(guid
);
1064 } else if (iter
->change_type() == syncer::SyncChange::ACTION_UPDATE
) {
1065 if (!existing_turl
) {
1066 error
= sync_error_factory_
->CreateAndUploadError(
1068 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE");
1071 if (existing_keyword_turl
&& (existing_keyword_turl
!= existing_turl
)) {
1072 // Resolve any conflicts with other entries so we can safely update the
1074 ResolveSyncKeywordConflict(turl
.get(), existing_keyword_turl
,
1077 UIThreadSearchTermsData
search_terms_data(existing_turl
->profile());
1078 if (UpdateNoNotify(existing_turl
, *turl
, search_terms_data
))
1081 // We've unexpectedly received an ACTION_INVALID.
1082 error
= sync_error_factory_
->CreateAndUploadError(
1084 "ProcessSyncChanges received an ACTION_INVALID");
1088 // If something went wrong, we want to prematurely exit to avoid pushing
1089 // inconsistent data to Sync. We return the last error we received.
1093 error
= sync_processor_
->ProcessSyncChanges(from_here
, new_changes
);
1098 syncer::SyncMergeResult
TemplateURLService::MergeDataAndStartSyncing(
1099 syncer::ModelType type
,
1100 const syncer::SyncDataList
& initial_sync_data
,
1101 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
1102 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_factory
) {
1104 DCHECK_EQ(type
, syncer::SEARCH_ENGINES
);
1105 DCHECK(!sync_processor_
.get());
1106 DCHECK(sync_processor
.get());
1107 DCHECK(sync_error_factory
.get());
1108 syncer::SyncMergeResult
merge_result(type
);
1109 sync_processor_
= sync_processor
.Pass();
1110 sync_error_factory_
= sync_error_factory
.Pass();
1112 // We just started syncing, so set our wait-for-default flag if we are
1113 // expecting a default from Sync.
1115 std::string default_guid
= GetPrefs()->GetString(
1116 prefs::kSyncedDefaultSearchProviderGUID
);
1117 const TemplateURL
* current_default
= GetDefaultSearchProvider();
1119 if (!default_guid
.empty() &&
1120 (!current_default
|| current_default
->sync_guid() != default_guid
))
1121 pending_synced_default_search_
= true;
1124 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we
1125 // don't step on our own toes.
1126 base::AutoReset
<bool> processing_changes(&processing_syncer_changes_
, true);
1128 // We've started syncing, so set our origin member to the base Sync value.
1129 // As we move through Sync Code, we may set this to increasingly specific
1130 // origins so we can tell what exactly caused a DSP change.
1131 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(&dsp_change_origin_
,
1132 DSP_CHANGE_SYNC_UNINTENTIONAL
);
1134 syncer::SyncChangeList new_changes
;
1136 // Build maps of our sync GUIDs to syncer::SyncData.
1137 SyncDataMap local_data_map
= CreateGUIDToSyncDataMap(
1138 GetAllSyncData(syncer::SEARCH_ENGINES
));
1139 SyncDataMap sync_data_map
= CreateGUIDToSyncDataMap(initial_sync_data
);
1141 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
1143 merge_result
.set_num_items_before_association(local_data_map
.size());
1144 for (SyncDataMap::const_iterator iter
= sync_data_map
.begin();
1145 iter
!= sync_data_map
.end(); ++iter
) {
1146 TemplateURL
* local_turl
= GetTemplateURLForGUID(iter
->first
);
1147 scoped_ptr
<TemplateURL
> sync_turl(
1148 CreateTemplateURLFromTemplateURLAndSyncData(profile_
, local_turl
,
1149 iter
->second
, &new_changes
));
1150 if (!sync_turl
.get())
1153 if (pre_sync_deletes_
.find(sync_turl
->sync_guid()) !=
1154 pre_sync_deletes_
.end()) {
1155 // This entry was deleted before the initial sync began (possibly through
1156 // preprocessing in TemplateURLService's loading code). Ignore it and send
1157 // an ACTION_DELETE up to the server.
1158 new_changes
.push_back(
1159 syncer::SyncChange(FROM_HERE
,
1160 syncer::SyncChange::ACTION_DELETE
,
1162 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName
,
1163 DELETE_ENGINE_PRE_SYNC
, DELETE_ENGINE_MAX
);
1168 DCHECK(IsFromSync(local_turl
, sync_data_map
));
1169 // This local search engine is already synced. If the timestamp differs
1170 // from Sync, we need to update locally or to the cloud. Note that if the
1171 // timestamps are equal, we touch neither.
1172 if (sync_turl
->last_modified() > local_turl
->last_modified()) {
1173 // We've received an update from Sync. We should replace all synced
1174 // fields in the local TemplateURL. Note that this includes the
1175 // TemplateURLID and the TemplateURL may have to be reparsed. This
1176 // also makes the local data's last_modified timestamp equal to Sync's,
1177 // avoiding an Update on the next MergeData call.
1178 UIThreadSearchTermsData
search_terms_data(local_turl
->profile());
1179 if (UpdateNoNotify(local_turl
, *sync_turl
, search_terms_data
))
1181 merge_result
.set_num_items_modified(
1182 merge_result
.num_items_modified() + 1);
1183 } else if (sync_turl
->last_modified() < local_turl
->last_modified()) {
1184 // Otherwise, we know we have newer data, so update Sync with our
1186 new_changes
.push_back(
1187 syncer::SyncChange(FROM_HERE
,
1188 syncer::SyncChange::ACTION_UPDATE
,
1189 local_data_map
[local_turl
->sync_guid()]));
1191 local_data_map
.erase(iter
->first
);
1193 // The search engine from the cloud has not been synced locally. Merge it
1194 // into our local model. This will handle any conflicts with local (and
1195 // already-synced) TemplateURLs. It will prefer to keep entries from Sync
1196 // over not-yet-synced TemplateURLs.
1197 MergeInSyncTemplateURL(sync_turl
.get(), sync_data_map
, &new_changes
,
1198 &local_data_map
, &merge_result
);
1202 // If there is a pending synced default search provider that was processed
1203 // above, set it now.
1204 TemplateURL
* pending_default
= GetPendingSyncedDefaultSearchProvider();
1205 if (pending_default
) {
1206 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(
1207 &dsp_change_origin_
, DSP_CHANGE_SYNC_ADD
);
1208 SetUserSelectedDefaultSearchProvider(pending_default
);
1211 // The remaining SyncData in local_data_map should be everything that needs to
1212 // be pushed as ADDs to sync.
1213 for (SyncDataMap::const_iterator iter
= local_data_map
.begin();
1214 iter
!= local_data_map
.end(); ++iter
) {
1215 new_changes
.push_back(
1216 syncer::SyncChange(FROM_HERE
,
1217 syncer::SyncChange::ACTION_ADD
,
1221 // Do some post-processing on the change list to ensure that we are sending
1222 // valid changes to sync_processor_.
1223 PruneSyncChanges(&sync_data_map
, &new_changes
);
1225 LogDuplicatesHistogram(GetTemplateURLs());
1226 merge_result
.set_num_items_after_association(
1227 GetAllSyncData(syncer::SEARCH_ENGINES
).size());
1228 merge_result
.set_error(
1229 sync_processor_
->ProcessSyncChanges(FROM_HERE
, new_changes
));
1230 if (merge_result
.error().IsSet())
1231 return merge_result
;
1233 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to
1234 // reuse them on the next call to MergeDataAndStartSyncing.
1235 pre_sync_deletes_
.clear();
1237 models_associated_
= true;
1238 return merge_result
;
1241 void TemplateURLService::StopSyncing(syncer::ModelType type
) {
1242 DCHECK_EQ(type
, syncer::SEARCH_ENGINES
);
1243 models_associated_
= false;
1244 sync_processor_
.reset();
1245 sync_error_factory_
.reset();
1248 void TemplateURLService::ProcessTemplateURLChange(
1249 const tracked_objects::Location
& from_here
,
1250 const TemplateURL
* turl
,
1251 syncer::SyncChange::SyncChangeType type
) {
1252 DCHECK_NE(type
, syncer::SyncChange::ACTION_INVALID
);
1255 if (!models_associated_
)
1256 return; // Not syncing.
1258 if (processing_syncer_changes_
)
1259 return; // These are changes originating from us. Ignore.
1261 // Avoid syncing keywords managed by policy.
1262 if (turl
->created_by_policy())
1265 // Avoid syncing extension-controlled search engines.
1266 if (turl
->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
)
1269 syncer::SyncChangeList changes
;
1271 syncer::SyncData sync_data
= CreateSyncDataFromTemplateURL(*turl
);
1272 changes
.push_back(syncer::SyncChange(from_here
,
1276 sync_processor_
->ProcessSyncChanges(FROM_HERE
, changes
);
1280 syncer::SyncData
TemplateURLService::CreateSyncDataFromTemplateURL(
1281 const TemplateURL
& turl
) {
1282 sync_pb::EntitySpecifics specifics
;
1283 sync_pb::SearchEngineSpecifics
* se_specifics
=
1284 specifics
.mutable_search_engine();
1285 se_specifics
->set_short_name(base::UTF16ToUTF8(turl
.short_name()));
1286 se_specifics
->set_keyword(base::UTF16ToUTF8(turl
.keyword()));
1287 se_specifics
->set_favicon_url(turl
.favicon_url().spec());
1288 se_specifics
->set_url(turl
.url());
1289 se_specifics
->set_safe_for_autoreplace(turl
.safe_for_autoreplace());
1290 se_specifics
->set_originating_url(turl
.originating_url().spec());
1291 se_specifics
->set_date_created(turl
.date_created().ToInternalValue());
1292 se_specifics
->set_input_encodings(JoinString(turl
.input_encodings(), ';'));
1293 se_specifics
->set_show_in_default_list(turl
.show_in_default_list());
1294 se_specifics
->set_suggestions_url(turl
.suggestions_url());
1295 se_specifics
->set_prepopulate_id(turl
.prepopulate_id());
1296 se_specifics
->set_instant_url(turl
.instant_url());
1297 if (!turl
.image_url().empty())
1298 se_specifics
->set_image_url(turl
.image_url());
1299 se_specifics
->set_new_tab_url(turl
.new_tab_url());
1300 if (!turl
.search_url_post_params().empty())
1301 se_specifics
->set_search_url_post_params(turl
.search_url_post_params());
1302 if (!turl
.suggestions_url_post_params().empty()) {
1303 se_specifics
->set_suggestions_url_post_params(
1304 turl
.suggestions_url_post_params());
1306 if (!turl
.instant_url_post_params().empty())
1307 se_specifics
->set_instant_url_post_params(turl
.instant_url_post_params());
1308 if (!turl
.image_url_post_params().empty())
1309 se_specifics
->set_image_url_post_params(turl
.image_url_post_params());
1310 se_specifics
->set_last_modified(turl
.last_modified().ToInternalValue());
1311 se_specifics
->set_sync_guid(turl
.sync_guid());
1312 for (size_t i
= 0; i
< turl
.alternate_urls().size(); ++i
)
1313 se_specifics
->add_alternate_urls(turl
.alternate_urls()[i
]);
1314 se_specifics
->set_search_terms_replacement_key(
1315 turl
.search_terms_replacement_key());
1317 return syncer::SyncData::CreateLocalData(se_specifics
->sync_guid(),
1318 se_specifics
->keyword(),
1323 TemplateURL
* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData(
1325 TemplateURL
* existing_turl
,
1326 const syncer::SyncData
& sync_data
,
1327 syncer::SyncChangeList
* change_list
) {
1328 DCHECK(change_list
);
1330 sync_pb::SearchEngineSpecifics specifics
=
1331 sync_data
.GetSpecifics().search_engine();
1333 // Past bugs might have caused either of these fields to be empty. Just
1334 // delete this data off the server.
1335 if (specifics
.url().empty() || specifics
.sync_guid().empty()) {
1336 change_list
->push_back(
1337 syncer::SyncChange(FROM_HERE
,
1338 syncer::SyncChange::ACTION_DELETE
,
1340 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName
,
1341 DELETE_ENGINE_EMPTY_FIELD
, DELETE_ENGINE_MAX
);
1345 TemplateURLData
data(existing_turl
?
1346 existing_turl
->data() : TemplateURLData());
1347 data
.short_name
= base::UTF8ToUTF16(specifics
.short_name());
1348 data
.originating_url
= GURL(specifics
.originating_url());
1349 base::string16
keyword(base::UTF8ToUTF16(specifics
.keyword()));
1350 // NOTE: Once this code has shipped in a couple of stable releases, we can
1351 // probably remove the migration portion, comment out the
1352 // "autogenerate_keyword" field entirely in the .proto file, and fold the
1353 // empty keyword case into the "delete data" block above.
1354 bool reset_keyword
=
1355 specifics
.autogenerate_keyword() || specifics
.keyword().empty();
1357 keyword
= base::ASCIIToUTF16("dummy"); // Will be replaced below.
1358 DCHECK(!keyword
.empty());
1359 data
.SetKeyword(keyword
);
1360 data
.SetURL(specifics
.url());
1361 data
.suggestions_url
= specifics
.suggestions_url();
1362 data
.instant_url
= specifics
.instant_url();
1363 data
.image_url
= specifics
.image_url();
1364 data
.new_tab_url
= specifics
.new_tab_url();
1365 data
.search_url_post_params
= specifics
.search_url_post_params();
1366 data
.suggestions_url_post_params
= specifics
.suggestions_url_post_params();
1367 data
.instant_url_post_params
= specifics
.instant_url_post_params();
1368 data
.image_url_post_params
= specifics
.image_url_post_params();
1369 data
.favicon_url
= GURL(specifics
.favicon_url());
1370 data
.show_in_default_list
= specifics
.show_in_default_list();
1371 data
.safe_for_autoreplace
= specifics
.safe_for_autoreplace();
1372 base::SplitString(specifics
.input_encodings(), ';', &data
.input_encodings
);
1373 // If the server data has duplicate encodings, we'll want to push an update
1374 // below to correct it. Note that we also fix this in
1375 // GetSearchProvidersUsingKeywordResult(), since otherwise we'd never correct
1376 // local problems for clients which have disabled search engine sync.
1377 bool deduped
= DeDupeEncodings(&data
.input_encodings
);
1378 data
.date_created
= base::Time::FromInternalValue(specifics
.date_created());
1379 data
.last_modified
= base::Time::FromInternalValue(specifics
.last_modified());
1380 data
.prepopulate_id
= specifics
.prepopulate_id();
1381 data
.sync_guid
= specifics
.sync_guid();
1382 data
.alternate_urls
.clear();
1383 for (int i
= 0; i
< specifics
.alternate_urls_size(); ++i
)
1384 data
.alternate_urls
.push_back(specifics
.alternate_urls(i
));
1385 data
.search_terms_replacement_key
= specifics
.search_terms_replacement_key();
1387 TemplateURL
* turl
= new TemplateURL(profile
, data
);
1388 // If this TemplateURL matches a built-in prepopulated template URL, it's
1389 // possible that sync is trying to modify fields that should not be touched.
1390 // Revert these fields to the built-in values.
1391 UpdateTemplateURLIfPrepopulated(turl
, profile
);
1392 DCHECK_NE(TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
, turl
->GetType());
1393 if (reset_keyword
|| deduped
) {
1395 turl
->ResetKeywordIfNecessary(true);
1396 syncer::SyncData sync_data
= CreateSyncDataFromTemplateURL(*turl
);
1397 change_list
->push_back(syncer::SyncChange(FROM_HERE
,
1398 syncer::SyncChange::ACTION_UPDATE
,
1400 } else if (turl
->IsGoogleSearchURLWithReplaceableKeyword()) {
1401 if (!existing_turl
) {
1402 // We're adding a new TemplateURL that uses the Google base URL, so set
1403 // its keyword appropriately for the local environment.
1404 turl
->ResetKeywordIfNecessary(false);
1405 } else if (existing_turl
->IsGoogleSearchURLWithReplaceableKeyword()) {
1406 // Ignore keyword changes triggered by the Google base URL changing on
1407 // another client. If the base URL changes in this client as well, we'll
1408 // pick that up separately at the appropriate time. Otherwise, changing
1409 // the keyword here could result in having the wrong keyword for the local
1411 turl
->data_
.SetKeyword(existing_turl
->keyword());
1419 SyncDataMap
TemplateURLService::CreateGUIDToSyncDataMap(
1420 const syncer::SyncDataList
& sync_data
) {
1421 SyncDataMap data_map
;
1422 for (syncer::SyncDataList::const_iterator
i(sync_data
.begin());
1423 i
!= sync_data
.end();
1425 data_map
[i
->GetSpecifics().search_engine().sync_guid()] = *i
;
1429 void TemplateURLService::SetKeywordSearchTermsForURL(
1430 const TemplateURL
* t_url
,
1432 const base::string16
& term
) {
1433 HistoryService
* history
= profile_
?
1434 HistoryServiceFactory::GetForProfile(profile_
,
1435 Profile::EXPLICIT_ACCESS
) :
1439 history
->SetKeywordSearchTermsForURL(url
, t_url
->id(), term
);
1442 void TemplateURLService::Init(const Initializer
* initializers
,
1443 int num_initializers
) {
1444 // Register for notifications.
1446 // TODO(sky): bug 1166191. The keywords should be moved into the history
1447 // db, which will mean we no longer need this notification and the history
1448 // backend can handle automatically adding the search terms as the user
1450 content::Source
<Profile
> profile_source(profile_
->GetOriginalProfile());
1451 notification_registrar_
.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED
,
1453 notification_registrar_
.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED
,
1455 pref_change_registrar_
.Init(GetPrefs());
1456 pref_change_registrar_
.Add(
1457 prefs::kSyncedDefaultSearchProviderGUID
,
1459 &TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged
,
1460 base::Unretained(this)));
1462 notification_registrar_
.Add(
1463 this, chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED
,
1464 content::NotificationService::AllSources());
1466 if (num_initializers
> 0) {
1467 // This path is only hit by test code and is used to simulate a loaded
1468 // TemplateURLService.
1469 ChangeToLoadedState();
1471 // Add specific initializers, if any.
1472 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
1473 for (int i(0); i
< num_initializers
; ++i
) {
1474 DCHECK(initializers
[i
].keyword
);
1475 DCHECK(initializers
[i
].url
);
1476 DCHECK(initializers
[i
].content
);
1478 // TemplateURLService ends up owning the TemplateURL, don't try and free
1480 TemplateURLData data
;
1481 data
.short_name
= base::UTF8ToUTF16(initializers
[i
].content
);
1482 data
.SetKeyword(base::UTF8ToUTF16(initializers
[i
].keyword
));
1483 data
.SetURL(initializers
[i
].url
);
1484 TemplateURL
* template_url
= new TemplateURL(profile_
, data
);
1485 AddNoNotify(template_url
, true);
1487 // Set the first provided identifier to be the default.
1489 SetDefaultSearchProviderNoNotify(template_url
);
1493 // Initialize default search.
1494 UpdateDefaultSearch();
1496 // Request a server check for the correct Google URL if Google is the
1497 // default search engine and not in headless mode.
1498 if (profile_
&& initial_default_search_provider_
.get() &&
1499 initial_default_search_provider_
->url_ref().HasGoogleBaseURLs()) {
1500 scoped_ptr
<base::Environment
> env(base::Environment::Create());
1501 if (!env
->HasVar(env_vars::kHeadless
))
1502 GoogleURLTracker::RequestServerCheck(profile_
, false);
1506 void TemplateURLService::RemoveFromMaps(TemplateURL
* template_url
) {
1507 const base::string16
& keyword
= template_url
->keyword();
1508 DCHECK_NE(0U, keyword_to_template_map_
.count(keyword
));
1509 if (keyword_to_template_map_
[keyword
] == template_url
) {
1510 // We need to check whether the keyword can now be provided by another
1511 // TemplateURL. See the comments in AddToMaps() for more information on
1512 // extension keywords and how they can coexist with non-extension keywords.
1513 // In the case of more than one extension, we use the most recently
1514 // installed (which will be the most recently added, which will have the
1516 TemplateURL
* best_fallback
= NULL
;
1517 for (TemplateURLVector::const_iterator
i(template_urls_
.begin());
1518 i
!= template_urls_
.end(); ++i
) {
1519 TemplateURL
* turl
= *i
;
1520 // This next statement relies on the fact that there can only be one
1521 // non-Omnibox API TemplateURL with a given keyword.
1522 if ((turl
!= template_url
) && (turl
->keyword() == keyword
) &&
1524 (best_fallback
->GetType() != TemplateURL::OMNIBOX_API_EXTENSION
) ||
1525 ((turl
->GetType() == TemplateURL::OMNIBOX_API_EXTENSION
) &&
1526 (turl
->id() > best_fallback
->id()))))
1527 best_fallback
= turl
;
1530 keyword_to_template_map_
[keyword
] = best_fallback
;
1532 keyword_to_template_map_
.erase(keyword
);
1535 if (!template_url
->sync_guid().empty())
1536 guid_to_template_map_
.erase(template_url
->sync_guid());
1538 UIThreadSearchTermsData
search_terms_data(template_url
->profile());
1539 provider_map_
->Remove(template_url
, search_terms_data
);
1543 void TemplateURLService::AddToMaps(TemplateURL
* template_url
) {
1544 bool template_url_is_omnibox_api
=
1545 template_url
->GetType() == TemplateURL::OMNIBOX_API_EXTENSION
;
1546 const base::string16
& keyword
= template_url
->keyword();
1547 KeywordToTemplateMap::const_iterator i
=
1548 keyword_to_template_map_
.find(keyword
);
1549 if (i
== keyword_to_template_map_
.end()) {
1550 keyword_to_template_map_
[keyword
] = template_url
;
1552 const TemplateURL
* existing_url
= i
->second
;
1553 // We should only have overlapping keywords when at least one comes from
1554 // an extension. In that case, the ranking order is:
1555 // Manually-modified keywords > extension keywords > replaceable keywords
1556 // When there are multiple extensions, the last-added wins.
1557 bool existing_url_is_omnibox_api
=
1558 existing_url
->GetType() == TemplateURL::OMNIBOX_API_EXTENSION
;
1559 DCHECK(existing_url_is_omnibox_api
|| template_url_is_omnibox_api
);
1560 if (existing_url_is_omnibox_api
?
1561 !CanReplace(template_url
) : CanReplace(existing_url
))
1562 keyword_to_template_map_
[keyword
] = template_url
;
1565 if (!template_url
->sync_guid().empty())
1566 guid_to_template_map_
[template_url
->sync_guid()] = template_url
;
1568 UIThreadSearchTermsData
search_terms_data(profile_
);
1569 provider_map_
->Add(template_url
, search_terms_data
);
1573 // Helper for partition() call in next function.
1574 bool HasValidID(TemplateURL
* t_url
) {
1575 return t_url
->id() != kInvalidTemplateURLID
;
1578 void TemplateURLService::SetTemplateURLs(TemplateURLVector
* urls
) {
1579 // Partition the URLs first, instead of implementing the loops below by simply
1580 // scanning the input twice. While it's not supposed to happen normally, it's
1581 // possible for corrupt databases to return multiple entries with the same
1582 // keyword. In this case, the first loop may delete the first entry when
1583 // adding the second. If this happens, the second loop must not attempt to
1584 // access the deleted entry. Partitioning ensures this constraint.
1585 TemplateURLVector::iterator
first_invalid(
1586 std::partition(urls
->begin(), urls
->end(), HasValidID
));
1588 // First, add the items that already have id's, so that the next_id_ gets
1590 for (TemplateURLVector::const_iterator i
= urls
->begin(); i
!= first_invalid
;
1592 next_id_
= std::max(next_id_
, (*i
)->id());
1593 AddNoNotify(*i
, false);
1596 // Next add the new items that don't have id's.
1597 for (TemplateURLVector::const_iterator i
= first_invalid
; i
!= urls
->end();
1599 AddNoNotify(*i
, true);
1601 // Clear the input vector to reduce the chance callers will try to use a
1602 // (possibly deleted) entry.
1606 void TemplateURLService::ChangeToLoadedState() {
1609 UIThreadSearchTermsData
search_terms_data(profile_
);
1610 provider_map_
->Init(template_urls_
, search_terms_data
);
1614 void TemplateURLService::SaveDefaultSearchProviderToPrefs(
1615 const TemplateURL
* t_url
) {
1616 PrefService
* prefs
= GetPrefs();
1620 bool enabled
= false;
1621 std::string search_url
;
1622 std::string suggest_url
;
1623 std::string instant_url
;
1624 std::string image_url
;
1625 std::string new_tab_url
;
1626 std::string search_url_post_params
;
1627 std::string suggest_url_post_params
;
1628 std::string instant_url_post_params
;
1629 std::string image_url_post_params
;
1630 std::string icon_url
;
1631 std::string encodings
;
1632 std::string short_name
;
1633 std::string keyword
;
1634 std::string id_string
;
1635 std::string prepopulate_id
;
1636 base::ListValue alternate_urls
;
1637 std::string search_terms_replacement_key
;
1639 DCHECK_EQ(TemplateURL::NORMAL
, t_url
->GetType());
1641 search_url
= t_url
->url();
1642 suggest_url
= t_url
->suggestions_url();
1643 instant_url
= t_url
->instant_url();
1644 image_url
= t_url
->image_url();
1645 new_tab_url
= t_url
->new_tab_url();
1646 search_url_post_params
= t_url
->search_url_post_params();
1647 suggest_url_post_params
= t_url
->suggestions_url_post_params();
1648 instant_url_post_params
= t_url
->instant_url_post_params();
1649 image_url_post_params
= t_url
->image_url_post_params();
1650 GURL icon_gurl
= t_url
->favicon_url();
1651 if (!icon_gurl
.is_empty())
1652 icon_url
= icon_gurl
.spec();
1653 encodings
= JoinString(t_url
->input_encodings(), ';');
1654 short_name
= base::UTF16ToUTF8(t_url
->short_name());
1655 keyword
= base::UTF16ToUTF8(t_url
->keyword());
1656 id_string
= base::Int64ToString(t_url
->id());
1657 prepopulate_id
= base::Int64ToString(t_url
->prepopulate_id());
1658 for (size_t i
= 0; i
< t_url
->alternate_urls().size(); ++i
)
1659 alternate_urls
.AppendString(t_url
->alternate_urls()[i
]);
1660 search_terms_replacement_key
= t_url
->search_terms_replacement_key();
1662 prefs
->SetBoolean(prefs::kDefaultSearchProviderEnabled
, enabled
);
1663 prefs
->SetString(prefs::kDefaultSearchProviderSearchURL
, search_url
);
1664 prefs
->SetString(prefs::kDefaultSearchProviderSuggestURL
, suggest_url
);
1665 prefs
->SetString(prefs::kDefaultSearchProviderInstantURL
, instant_url
);
1666 prefs
->SetString(prefs::kDefaultSearchProviderImageURL
, image_url
);
1667 prefs
->SetString(prefs::kDefaultSearchProviderNewTabURL
, new_tab_url
);
1668 prefs
->SetString(prefs::kDefaultSearchProviderSearchURLPostParams
,
1669 search_url_post_params
);
1670 prefs
->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams
,
1671 suggest_url_post_params
);
1672 prefs
->SetString(prefs::kDefaultSearchProviderInstantURLPostParams
,
1673 instant_url_post_params
);
1674 prefs
->SetString(prefs::kDefaultSearchProviderImageURLPostParams
,
1675 image_url_post_params
);
1676 prefs
->SetString(prefs::kDefaultSearchProviderIconURL
, icon_url
);
1677 prefs
->SetString(prefs::kDefaultSearchProviderEncodings
, encodings
);
1678 prefs
->SetString(prefs::kDefaultSearchProviderName
, short_name
);
1679 prefs
->SetString(prefs::kDefaultSearchProviderKeyword
, keyword
);
1680 prefs
->SetString(prefs::kDefaultSearchProviderID
, id_string
);
1681 prefs
->SetString(prefs::kDefaultSearchProviderPrepopulateID
, prepopulate_id
);
1682 prefs
->Set(prefs::kDefaultSearchProviderAlternateURLs
, alternate_urls
);
1683 prefs
->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey
,
1684 search_terms_replacement_key
);
1687 bool TemplateURLService::LoadDefaultSearchProviderFromPrefs(
1688 scoped_ptr
<TemplateURL
>* default_provider
,
1690 PrefService
* prefs
= GetPrefs();
1691 if (!prefs
|| !prefs
->HasPrefPath(prefs::kDefaultSearchProviderSearchURL
))
1694 const PrefService::Preference
* pref
=
1695 prefs
->FindPreference(prefs::kDefaultSearchProviderSearchURL
);
1696 *is_managed
= pref
&& pref
->IsManaged();
1698 if (!prefs
->GetBoolean(prefs::kDefaultSearchProviderEnabled
)) {
1699 // The user doesn't want a default search provider.
1700 default_provider
->reset(NULL
);
1704 base::string16 name
=
1705 base::UTF8ToUTF16(prefs
->GetString(prefs::kDefaultSearchProviderName
));
1706 base::string16 keyword
=
1707 base::UTF8ToUTF16(prefs
->GetString(prefs::kDefaultSearchProviderKeyword
));
1708 // Force keyword to be non-empty.
1709 // TODO(pkasting): This is only necessary as long as we're potentially loading
1710 // older prefs where empty keywords are theoretically possible. Eventually
1711 // this code can be replaced with a DCHECK(!keyword.empty());.
1712 bool update_keyword
= keyword
.empty();
1714 keyword
= base::ASCIIToUTF16("dummy");
1715 std::string search_url
=
1716 prefs
->GetString(prefs::kDefaultSearchProviderSearchURL
);
1717 // Force URL to be non-empty. We've never supported this case, but past bugs
1718 // might have resulted in it slipping through; eventually this code can be
1719 // replaced with a DCHECK(!search_url.empty());.
1720 if (search_url
.empty())
1722 std::string suggest_url
=
1723 prefs
->GetString(prefs::kDefaultSearchProviderSuggestURL
);
1724 std::string instant_url
=
1725 prefs
->GetString(prefs::kDefaultSearchProviderInstantURL
);
1726 std::string image_url
=
1727 prefs
->GetString(prefs::kDefaultSearchProviderImageURL
);
1728 std::string new_tab_url
=
1729 prefs
->GetString(prefs::kDefaultSearchProviderNewTabURL
);
1730 std::string search_url_post_params
=
1731 prefs
->GetString(prefs::kDefaultSearchProviderSearchURLPostParams
);
1732 std::string suggest_url_post_params
=
1733 prefs
->GetString(prefs::kDefaultSearchProviderSuggestURLPostParams
);
1734 std::string instant_url_post_params
=
1735 prefs
->GetString(prefs::kDefaultSearchProviderInstantURLPostParams
);
1736 std::string image_url_post_params
=
1737 prefs
->GetString(prefs::kDefaultSearchProviderImageURLPostParams
);
1738 std::string icon_url
=
1739 prefs
->GetString(prefs::kDefaultSearchProviderIconURL
);
1740 std::string encodings
=
1741 prefs
->GetString(prefs::kDefaultSearchProviderEncodings
);
1742 std::string id_string
= prefs
->GetString(prefs::kDefaultSearchProviderID
);
1743 std::string prepopulate_id
=
1744 prefs
->GetString(prefs::kDefaultSearchProviderPrepopulateID
);
1745 const base::ListValue
* alternate_urls
=
1746 prefs
->GetList(prefs::kDefaultSearchProviderAlternateURLs
);
1747 std::string search_terms_replacement_key
= prefs
->GetString(
1748 prefs::kDefaultSearchProviderSearchTermsReplacementKey
);
1750 TemplateURLData data
;
1751 data
.short_name
= name
;
1752 data
.SetKeyword(keyword
);
1753 data
.SetURL(search_url
);
1754 data
.suggestions_url
= suggest_url
;
1755 data
.instant_url
= instant_url
;
1756 data
.image_url
= image_url
;
1757 data
.new_tab_url
= new_tab_url
;
1758 data
.search_url_post_params
= search_url_post_params
;
1759 data
.suggestions_url_post_params
= suggest_url_post_params
;
1760 data
.instant_url_post_params
= instant_url_post_params
;
1761 data
.image_url_post_params
= image_url_post_params
;
1762 data
.favicon_url
= GURL(icon_url
);
1763 data
.show_in_default_list
= true;
1764 data
.alternate_urls
.clear();
1765 for (size_t i
= 0; i
< alternate_urls
->GetSize(); ++i
) {
1766 std::string alternate_url
;
1767 if (alternate_urls
->GetString(i
, &alternate_url
))
1768 data
.alternate_urls
.push_back(alternate_url
);
1770 data
.search_terms_replacement_key
= search_terms_replacement_key
;
1771 base::SplitString(encodings
, ';', &data
.input_encodings
);
1772 if (!id_string
.empty() && !*is_managed
) {
1774 base::StringToInt64(id_string
, &value
);
1777 if (!prepopulate_id
.empty() && !*is_managed
) {
1779 base::StringToInt(prepopulate_id
, &value
);
1780 data
.prepopulate_id
= value
;
1782 default_provider
->reset(new TemplateURL(profile_
, data
));
1783 DCHECK_EQ(TemplateURL::NORMAL
, (*default_provider
)->GetType());
1785 (*default_provider
)->ResetKeywordIfNecessary(true);
1789 void TemplateURLService::ClearDefaultProviderFromPrefs() {
1790 // We overwrite user preferences. If the default search engine is managed,
1791 // there is no effect.
1792 SaveDefaultSearchProviderToPrefs(NULL
);
1793 // Default value for kDefaultSearchProviderEnabled is true.
1794 PrefService
* prefs
= GetPrefs();
1796 prefs
->SetBoolean(prefs::kDefaultSearchProviderEnabled
, true);
1799 bool TemplateURLService::CanReplaceKeywordForHost(
1800 const std::string
& host
,
1801 TemplateURL
** to_replace
) {
1802 DCHECK(!to_replace
|| !*to_replace
);
1803 const TemplateURLSet
* urls
= provider_map_
->GetURLsForHost(host
);
1806 for (TemplateURLSet::const_iterator
i(urls
->begin()); i
!= urls
->end(); ++i
) {
1807 if (CanReplace(*i
)) {
1816 bool TemplateURLService::CanReplace(const TemplateURL
* t_url
) {
1817 return (t_url
!= default_search_provider_
&& !t_url
->show_in_default_list() &&
1818 t_url
->safe_for_autoreplace());
1821 TemplateURL
* TemplateURLService::FindNonExtensionTemplateURLForKeyword(
1822 const base::string16
& keyword
) {
1823 TemplateURL
* keyword_turl
= GetTemplateURLForKeyword(keyword
);
1824 if (!keyword_turl
|| (keyword_turl
->GetType() == TemplateURL::NORMAL
))
1825 return keyword_turl
;
1826 // The extension keyword in the model may be hiding a replaceable
1827 // non-extension keyword. Look for it.
1828 for (TemplateURLVector::const_iterator
i(template_urls_
.begin());
1829 i
!= template_urls_
.end(); ++i
) {
1830 if (((*i
)->GetType() == TemplateURL::NORMAL
) &&
1831 ((*i
)->keyword() == keyword
))
1837 bool TemplateURLService::UpdateNoNotify(
1838 TemplateURL
* existing_turl
,
1839 const TemplateURL
& new_values
,
1840 const SearchTermsData
& old_search_terms_data
) {
1842 DCHECK(existing_turl
);
1843 if (std::find(template_urls_
.begin(), template_urls_
.end(), existing_turl
) ==
1844 template_urls_
.end())
1847 base::string16
old_keyword(existing_turl
->keyword());
1848 keyword_to_template_map_
.erase(old_keyword
);
1849 if (!existing_turl
->sync_guid().empty())
1850 guid_to_template_map_
.erase(existing_turl
->sync_guid());
1852 provider_map_
->Remove(existing_turl
, old_search_terms_data
);
1853 TemplateURLID previous_id
= existing_turl
->id();
1854 existing_turl
->CopyFrom(new_values
);
1855 existing_turl
->data_
.id
= previous_id
;
1856 UIThreadSearchTermsData
new_search_terms_data(profile_
);
1857 provider_map_
->Add(existing_turl
, new_search_terms_data
);
1859 const base::string16
& keyword
= existing_turl
->keyword();
1860 KeywordToTemplateMap::const_iterator i
=
1861 keyword_to_template_map_
.find(keyword
);
1862 if (i
== keyword_to_template_map_
.end()) {
1863 keyword_to_template_map_
[keyword
] = existing_turl
;
1865 // We can theoretically reach here in two cases:
1866 // * There is an existing extension keyword and sync brings in a rename of
1867 // a non-extension keyword to match. In this case we just need to pick
1868 // which keyword has priority to update the keyword map.
1869 // * Autogeneration of the keyword for a Google default search provider
1870 // at load time causes it to conflict with an existing keyword. In this
1871 // case we delete the existing keyword if it's replaceable, or else undo
1872 // the change in keyword for |existing_turl|.
1873 TemplateURL
* existing_keyword_turl
= i
->second
;
1874 if (existing_keyword_turl
->GetType() != TemplateURL::NORMAL
) {
1875 if (!CanReplace(existing_turl
))
1876 keyword_to_template_map_
[keyword
] = existing_turl
;
1878 if (CanReplace(existing_keyword_turl
)) {
1879 RemoveNoNotify(existing_keyword_turl
);
1881 existing_turl
->data_
.SetKeyword(old_keyword
);
1882 keyword_to_template_map_
[old_keyword
] = existing_turl
;
1886 if (!existing_turl
->sync_guid().empty())
1887 guid_to_template_map_
[existing_turl
->sync_guid()] = existing_turl
;
1890 service_
->UpdateKeyword(existing_turl
->data());
1892 // Inform sync of the update.
1893 ProcessTemplateURLChange(FROM_HERE
,
1895 syncer::SyncChange::ACTION_UPDATE
);
1897 if (default_search_provider_
== existing_turl
) {
1898 bool success
= SetDefaultSearchProviderNoNotify(existing_turl
);
1905 void TemplateURLService::UpdateTemplateURLIfPrepopulated(
1906 TemplateURL
* template_url
,
1908 int prepopulate_id
= template_url
->prepopulate_id();
1909 if (template_url
->prepopulate_id() == 0)
1912 size_t default_search_index
;
1913 ScopedVector
<TemplateURL
> prepopulated_urls
=
1914 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile
,
1915 &default_search_index
);
1917 for (size_t i
= 0; i
< prepopulated_urls
.size(); ++i
) {
1918 if (prepopulated_urls
[i
]->prepopulate_id() == prepopulate_id
) {
1919 MergeIntoPrepopulatedEngineData(&prepopulated_urls
[i
]->data_
,
1921 template_url
->CopyFrom(*prepopulated_urls
[i
]);
1926 PrefService
* TemplateURLService::GetPrefs() {
1927 return profile_
? profile_
->GetPrefs() : NULL
;
1930 void TemplateURLService::UpdateKeywordSearchTermsForURL(
1931 const history::URLVisitedDetails
& details
) {
1932 const history::URLRow
& row
= details
.row
;
1933 if (!row
.url().is_valid())
1936 const TemplateURLSet
* urls_for_host
=
1937 provider_map_
->GetURLsForHost(row
.url().host());
1941 for (TemplateURLSet::const_iterator i
= urls_for_host
->begin();
1942 i
!= urls_for_host
->end(); ++i
) {
1943 base::string16 search_terms
;
1944 if ((*i
)->ExtractSearchTermsFromURL(row
.url(), &search_terms
) &&
1945 !search_terms
.empty()) {
1946 if (content::PageTransitionStripQualifier(details
.transition
) ==
1947 content::PAGE_TRANSITION_KEYWORD
) {
1948 // The visit is the result of the user entering a keyword, generate a
1949 // KEYWORD_GENERATED visit for the KEYWORD so that the keyword typed
1950 // count is boosted.
1951 AddTabToSearchVisit(**i
);
1953 SetKeywordSearchTermsForURL(*i
, row
.url(), search_terms
);
1958 void TemplateURLService::AddTabToSearchVisit(const TemplateURL
& t_url
) {
1959 // Only add visits for entries the user hasn't modified. If the user modified
1960 // the entry the keyword may no longer correspond to the host name. It may be
1961 // possible to do something more sophisticated here, but it's so rare as to
1963 if (!t_url
.safe_for_autoreplace())
1969 HistoryService
* history
=
1970 HistoryServiceFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
);
1974 GURL
url(URLFixerUpper::FixupURL(base::UTF16ToUTF8(t_url
.keyword()),
1976 if (!url
.is_valid())
1979 // Synthesize a visit for the keyword. This ensures the url for the keyword is
1980 // autocompleted even if the user doesn't type the url in directly.
1981 history
->AddPage(url
, base::Time::Now(), NULL
, 0, GURL(),
1982 history::RedirectList(),
1983 content::PAGE_TRANSITION_KEYWORD_GENERATED
,
1984 history::SOURCE_BROWSED
, false);
1987 void TemplateURLService::GoogleBaseURLChanged(const GURL
& old_base_url
) {
1988 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
1989 bool something_changed
= false;
1990 for (TemplateURLVector::iterator
i(template_urls_
.begin());
1991 i
!= template_urls_
.end(); ++i
) {
1992 TemplateURL
* t_url
= *i
;
1993 if (t_url
->url_ref().HasGoogleBaseURLs() ||
1994 t_url
->suggestions_url_ref().HasGoogleBaseURLs()) {
1995 TemplateURL
updated_turl(t_url
->profile(), t_url
->data());
1996 updated_turl
.ResetKeywordIfNecessary(false);
1997 KeywordToTemplateMap::const_iterator existing_entry
=
1998 keyword_to_template_map_
.find(updated_turl
.keyword());
1999 if ((existing_entry
!= keyword_to_template_map_
.end()) &&
2000 (existing_entry
->second
!= t_url
)) {
2001 // The new autogenerated keyword conflicts with another TemplateURL.
2002 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its
2003 // current keyword. (This will not prevent |t_url| from auto-updating
2004 // the keyword in the future if the conflicting TemplateURL disappears.)
2005 // Note that we must still update |t_url| in this case, or the
2006 // |provider_map_| will not be updated correctly.
2007 if (CanReplace(existing_entry
->second
))
2008 RemoveNoNotify(existing_entry
->second
);
2010 updated_turl
.data_
.SetKeyword(t_url
->keyword());
2012 something_changed
= true;
2013 // This will send the keyword change to sync. Note that other clients
2014 // need to reset the keyword to an appropriate local value when this
2015 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData().
2016 UpdateNoNotify(t_url
, updated_turl
,
2017 OldBaseURLSearchTermsData(t_url
->profile(), old_base_url
.spec()));
2020 if (something_changed
)
2024 void TemplateURLService::UpdateDefaultSearch() {
2026 // Set |initial_default_search_provider_| from the preferences. We use this
2027 // value for default search provider until the database has been loaded.
2028 if (!LoadDefaultSearchProviderFromPrefs(&initial_default_search_provider_
,
2029 &is_default_search_managed_
)) {
2030 // Prefs does not specify, so rely on the prepopulated engines. This
2031 // should happen only the first time Chrome is started.
2032 initial_default_search_provider_
.reset(
2033 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_
));
2034 is_default_search_managed_
= false;
2038 // Load the default search specified in prefs.
2039 scoped_ptr
<TemplateURL
> new_default_from_prefs
;
2040 bool new_is_default_managed
= false;
2041 // Load the default from prefs. It's possible that it won't succeed
2042 // because we are in the middle of doing SaveDefaultSearchProviderToPrefs()
2043 // and all the preference items have not been saved. In that case, we
2044 // don't have yet a default. It would be much better if we could save
2045 // preferences in batches and trigger notifications at the end.
2046 LoadDefaultSearchProviderFromPrefs(&new_default_from_prefs
,
2047 &new_is_default_managed
);
2048 if (!is_default_search_managed_
&& !new_is_default_managed
) {
2049 // We're not interested in cases where the default was and remains
2050 // unmanaged. In that case, preferences have no impact on the default.
2053 WebDataService::KeywordBatchModeScoper
keyword_scoper(service_
.get());
2054 if (is_default_search_managed_
&& new_is_default_managed
) {
2055 // The default was managed and remains managed. Update the default only
2056 // if it has changed; we don't want to respond to changes triggered by
2057 // SaveDefaultSearchProviderToPrefs.
2058 if (TemplateURLsHaveSamePrefs(default_search_provider_
,
2059 new_default_from_prefs
.get()))
2061 if (new_default_from_prefs
.get() == NULL
) {
2062 // default_search_provider_ can't be NULL otherwise
2063 // TemplateURLsHaveSamePrefs would have returned true. Remove this now
2065 TemplateURL
* old_default
= default_search_provider_
;
2066 bool success
= SetDefaultSearchProviderNoNotify(NULL
);
2068 RemoveNoNotify(old_default
);
2069 } else if (default_search_provider_
) {
2070 TemplateURLData
data(new_default_from_prefs
->data());
2071 data
.created_by_policy
= true;
2072 TemplateURL
new_values(new_default_from_prefs
->profile(), data
);
2073 UIThreadSearchTermsData
search_terms_data(
2074 default_search_provider_
->profile());
2075 UpdateNoNotify(default_search_provider_
, new_values
, search_terms_data
);
2077 TemplateURL
* new_template
= NULL
;
2078 if (new_default_from_prefs
.get()) {
2079 TemplateURLData
data(new_default_from_prefs
->data());
2080 data
.created_by_policy
= true;
2081 new_template
= new TemplateURL(profile_
, data
);
2082 if (!AddNoNotify(new_template
, true))
2085 bool success
= SetDefaultSearchProviderNoNotify(new_template
);
2088 } else if (!is_default_search_managed_
&& new_is_default_managed
) {
2089 // The default used to be unmanaged and is now managed. Add the new
2090 // managed default to the list of URLs and set it as default.
2091 is_default_search_managed_
= new_is_default_managed
;
2092 TemplateURL
* new_template
= NULL
;
2093 if (new_default_from_prefs
.get()) {
2094 TemplateURLData
data(new_default_from_prefs
->data());
2095 data
.created_by_policy
= true;
2096 new_template
= new TemplateURL(profile_
, data
);
2097 if (!AddNoNotify(new_template
, true))
2100 bool success
= SetDefaultSearchProviderNoNotify(new_template
);
2103 // The default was managed and is no longer.
2104 DCHECK(is_default_search_managed_
&& !new_is_default_managed
);
2105 is_default_search_managed_
= new_is_default_managed
;
2106 // If we had a default, delete the previous default if created by policy
2107 // and set a likely default.
2108 if ((default_search_provider_
!= NULL
) &&
2109 default_search_provider_
->created_by_policy()) {
2110 TemplateURL
* old_default
= default_search_provider_
;
2111 default_search_provider_
= NULL
;
2112 RemoveNoNotify(old_default
);
2115 // The likely default should be from Sync if we were waiting on Sync.
2116 // Otherwise, it should be FindNewDefaultSearchProvider.
2117 TemplateURL
* synced_default
= GetPendingSyncedDefaultSearchProvider();
2118 if (synced_default
) {
2119 pending_synced_default_search_
= false;
2121 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(
2122 &dsp_change_origin_
, DSP_CHANGE_SYNC_NOT_MANAGED
);
2123 SetDefaultSearchProviderNoNotify(synced_default
);
2125 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider());
2131 void TemplateURLService::SetDefaultSearchProvider(
2133 DCHECK(!is_default_search_managed_
);
2134 // Omnibox keywords cannot be made default. Extension-controlled search
2135 // engines can be made default only by the extension itself because they
2136 // aren't persisted.
2137 DCHECK(!url
|| (url
->GetType() == TemplateURL::NORMAL
));
2139 // Always persist the setting in the database, that way if the backup
2140 // signature has changed out from under us it gets reset correctly.
2141 if (SetDefaultSearchProviderNoNotify(url
))
2145 bool TemplateURLService::SetDefaultSearchProviderNoNotify(TemplateURL
* url
) {
2147 if (std::find(template_urls_
.begin(), template_urls_
.end(), url
) ==
2148 template_urls_
.end())
2150 // Omnibox keywords cannot be made default.
2151 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION
, url
->GetType());
2154 // Only bother reassigning |url| if it has changed. Notice that we don't just
2155 // early exit if they are equal, because |url| may have had its fields
2156 // changed, and needs to be persisted below (for example, when this is called
2157 // from UpdateNoNotify).
2158 if (default_search_provider_
!= url
) {
2159 // Engines set by policy override extension-controlled engines, which
2160 // override other engines.
2161 DCHECK(!is_default_search_managed() || !url
||
2162 (url
->GetType() == TemplateURL::NORMAL
));
2163 if (is_default_search_managed() || !default_search_provider_
||
2164 (default_search_provider_
->GetType() == TemplateURL::NORMAL
) ||
2166 (url
->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
))){
2167 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchChangeOrigin",
2168 dsp_change_origin_
, DSP_CHANGE_MAX
);
2169 default_search_provider_
= url
;
2174 // Don't mark the url as edited, otherwise we won't be able to rev the
2175 // template urls we ship with.
2176 url
->data_
.show_in_default_list
= true;
2177 if (service_
.get() && (url
->GetType() == TemplateURL::NORMAL
))
2178 service_
->UpdateKeyword(url
->data());
2180 if (url
->url_ref().HasGoogleBaseURLs()) {
2181 GoogleURLTracker::RequestServerCheck(profile_
, false);
2182 #if defined(ENABLE_RLZ)
2183 RLZTracker::RecordProductEvent(rlz_lib::CHROME
,
2184 RLZTracker::CHROME_OMNIBOX
,
2185 rlz_lib::SET_TO_GOOGLE
);
2190 // Extension-controlled search engines shouldn't be persisted anywhere.
2191 if (url
&& (url
->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
))
2194 if (!is_default_search_managed_
) {
2195 SaveDefaultSearchProviderToPrefs(url
);
2197 // If we are syncing, we want to set the synced pref that will notify other
2198 // instances to change their default to this new search provider.
2199 // Note: we don't update the pref if we're currently in the middle of
2200 // handling a sync operation. Sync operations from other clients are not
2201 // guaranteed to arrive together, and any client that deletes the default
2202 // needs to set a new default as well. If we update the default here, we're
2203 // likely to race with the update from the other client, resulting in
2204 // a possibly random default search provider.
2205 if (sync_processor_
.get() && url
&& !url
->sync_guid().empty() &&
2206 GetPrefs() && !processing_syncer_changes_
) {
2207 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID
,
2213 service_
->SetDefaultSearchProviderID(url
? url
->id() : 0);
2215 // Inform sync the change to the show_in_default_list flag.
2217 ProcessTemplateURLChange(FROM_HERE
,
2219 syncer::SyncChange::ACTION_UPDATE
);
2223 bool TemplateURLService::AddNoNotify(TemplateURL
* template_url
,
2224 bool newly_adding
) {
2225 DCHECK(template_url
);
2228 DCHECK_EQ(kInvalidTemplateURLID
, template_url
->id());
2229 DCHECK(std::find(template_urls_
.begin(), template_urls_
.end(),
2230 template_url
) == template_urls_
.end());
2231 template_url
->data_
.id
= ++next_id_
;
2234 template_url
->ResetKeywordIfNecessary(false);
2235 // Check whether |template_url|'s keyword conflicts with any already in the
2237 TemplateURL
* existing_keyword_turl
=
2238 GetTemplateURLForKeyword(template_url
->keyword());
2239 if (existing_keyword_turl
!= NULL
) {
2240 DCHECK_NE(existing_keyword_turl
, template_url
);
2241 // Only replace one of the TemplateURLs if they are either both extensions,
2242 // or both not extensions.
2243 bool are_same_type
= existing_keyword_turl
->GetType() ==
2244 template_url
->GetType();
2245 if (CanReplace(existing_keyword_turl
) && are_same_type
) {
2246 RemoveNoNotify(existing_keyword_turl
);
2247 } else if (CanReplace(template_url
) && are_same_type
) {
2248 delete template_url
;
2251 base::string16 new_keyword
=
2252 UniquifyKeyword(*existing_keyword_turl
, false);
2253 ResetTemplateURL(existing_keyword_turl
,
2254 existing_keyword_turl
->short_name(), new_keyword
,
2255 existing_keyword_turl
->url());
2258 template_urls_
.push_back(template_url
);
2259 AddToMaps(template_url
);
2262 (template_url
->GetType() !=
2263 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
)) {
2265 service_
->AddKeyword(template_url
->data());
2267 // Inform sync of the addition. Note that this will assign a GUID to
2268 // template_url and add it to the guid_to_template_map_.
2269 ProcessTemplateURLChange(FROM_HERE
,
2271 syncer::SyncChange::ACTION_ADD
);
2277 void TemplateURLService::RemoveNoNotify(TemplateURL
* template_url
) {
2278 DCHECK(template_url
!= default_search_provider_
);
2280 TemplateURLVector::iterator i
=
2281 std::find(template_urls_
.begin(), template_urls_
.end(), template_url
);
2282 if (i
== template_urls_
.end())
2285 RemoveFromMaps(template_url
);
2287 // Remove it from the vector containing all TemplateURLs.
2288 template_urls_
.erase(i
);
2290 if (template_url
->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
) {
2292 service_
->RemoveKeyword(template_url
->id());
2294 // Inform sync of the deletion.
2295 ProcessTemplateURLChange(FROM_HERE
,
2297 syncer::SyncChange::ACTION_DELETE
);
2299 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName
,
2300 DELETE_ENGINE_USER_ACTION
, DELETE_ENGINE_MAX
);
2304 content::Source
<Profile
> source(profile_
);
2305 TemplateURLID id
= template_url
->id();
2306 content::NotificationService::current()->Notify(
2307 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED
,
2309 content::Details
<TemplateURLID
>(&id
));
2312 // We own the TemplateURL and need to delete it.
2313 delete template_url
;
2316 void TemplateURLService::NotifyObservers() {
2320 FOR_EACH_OBSERVER(TemplateURLServiceObserver
, model_observers_
,
2321 OnTemplateURLServiceChanged());
2324 // |template_urls| are the TemplateURLs loaded from the database.
2325 // |default_search_provider| points to one of them, if it was set in the db.
2326 // |default_from_prefs| is the default search provider from the preferences.
2327 // Check |is_default_search_managed_| to determine if it was set by policy.
2329 // This function removes from the vector and the database all the TemplateURLs
2330 // that were set by policy, unless it is the current default search provider
2331 // and matches what is set by a managed preference.
2332 void TemplateURLService::RemoveProvidersCreatedByPolicy(
2333 TemplateURLVector
* template_urls
,
2334 TemplateURL
** default_search_provider
,
2335 TemplateURL
* default_from_prefs
) {
2336 DCHECK(template_urls
);
2337 DCHECK(default_search_provider
);
2338 for (TemplateURLVector::iterator i
= template_urls
->begin();
2339 i
!= template_urls
->end(); ) {
2340 TemplateURL
* template_url
= *i
;
2341 if (template_url
->created_by_policy()) {
2342 if (template_url
== *default_search_provider
&&
2343 is_default_search_managed_
&&
2344 TemplateURLsHaveSamePrefs(template_url
,
2345 default_from_prefs
)) {
2346 // If the database specified a default search provider that was set
2347 // by policy, and the default search provider from the preferences
2348 // is also set by policy and they are the same, keep the entry in the
2349 // database and the |default_search_provider|.
2354 // The database loaded a managed |default_search_provider|, but it has
2355 // been updated in the prefs. Remove it from the database, and update the
2356 // |default_search_provider| pointer here.
2357 if (*default_search_provider
&&
2358 (*default_search_provider
)->id() == template_url
->id())
2359 *default_search_provider
= NULL
;
2361 i
= template_urls
->erase(i
);
2363 service_
->RemoveKeyword(template_url
->id());
2364 delete template_url
;
2371 void TemplateURLService::ResetTemplateURLGUID(TemplateURL
* url
,
2372 const std::string
& guid
) {
2374 DCHECK(!guid
.empty());
2376 TemplateURLData
data(url
->data());
2377 data
.sync_guid
= guid
;
2378 TemplateURL
new_url(url
->profile(), data
);
2379 UIThreadSearchTermsData
search_terms_data(url
->profile());
2380 UpdateNoNotify(url
, new_url
, search_terms_data
);
2383 base::string16
TemplateURLService::UniquifyKeyword(const TemplateURL
& turl
,
2387 if (!GetTemplateURLForKeyword(turl
.keyword()))
2388 return turl
.keyword();
2390 // First, try to return the generated keyword for the TemplateURL (except
2391 // for extensions, as their keywords are not associated with their URLs).
2392 GURL
gurl(turl
.url());
2393 if (gurl
.is_valid() &&
2394 (turl
.GetType() != TemplateURL::OMNIBOX_API_EXTENSION
)) {
2395 base::string16 keyword_candidate
= GenerateKeyword(gurl
);
2396 if (!GetTemplateURLForKeyword(keyword_candidate
))
2397 return keyword_candidate
;
2401 // We try to uniquify the keyword by appending a special character to the end.
2402 // This is a best-effort approach where we try to preserve the original
2403 // keyword and let the user do what they will after our attempt.
2404 base::string16
keyword_candidate(turl
.keyword());
2406 keyword_candidate
.append(base::ASCIIToUTF16("_"));
2407 } while (GetTemplateURLForKeyword(keyword_candidate
));
2409 return keyword_candidate
;
2412 bool TemplateURLService::IsLocalTemplateURLBetter(
2413 const TemplateURL
* local_turl
,
2414 const TemplateURL
* sync_turl
) {
2415 DCHECK(GetTemplateURLForGUID(local_turl
->sync_guid()));
2416 return local_turl
->last_modified() > sync_turl
->last_modified() ||
2417 local_turl
->created_by_policy() ||
2418 local_turl
== GetDefaultSearchProvider();
2421 void TemplateURLService::ResolveSyncKeywordConflict(
2422 TemplateURL
* unapplied_sync_turl
,
2423 TemplateURL
* applied_sync_turl
,
2424 syncer::SyncChangeList
* change_list
) {
2426 DCHECK(unapplied_sync_turl
);
2427 DCHECK(applied_sync_turl
);
2428 DCHECK(change_list
);
2429 DCHECK_EQ(applied_sync_turl
->keyword(), unapplied_sync_turl
->keyword());
2430 DCHECK_NE(TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
,
2431 applied_sync_turl
->GetType());
2433 // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so
2434 // don't delete either of them. Instead, determine which is "better" and
2435 // uniquify the other one, sending an update to the server for the updated
2437 const bool applied_turl_is_better
=
2438 IsLocalTemplateURLBetter(applied_sync_turl
, unapplied_sync_turl
);
2439 TemplateURL
* loser
= applied_turl_is_better
?
2440 unapplied_sync_turl
: applied_sync_turl
;
2441 base::string16 new_keyword
= UniquifyKeyword(*loser
, false);
2442 DCHECK(!GetTemplateURLForKeyword(new_keyword
));
2443 if (applied_turl_is_better
) {
2444 // Just set the keyword of |unapplied_sync_turl|. The caller is responsible
2445 // for adding or updating unapplied_sync_turl in the local model.
2446 unapplied_sync_turl
->data_
.SetKeyword(new_keyword
);
2448 // Update |applied_sync_turl| in the local model with the new keyword.
2449 TemplateURLData
data(applied_sync_turl
->data());
2450 data
.SetKeyword(new_keyword
);
2451 TemplateURL
new_turl(applied_sync_turl
->profile(), data
);
2452 UIThreadSearchTermsData
search_terms_data(applied_sync_turl
->profile());
2453 if (UpdateNoNotify(applied_sync_turl
, new_turl
, search_terms_data
))
2456 // The losing TemplateURL should have their keyword updated. Send a change to
2457 // the server to reflect this change.
2458 syncer::SyncData sync_data
= CreateSyncDataFromTemplateURL(*loser
);
2459 change_list
->push_back(syncer::SyncChange(FROM_HERE
,
2460 syncer::SyncChange::ACTION_UPDATE
,
2464 void TemplateURLService::MergeInSyncTemplateURL(
2465 TemplateURL
* sync_turl
,
2466 const SyncDataMap
& sync_data
,
2467 syncer::SyncChangeList
* change_list
,
2468 SyncDataMap
* local_data
,
2469 syncer::SyncMergeResult
* merge_result
) {
2471 DCHECK(!GetTemplateURLForGUID(sync_turl
->sync_guid()));
2472 DCHECK(IsFromSync(sync_turl
, sync_data
));
2474 TemplateURL
* conflicting_turl
=
2475 FindNonExtensionTemplateURLForKeyword(sync_turl
->keyword());
2476 bool should_add_sync_turl
= true;
2478 // If there was no TemplateURL in the local model that conflicts with
2479 // |sync_turl|, skip the following preparation steps and just add |sync_turl|
2480 // directly. Otherwise, modify |conflicting_turl| to make room for
2482 if (conflicting_turl
) {
2483 if (IsFromSync(conflicting_turl
, sync_data
)) {
2484 // |conflicting_turl| is already known to Sync, so we're not allowed to
2485 // remove it. In this case, we want to uniquify the worse one and send an
2486 // update for the changed keyword to sync. We can reuse the logic from
2487 // ResolveSyncKeywordConflict for this.
2488 ResolveSyncKeywordConflict(sync_turl
, conflicting_turl
, change_list
);
2489 merge_result
->set_num_items_modified(
2490 merge_result
->num_items_modified() + 1);
2492 // |conflicting_turl| is not yet known to Sync. If it is better, then we
2493 // want to transfer its values up to sync. Otherwise, we remove it and
2494 // allow the entry from Sync to overtake it in the model.
2495 const std::string guid
= conflicting_turl
->sync_guid();
2496 if (IsLocalTemplateURLBetter(conflicting_turl
, sync_turl
)) {
2497 ResetTemplateURLGUID(conflicting_turl
, sync_turl
->sync_guid());
2498 syncer::SyncData sync_data
=
2499 CreateSyncDataFromTemplateURL(*conflicting_turl
);
2500 change_list
->push_back(syncer::SyncChange(
2501 FROM_HERE
, syncer::SyncChange::ACTION_UPDATE
, sync_data
));
2502 if (conflicting_turl
== GetDefaultSearchProvider() &&
2503 !pending_synced_default_search_
) {
2504 // If we're not waiting for the Synced default to come in, we should
2505 // override the pref with our new GUID. If we are waiting for the
2506 // arrival of a synced default, setting the pref here would cause us
2507 // to lose the GUID we are waiting on.
2508 PrefService
* prefs
= GetPrefs();
2510 prefs
->SetString(prefs::kSyncedDefaultSearchProviderGUID
,
2511 conflicting_turl
->sync_guid());
2514 // Note that in this case we do not add the Sync TemplateURL to the
2515 // local model, since we've effectively "merged" it in by updating the
2516 // local conflicting entry with its sync_guid.
2517 should_add_sync_turl
= false;
2518 merge_result
->set_num_items_modified(
2519 merge_result
->num_items_modified() + 1);
2521 // We guarantee that this isn't the local search provider. Otherwise,
2522 // local would have won.
2523 DCHECK(conflicting_turl
!= GetDefaultSearchProvider());
2524 Remove(conflicting_turl
);
2525 merge_result
->set_num_items_deleted(
2526 merge_result
->num_items_deleted() + 1);
2528 // This TemplateURL was either removed or overwritten in the local model.
2529 // Remove the entry from the local data so it isn't pushed up to Sync.
2530 local_data
->erase(guid
);
2534 if (should_add_sync_turl
) {
2535 // Force the local ID to kInvalidTemplateURLID so we can add it.
2536 TemplateURLData
data(sync_turl
->data());
2537 data
.id
= kInvalidTemplateURLID
;
2538 Add(new TemplateURL(profile_
, data
));
2539 merge_result
->set_num_items_added(
2540 merge_result
->num_items_added() + 1);
2544 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced(
2545 const std::string
& guid
) {
2546 // If we're not syncing or if default search is managed by policy, ignore.
2547 if (!sync_processor_
.get() || is_default_search_managed_
)
2550 PrefService
* prefs
= GetPrefs();
2551 if (prefs
&& pending_synced_default_search_
&&
2552 prefs
->GetString(prefs::kSyncedDefaultSearchProviderGUID
) == guid
) {
2553 // Make sure this actually exists. We should not be calling this unless we
2554 // really just added this TemplateURL.
2555 TemplateURL
* turl_from_sync
= GetTemplateURLForGUID(guid
);
2556 if (turl_from_sync
&& turl_from_sync
->SupportsReplacement()) {
2557 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(
2558 &dsp_change_origin_
, DSP_CHANGE_SYNC_ADD
);
2559 SetDefaultSearchProvider(turl_from_sync
);
2561 pending_synced_default_search_
= false;
2565 TemplateURL
* TemplateURLService::GetPendingSyncedDefaultSearchProvider() {
2566 PrefService
* prefs
= GetPrefs();
2567 if (!prefs
|| !pending_synced_default_search_
)
2570 // Could be NULL if no such thing exists.
2571 return GetTemplateURLForGUID(
2572 prefs
->GetString(prefs::kSyncedDefaultSearchProviderGUID
));
2575 void TemplateURLService::PatchMissingSyncGUIDs(
2576 TemplateURLVector
* template_urls
) {
2577 DCHECK(template_urls
);
2578 for (TemplateURLVector::iterator i
= template_urls
->begin();
2579 i
!= template_urls
->end(); ++i
) {
2580 TemplateURL
* template_url
= *i
;
2581 DCHECK(template_url
);
2582 if (template_url
->sync_guid().empty() &&
2583 (template_url
->GetType() !=
2584 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
)) {
2585 template_url
->data_
.sync_guid
= base::GenerateGUID();
2587 service_
->UpdateKeyword(template_url
->data());
2592 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine(
2593 TemplateURLVector
* template_urls
,
2594 TemplateURL
* default_search_provider
) {
2595 DCHECK(template_urls
);
2596 is_default_search_managed_
= false;
2597 bool database_specified_a_default
= (default_search_provider
!= NULL
);
2599 // Check if default search provider is now managed.
2600 scoped_ptr
<TemplateURL
> default_from_prefs
;
2601 LoadDefaultSearchProviderFromPrefs(&default_from_prefs
,
2602 &is_default_search_managed_
);
2604 // Remove entries that were created because of policy as they may have
2605 // changed since the database was saved.
2606 RemoveProvidersCreatedByPolicy(template_urls
,
2607 &default_search_provider
,
2608 default_from_prefs
.get());
2610 PatchMissingSyncGUIDs(template_urls
);
2612 if (is_default_search_managed_
) {
2613 SetTemplateURLs(template_urls
);
2615 if (TemplateURLsHaveSamePrefs(default_search_provider
,
2616 default_from_prefs
.get())) {
2617 // The value from the preferences was previously stored in the database.
2620 // The value from the preferences takes over.
2621 default_search_provider
= NULL
;
2622 if (default_from_prefs
) {
2623 TemplateURLData
data(default_from_prefs
->data());
2624 data
.created_by_policy
= true;
2625 data
.id
= kInvalidTemplateURLID
;
2626 default_search_provider
= new TemplateURL(profile_
, data
);
2627 if (!AddNoNotify(default_search_provider
, true))
2628 default_search_provider
= NULL
;
2631 // Note that this saves the default search provider to prefs.
2632 if (!default_search_provider
||
2633 ((default_search_provider
->GetType() !=
2634 TemplateURL::OMNIBOX_API_EXTENSION
) &&
2635 default_search_provider
->SupportsReplacement())) {
2636 bool success
= SetDefaultSearchProviderNoNotify(default_search_provider
);
2640 // If we had a managed default, replace it with the synced default if
2641 // applicable, or the first provider of the list.
2642 TemplateURL
* synced_default
= GetPendingSyncedDefaultSearchProvider();
2643 if (synced_default
) {
2644 default_search_provider
= synced_default
;
2645 pending_synced_default_search_
= false;
2646 } else if (database_specified_a_default
&&
2647 default_search_provider
== NULL
) {
2648 UMA_HISTOGRAM_ENUMERATION(kFirstPotentialEngineHistogramName
,
2649 FIRST_POTENTIAL_CALLSITE_ON_LOAD
,
2650 FIRST_POTENTIAL_CALLSITE_MAX
);
2651 default_search_provider
= FirstPotentialDefaultEngine(*template_urls
);
2654 // If the default search provider existed previously, then just
2655 // set the member variable. Otherwise, we'll set it using the method
2656 // to ensure that it is saved properly after its id is set.
2657 if (default_search_provider
&&
2658 (default_search_provider
->id() != kInvalidTemplateURLID
)) {
2659 default_search_provider_
= default_search_provider
;
2660 default_search_provider
= NULL
;
2662 SetTemplateURLs(template_urls
);
2664 if (default_search_provider
) {
2665 base::AutoReset
<DefaultSearchChangeOrigin
> change_origin(
2666 &dsp_change_origin_
, default_from_prefs
?
2667 dsp_change_origin_
: DSP_CHANGE_NEW_ENGINE_NO_PREFS
);
2668 // Note that this saves the default search provider to prefs.
2669 SetDefaultSearchProvider(default_search_provider
);
2671 // Always save the default search provider to prefs. That way we don't
2672 // have to worry about it being out of sync.
2673 if (default_search_provider_
)
2674 SaveDefaultSearchProviderToPrefs(default_search_provider_
);
2679 void TemplateURLService::EnsureDefaultSearchProviderExists() {
2680 if (!is_default_search_managed()) {
2681 bool has_default_search_provider
= default_search_provider_
&&
2682 default_search_provider_
->SupportsReplacement();
2683 UMA_HISTOGRAM_BOOLEAN("Search.HasDefaultSearchProvider",
2684 has_default_search_provider
);
2685 // Ensure that default search provider exists. See http://crbug.com/116952.
2686 if (!has_default_search_provider
) {
2688 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider());
2691 // Don't log anything if the user has a NULL default search provider.
2692 if (default_search_provider_
) {
2693 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchProviderType",
2694 TemplateURLPrepopulateData::GetEngineType(*default_search_provider_
),
2700 TemplateURL
* TemplateURLService::CreateTemplateURLForExtension(
2701 const ExtensionKeyword
& extension_keyword
) const {
2702 TemplateURLData data
;
2703 data
.short_name
= base::UTF8ToUTF16(extension_keyword
.extension_name
);
2704 data
.SetKeyword(base::UTF8ToUTF16(extension_keyword
.extension_keyword
));
2705 // This URL is not actually used for navigation. It holds the extension's
2706 // ID, as well as forcing the TemplateURL to be treated as a search keyword.
2707 data
.SetURL(std::string(extensions::kExtensionScheme
) + "://" +
2708 extension_keyword
.extension_id
+ "/?q={searchTerms}");
2709 return new TemplateURL(profile_
, data
);
2712 TemplateURL
* TemplateURLService::FindTemplateURLForExtension(
2713 const std::string
& extension_id
,
2714 TemplateURL::Type type
) const {
2715 DCHECK_NE(TemplateURL::NORMAL
, type
);
2716 for (TemplateURLVector::const_iterator i
= template_urls_
.begin();
2717 i
!= template_urls_
.end(); ++i
) {
2718 if ((*i
)->GetType() == type
&&
2719 (*i
)->GetExtensionId() == extension_id
)
2726 TemplateURL
* TemplateURLService::FindExtensionDefaultSearchEngine() const {
2727 TemplateURL
* most_recently_intalled_default
= NULL
;
2728 for (TemplateURLVector::const_iterator i
= template_urls_
.begin();
2729 i
!= template_urls_
.end(); ++i
) {
2730 if (((*i
)->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
) &&
2731 (*i
)->extension_info_
->wants_to_be_default_engine
&&
2732 (*i
)->SupportsReplacement() &&
2733 (!most_recently_intalled_default
||
2734 (most_recently_intalled_default
->extension_info_
->install_time
<
2735 (*i
)->extension_info_
->install_time
)))
2736 most_recently_intalled_default
= *i
;
2739 return most_recently_intalled_default
;
2742 void TemplateURLService::
2743 SetDefaultSearchProviderAfterRemovingDefaultExtension() {
2744 DCHECK(!is_default_search_managed());
2745 TemplateURL
* new_dse
= FindExtensionDefaultSearchEngine();
2747 scoped_ptr
<TemplateURL
> default_provider
;
2749 if (LoadDefaultSearchProviderFromPrefs(&default_provider
, &is_managed
) &&
2751 for (TemplateURLVector::const_iterator i
= template_urls_
.begin();
2752 i
!= template_urls_
.end(); ++i
) {
2753 if ((*i
)->id() == default_provider
->id()) {
2761 new_dse
= FindNewDefaultSearchProvider();
2762 SetDefaultSearchProviderNoNotify(new_dse
);