Home | History | Annotate | Download | only in search_engines
      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.
      4 
      5 #include "chrome/browser/search_engines/template_url_service.h"
      6 
      7 #include <algorithm>
      8 #include <utility>
      9 
     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"
     54 
     55 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
     56 typedef TemplateURLService::SyncDataMap SyncDataMap;
     57 
     58 namespace {
     59 
     60 bool TemplateURLsHaveSamePrefs(const TemplateURL* url1,
     61                                const TemplateURL* url2) {
     62   if (url1 == url2)
     63     return true;
     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->search_url_post_params() == url2->search_url_post_params()) &&
     72       (url1->suggestions_url_post_params() ==
     73           url2->suggestions_url_post_params()) &&
     74       (url1->instant_url_post_params() == url2->instant_url_post_params()) &&
     75       (url1->image_url_post_params() == url2->image_url_post_params()) &&
     76       (url1->image_url() == url2->image_url()) &&
     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());
     84 }
     85 
     86 const char kFirstPotentialEngineHistogramName[] =
     87     "Search.FirstPotentialEngineCalled";
     88 
     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,
     98 };
     99 
    100 const char kDeleteSyncedEngineHistogramName[] =
    101     "Search.DeleteSyncedSearchEngine";
    102 
    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,
    109   DELETE_ENGINE_MAX,
    110 };
    111 
    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       DCHECK(!(*i)->IsExtensionKeyword());
    118       return *i;
    119     }
    120   }
    121   return NULL;
    122 }
    123 
    124 // Returns true iff the change in |change_list| at index |i| should not be sent
    125 // up to the server based on its GUIDs presence in |sync_data| or when compared
    126 // to changes after it in |change_list|.
    127 // The criteria is:
    128 //  1) It is an ACTION_UPDATE or ACTION_DELETE and the sync_guid associated
    129 //     with it is NOT found in |sync_data|. We can only update and remove
    130 //     entries that were originally from the Sync server.
    131 //  2) It is an ACTION_ADD and the sync_guid associated with it is found in
    132 //     |sync_data|. We cannot re-add entries that Sync already knew about.
    133 //  3) There is an update after an update for the same GUID. We prune earlier
    134 //     ones just to save bandwidth (Sync would normally coalesce them).
    135 bool ShouldRemoveSyncChange(size_t index,
    136                             syncer::SyncChangeList* change_list,
    137                             const SyncDataMap* sync_data) {
    138   DCHECK(index < change_list->size());
    139   const syncer::SyncChange& change_i = (*change_list)[index];
    140   const std::string guid = change_i.sync_data().GetSpecifics()
    141       .search_engine().sync_guid();
    142   syncer::SyncChange::SyncChangeType type = change_i.change_type();
    143   if ((type == syncer::SyncChange::ACTION_UPDATE ||
    144        type == syncer::SyncChange::ACTION_DELETE) &&
    145        sync_data->find(guid) == sync_data->end())
    146     return true;
    147   if (type == syncer::SyncChange::ACTION_ADD &&
    148       sync_data->find(guid) != sync_data->end())
    149     return true;
    150   if (type == syncer::SyncChange::ACTION_UPDATE) {
    151     for (size_t j = index + 1; j < change_list->size(); j++) {
    152       const syncer::SyncChange& change_j = (*change_list)[j];
    153       if ((syncer::SyncChange::ACTION_UPDATE == change_j.change_type()) &&
    154           (change_j.sync_data().GetSpecifics().search_engine().sync_guid() ==
    155               guid))
    156         return true;
    157     }
    158   }
    159   return false;
    160 }
    161 
    162 // Remove SyncChanges that should not be sent to the server from |change_list|.
    163 // This is done to eliminate incorrect SyncChanges added by the merge and
    164 // conflict resolution logic when it is unsure of whether or not an entry is new
    165 // from Sync or originally from the local model. This also removes changes that
    166 // would be otherwise be coalesced by Sync in order to save bandwidth.
    167 void PruneSyncChanges(const SyncDataMap* sync_data,
    168                       syncer::SyncChangeList* change_list) {
    169   for (size_t i = 0; i < change_list->size(); ) {
    170     if (ShouldRemoveSyncChange(i, change_list, sync_data))
    171       change_list->erase(change_list->begin() + i);
    172     else
    173       ++i;
    174   }
    175 }
    176 
    177 // Helper class that reports a specific string as the Google base URL.  We use
    178 // this so that code can calculate a TemplateURL's previous search URL after a
    179 // change to the global base URL.
    180 class OldBaseURLSearchTermsData : public UIThreadSearchTermsData {
    181  public:
    182   OldBaseURLSearchTermsData(Profile* profile, const std::string& old_base_url);
    183   virtual ~OldBaseURLSearchTermsData();
    184 
    185   virtual std::string GoogleBaseURLValue() const OVERRIDE;
    186 
    187  private:
    188   std::string old_base_url;
    189 };
    190 
    191 OldBaseURLSearchTermsData::OldBaseURLSearchTermsData(
    192     Profile* profile,
    193     const std::string& old_base_url)
    194     : UIThreadSearchTermsData(profile),
    195       old_base_url(old_base_url) {
    196 }
    197 
    198 OldBaseURLSearchTermsData::~OldBaseURLSearchTermsData() {
    199 }
    200 
    201 std::string OldBaseURLSearchTermsData::GoogleBaseURLValue() const {
    202   return old_base_url;
    203 }
    204 
    205 // Returns true if |turl|'s GUID is not found inside |sync_data|. This is to be
    206 // used in MergeDataAndStartSyncing to differentiate between TemplateURLs from
    207 // Sync and TemplateURLs that were initially local, assuming |sync_data| is the
    208 // |initial_sync_data| parameter.
    209 bool IsFromSync(const TemplateURL* turl, const SyncDataMap& sync_data) {
    210   return !!sync_data.count(turl->sync_guid());
    211 }
    212 
    213 // Log the number of instances of a keyword that exist, with zero or more
    214 // underscores, which could occur as the result of conflict resolution.
    215 void LogDuplicatesHistogram(
    216     const TemplateURLService::TemplateURLVector& template_urls) {
    217   std::map<std::string, int> duplicates;
    218   for (TemplateURLService::TemplateURLVector::const_iterator it =
    219       template_urls.begin(); it != template_urls.end(); ++it) {
    220     std::string keyword = UTF16ToASCII((*it)->keyword());
    221     TrimString(keyword, "_", &keyword);
    222     duplicates[keyword]++;
    223   }
    224 
    225   // Count the keywords with duplicates.
    226   int num_dupes = 0;
    227   for (std::map<std::string, int>::const_iterator it = duplicates.begin();
    228       it != duplicates.end(); ++it) {
    229     if (it->second > 1)
    230       num_dupes++;
    231   }
    232 
    233   UMA_HISTOGRAM_COUNTS_100("Search.SearchEngineDuplicateCounts", num_dupes);
    234 }
    235 
    236 typedef std::vector<TemplateURLService::ExtensionKeyword> ExtensionKeywords;
    237 
    238 #if !defined(OS_ANDROID)
    239 // Extract all installed Omnibox Extensions.
    240 ExtensionKeywords GetExtensionKeywords(Profile* profile) {
    241   DCHECK(profile);
    242   ExtensionService* extension_service = profile->GetExtensionService();
    243   DCHECK(extension_service);
    244   const ExtensionSet* extensions = extension_service->extensions();
    245   ExtensionKeywords extension_keywords;
    246   for (ExtensionSet::const_iterator it = extensions->begin();
    247        it != extensions->end(); ++it) {
    248     const std::string& keyword = extensions::OmniboxInfo::GetKeyword(*it);
    249     if (!keyword.empty()) {
    250       extension_keywords.push_back(TemplateURLService::ExtensionKeyword(
    251           (*it)->id(), (*it)->name(), keyword));
    252     }
    253   }
    254   return extension_keywords;
    255 }
    256 #else
    257 // Extensions are not supported.
    258 ExtensionKeywords GetExtensionKeywords(Profile* profile) {
    259   return ExtensionKeywords();
    260 }
    261 #endif
    262 
    263 }  // namespace
    264 
    265 
    266 // TemplateURLService::ExtensionKeyword ---------------------------------------
    267 
    268 TemplateURLService::ExtensionKeyword::ExtensionKeyword(
    269     const std::string& id,
    270     const std::string& name,
    271     const std::string& keyword)
    272     : extension_id(id),
    273       extension_name(name),
    274       extension_keyword(keyword) {
    275 }
    276 
    277 TemplateURLService::ExtensionKeyword::~ExtensionKeyword() {}
    278 
    279 
    280 // TemplateURLService::LessWithPrefix -----------------------------------------
    281 
    282 class TemplateURLService::LessWithPrefix {
    283  public:
    284   // We want to find the set of keywords that begin with a prefix.  The STL
    285   // algorithms will return the set of elements that are "equal to" the
    286   // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))".  When
    287   // cmp() is the typical std::less<>, this results in lexicographic equality;
    288   // we need to extend this to mark a prefix as "not less than" a keyword it
    289   // begins, which will cause the desired elements to be considered "equal to"
    290   // the prefix.  Note: this is still a strict weak ordering, as required by
    291   // equal_range() (though I will not prove that here).
    292   //
    293   // Unfortunately the calling convention is not "prefix and element" but
    294   // rather "two elements", so we pass the prefix as a fake "element" which has
    295   // a NULL KeywordDataElement pointer.
    296   bool operator()(const KeywordToTemplateMap::value_type& elem1,
    297                   const KeywordToTemplateMap::value_type& elem2) const {
    298     return (elem1.second == NULL) ?
    299         (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) :
    300         (elem1.first < elem2.first);
    301   }
    302 };
    303 
    304 
    305 // TemplateURLService ---------------------------------------------------------
    306 
    307 TemplateURLService::TemplateURLService(Profile* profile)
    308     : provider_map_(new SearchHostToURLsMap),
    309       profile_(profile),
    310       loaded_(false),
    311       load_failed_(false),
    312       load_handle_(0),
    313       default_search_provider_(NULL),
    314       is_default_search_managed_(false),
    315       next_id_(kInvalidTemplateURLID + 1),
    316       time_provider_(&base::Time::Now),
    317       models_associated_(false),
    318       processing_syncer_changes_(false),
    319       pending_synced_default_search_(false),
    320       dsp_change_origin_(DSP_CHANGE_OTHER) {
    321   DCHECK(profile_);
    322   Init(NULL, 0);
    323 }
    324 
    325 TemplateURLService::TemplateURLService(const Initializer* initializers,
    326                                        const int count)
    327     : provider_map_(new SearchHostToURLsMap),
    328       profile_(NULL),
    329       loaded_(false),
    330       load_failed_(false),
    331       load_handle_(0),
    332       service_(NULL),
    333       default_search_provider_(NULL),
    334       is_default_search_managed_(false),
    335       next_id_(kInvalidTemplateURLID + 1),
    336       time_provider_(&base::Time::Now),
    337       models_associated_(false),
    338       processing_syncer_changes_(false),
    339       pending_synced_default_search_(false),
    340       dsp_change_origin_(DSP_CHANGE_OTHER) {
    341   Init(initializers, count);
    342 }
    343 
    344 TemplateURLService::~TemplateURLService() {
    345   if (service_.get())
    346     Shutdown();
    347   STLDeleteElements(&template_urls_);
    348 }
    349 
    350 // static
    351 string16 TemplateURLService::GenerateKeyword(const GURL& url) {
    352   DCHECK(url.is_valid());
    353   // Strip "www." off the front of the keyword; otherwise the keyword won't work
    354   // properly.  See http://code.google.com/p/chromium/issues/detail?id=6984 .
    355   // Special case: if the host was exactly "www." (not sure this can happen but
    356   // perhaps with some weird intranet and custom DNS server?), ensure we at
    357   // least don't return the empty string.
    358   string16 keyword(net::StripWWWFromHost(url));
    359   return keyword.empty() ? ASCIIToUTF16("www") : keyword;
    360 }
    361 
    362 // static
    363 string16 TemplateURLService::CleanUserInputKeyword(const string16& keyword) {
    364   // Remove the scheme.
    365   string16 result(base::i18n::ToLower(keyword));
    366   TrimWhitespace(result, TRIM_ALL, &result);
    367   url_parse::Component scheme_component;
    368   if (url_parse::ExtractScheme(UTF16ToUTF8(keyword).c_str(),
    369                                static_cast<int>(keyword.length()),
    370                                &scheme_component)) {
    371     // If the scheme isn't "http" or "https", bail.  The user isn't trying to
    372     // type a web address, but rather an FTP, file:, or other scheme URL, or a
    373     // search query with some sort of initial operator (e.g. "site:").
    374     if (result.compare(0, scheme_component.end(),
    375                        ASCIIToUTF16(chrome::kHttpScheme)) &&
    376         result.compare(0, scheme_component.end(),
    377                        ASCIIToUTF16(chrome::kHttpsScheme)))
    378       return string16();
    379 
    380     // Include trailing ':'.
    381     result.erase(0, scheme_component.end() + 1);
    382     // Many schemes usually have "//" after them, so strip it too.
    383     const string16 after_scheme(ASCIIToUTF16("//"));
    384     if (result.compare(0, after_scheme.length(), after_scheme) == 0)
    385       result.erase(0, after_scheme.length());
    386   }
    387 
    388   // Remove leading "www.".
    389   result = net::StripWWW(result);
    390 
    391   // Remove trailing "/".
    392   return (result.length() > 0 && result[result.length() - 1] == '/') ?
    393       result.substr(0, result.length() - 1) : result;
    394 }
    395 
    396 // static
    397 GURL TemplateURLService::GenerateSearchURL(TemplateURL* t_url) {
    398   DCHECK(t_url);
    399   UIThreadSearchTermsData search_terms_data(t_url->profile());
    400   return GenerateSearchURLUsingTermsData(t_url, search_terms_data);
    401 }
    402 
    403 // static
    404 GURL TemplateURLService::GenerateSearchURLUsingTermsData(
    405     const TemplateURL* t_url,
    406     const SearchTermsData& search_terms_data) {
    407   DCHECK(t_url);
    408 
    409   const TemplateURLRef& search_ref = t_url->url_ref();
    410   if (!search_ref.IsValidUsingTermsData(search_terms_data))
    411     return GURL();
    412 
    413   if (!search_ref.SupportsReplacementUsingTermsData(search_terms_data))
    414     return GURL(t_url->url());
    415 
    416   // Use something obscure for the search terms argument so that in the rare
    417   // case the term replaces the URL it's unlikely another keyword would have the
    418   // same url.
    419   // TODO(jnd): Add additional parameters to get post data when the search URL
    420   // has post parameters.
    421   return GURL(search_ref.ReplaceSearchTermsUsingTermsData(
    422       TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("blah.blah.blah.blah.blah")),
    423       search_terms_data, NULL));
    424 }
    425 
    426 bool TemplateURLService::CanReplaceKeyword(
    427     const string16& keyword,
    428     const GURL& url,
    429     TemplateURL** template_url_to_replace) {
    430   DCHECK(!keyword.empty());  // This should only be called for non-empty
    431                              // keywords. If we need to support empty kewords
    432                              // the code needs to change slightly.
    433   TemplateURL* existing_url = GetTemplateURLForKeyword(keyword);
    434   if (template_url_to_replace)
    435     *template_url_to_replace = existing_url;
    436   if (existing_url) {
    437     // We already have a TemplateURL for this keyword. Only allow it to be
    438     // replaced if the TemplateURL can be replaced.
    439     return CanReplace(existing_url);
    440   }
    441 
    442   // We don't have a TemplateURL with keyword. Only allow a new one if there
    443   // isn't a TemplateURL for the specified host, or there is one but it can
    444   // be replaced. We do this to ensure that if the user assigns a different
    445   // keyword to a generated TemplateURL, we won't regenerate another keyword for
    446   // the same host.
    447   return !url.is_valid() || url.host().empty() ||
    448       CanReplaceKeywordForHost(url.host(), template_url_to_replace);
    449 }
    450 
    451 void TemplateURLService::FindMatchingKeywords(
    452     const string16& prefix,
    453     bool support_replacement_only,
    454     TemplateURLVector* matches) const {
    455   // Sanity check args.
    456   if (prefix.empty())
    457     return;
    458   DCHECK(matches != NULL);
    459   DCHECK(matches->empty());  // The code for exact matches assumes this.
    460 
    461   // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
    462   TemplateURL* const kNullTemplateURL = NULL;
    463 
    464   // Find matching keyword range.  Searches the element map for keywords
    465   // beginning with |prefix| and stores the endpoints of the resulting set in
    466   // |match_range|.
    467   const std::pair<KeywordToTemplateMap::const_iterator,
    468                   KeywordToTemplateMap::const_iterator> match_range(
    469       std::equal_range(
    470           keyword_to_template_map_.begin(), keyword_to_template_map_.end(),
    471           KeywordToTemplateMap::value_type(prefix, kNullTemplateURL),
    472           LessWithPrefix()));
    473 
    474   // Return vector of matching keywords.
    475   for (KeywordToTemplateMap::const_iterator i(match_range.first);
    476        i != match_range.second; ++i) {
    477     if (!support_replacement_only || i->second->url_ref().SupportsReplacement())
    478       matches->push_back(i->second);
    479   }
    480 }
    481 
    482 TemplateURL* TemplateURLService::GetTemplateURLForKeyword(
    483     const string16& keyword) {
    484   KeywordToTemplateMap::const_iterator elem(
    485       keyword_to_template_map_.find(keyword));
    486   if (elem != keyword_to_template_map_.end())
    487     return elem->second;
    488   return ((!loaded_ || load_failed_) &&
    489       initial_default_search_provider_.get() &&
    490       (initial_default_search_provider_->keyword() == keyword)) ?
    491       initial_default_search_provider_.get() : NULL;
    492 }
    493 
    494 TemplateURL* TemplateURLService::GetTemplateURLForGUID(
    495     const std::string& sync_guid) {
    496   GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid));
    497   if (elem != guid_to_template_map_.end())
    498     return elem->second;
    499   return ((!loaded_ || load_failed_) &&
    500       initial_default_search_provider_.get() &&
    501       (initial_default_search_provider_->sync_guid() == sync_guid)) ?
    502       initial_default_search_provider_.get() : NULL;
    503 }
    504 
    505 TemplateURL* TemplateURLService::GetTemplateURLForHost(
    506     const std::string& host) {
    507   if (loaded_) {
    508     TemplateURL* t_url = provider_map_->GetTemplateURLForHost(host);
    509     if (t_url)
    510       return t_url;
    511   }
    512   return ((!loaded_ || load_failed_) &&
    513       initial_default_search_provider_.get() &&
    514       (GenerateSearchURL(initial_default_search_provider_.get()).host() ==
    515           host)) ? initial_default_search_provider_.get() : NULL;
    516 }
    517 
    518 void TemplateURLService::Add(TemplateURL* template_url) {
    519   if (AddNoNotify(template_url, true))
    520     NotifyObservers();
    521 }
    522 
    523 void TemplateURLService::AddAndSetProfile(TemplateURL* template_url,
    524                                           Profile* profile) {
    525   template_url->profile_ = profile;
    526   Add(template_url);
    527 }
    528 
    529 void TemplateURLService::AddWithOverrides(TemplateURL* template_url,
    530                                           const string16& short_name,
    531                                           const string16& keyword,
    532                                           const std::string& url) {
    533   DCHECK(!keyword.empty());
    534   DCHECK(!url.empty());
    535   template_url->data_.short_name = short_name;
    536   template_url->data_.SetKeyword(keyword);
    537   template_url->SetURL(url);
    538   Add(template_url);
    539 }
    540 
    541 void TemplateURLService::Remove(TemplateURL* template_url) {
    542   RemoveNoNotify(template_url);
    543   NotifyObservers();
    544 }
    545 
    546 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) {
    547   RemoveAutoGeneratedBetween(created_after, base::Time());
    548 }
    549 
    550 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after,
    551                                                     base::Time created_before) {
    552   RemoveAutoGeneratedForOriginBetween(GURL(), created_after, created_before);
    553 }
    554 
    555 void TemplateURLService::RemoveAutoGeneratedForOriginBetween(
    556     const GURL& origin,
    557     base::Time created_after,
    558     base::Time created_before) {
    559   GURL o(origin.GetOrigin());
    560   bool should_notify = false;
    561   for (size_t i = 0; i < template_urls_.size();) {
    562     if (template_urls_[i]->date_created() >= created_after &&
    563         (created_before.is_null() ||
    564          template_urls_[i]->date_created() < created_before) &&
    565         CanReplace(template_urls_[i]) &&
    566         (o.is_empty() ||
    567          GenerateSearchURL(template_urls_[i]).GetOrigin() == o)) {
    568       RemoveNoNotify(template_urls_[i]);
    569       should_notify = true;
    570     } else {
    571       ++i;
    572     }
    573   }
    574   if (should_notify)
    575     NotifyObservers();
    576 }
    577 
    578 
    579 void TemplateURLService::RegisterExtensionKeyword(
    580     const std::string& extension_id,
    581     const std::string& extension_name,
    582     const std::string& keyword) {
    583   DCHECK(loaded_);
    584 
    585   if (!GetTemplateURLForExtension(extension_id)) {
    586     ExtensionKeyword extension_url(extension_id, extension_name, keyword);
    587     Add(CreateTemplateURLForExtension(extension_url));
    588   }
    589 }
    590 
    591 void TemplateURLService::UnregisterExtensionKeyword(
    592     const std::string& extension_id) {
    593   DCHECK(loaded_);
    594   TemplateURL* url = GetTemplateURLForExtension(extension_id);
    595   if (url)
    596     Remove(url);
    597 }
    598 
    599 TemplateURL* TemplateURLService::GetTemplateURLForExtension(
    600     const std::string& extension_id) {
    601   for (TemplateURLVector::const_iterator i = template_urls_.begin();
    602        i != template_urls_.end(); ++i) {
    603     if ((*i)->IsExtensionKeyword() &&
    604         ((*i)->url_ref().GetHost() == extension_id))
    605       return *i;
    606   }
    607 
    608   return NULL;
    609 }
    610 
    611 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() {
    612   return template_urls_;
    613 }
    614 
    615 void TemplateURLService::IncrementUsageCount(TemplateURL* url) {
    616   DCHECK(url);
    617   if (std::find(template_urls_.begin(), template_urls_.end(), url) ==
    618       template_urls_.end())
    619     return;
    620   ++url->data_.usage_count;
    621   if (service_.get())
    622     service_.get()->UpdateKeyword(url->data());
    623 }
    624 
    625 void TemplateURLService::ResetTemplateURL(TemplateURL* url,
    626                                           const string16& title,
    627                                           const string16& keyword,
    628                                           const std::string& search_url) {
    629   if (!loaded_)
    630     return;
    631   DCHECK(!keyword.empty());
    632   DCHECK(!search_url.empty());
    633   TemplateURLData data(url->data());
    634   data.short_name = title;
    635   data.SetKeyword(keyword);
    636   if (search_url != data.url()) {
    637     data.SetURL(search_url);
    638     // The urls have changed, reset the favicon url.
    639     data.favicon_url = GURL();
    640   }
    641   data.safe_for_autoreplace = false;
    642   data.last_modified = time_provider_();
    643   TemplateURL new_url(url->profile(), data);
    644   UIThreadSearchTermsData search_terms_data(url->profile());
    645   if (UpdateNoNotify(url, new_url, search_terms_data))
    646     NotifyObservers();
    647 }
    648 
    649 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) {
    650   return url != GetDefaultSearchProvider() &&
    651       url->url_ref().SupportsReplacement() && !is_default_search_managed();
    652 }
    653 
    654 void TemplateURLService::SetDefaultSearchProvider(TemplateURL* url) {
    655   DCHECK(!is_default_search_managed_);
    656   // Extension keywords cannot be made default, as they are inherently async.
    657   DCHECK(!url || !url->IsExtensionKeyword());
    658 
    659   // Always persist the setting in the database, that way if the backup
    660   // signature has changed out from under us it gets reset correctly.
    661   if (SetDefaultSearchProviderNoNotify(url))
    662     NotifyObservers();
    663 }
    664 
    665 TemplateURL* TemplateURLService::GetDefaultSearchProvider() {
    666   if (loaded_ && !load_failed_)
    667     return default_search_provider_;
    668   // We're not loaded, rely on the default search provider stored in prefs.
    669   return initial_default_search_provider_.get();
    670 }
    671 
    672 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider(
    673     const GURL& url) {
    674   TemplateURL* default_provider = GetDefaultSearchProvider();
    675   return default_provider && default_provider->IsSearchURL(url);
    676 }
    677 
    678 TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() {
    679   // See if the prepopulated default still exists.
    680   scoped_ptr<TemplateURL> prepopulated_default(
    681       TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_));
    682   for (TemplateURLVector::iterator i = template_urls_.begin();
    683        i != template_urls_.end(); ++i) {
    684     if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id())
    685       return *i;
    686   }
    687   // If not, use the first non-extension keyword of the templates that supports
    688   // search term replacement.
    689   if (processing_syncer_changes_) {
    690     UMA_HISTOGRAM_ENUMERATION(
    691         kFirstPotentialEngineHistogramName,
    692         FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_PROCESSING_SYNC_CHANGES,
    693         FIRST_POTENTIAL_CALLSITE_MAX);
    694   } else {
    695     if (sync_processor_.get()) {
    696       // We're not currently in a sync cycle, but we're syncing.
    697       UMA_HISTOGRAM_ENUMERATION(kFirstPotentialEngineHistogramName,
    698                                 FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_SYNCING,
    699                                 FIRST_POTENTIAL_CALLSITE_MAX);
    700     } else {
    701       // We're not syncing at all.
    702       UMA_HISTOGRAM_ENUMERATION(
    703           kFirstPotentialEngineHistogramName,
    704           FIRST_POTENTIAL_CALLSITE_FIND_NEW_DSP_NOT_SYNCING,
    705           FIRST_POTENTIAL_CALLSITE_MAX);
    706     }
    707   }
    708   return FirstPotentialDefaultEngine(template_urls_);
    709 }
    710 
    711 void TemplateURLService::ResetURLs() {
    712   // Can't clean DB if it hasn't been loaded.
    713   DCHECK(loaded());
    714   DCHECK(service_);
    715   ClearDefaultProviderFromPrefs();
    716 
    717   TemplateURLVector entries_to_process = template_urls_;
    718   // Clear default provider to be able to delete it.
    719   default_search_provider_ = NULL;
    720   for (TemplateURLVector::const_iterator i = entries_to_process.begin();
    721        i != entries_to_process.end(); ++i)
    722     RemoveNoNotify(*i);
    723 
    724   entries_to_process.clear();
    725   provider_map_.reset(new SearchHostToURLsMap);
    726   UIThreadSearchTermsData search_terms_data(profile_);
    727   provider_map_->Init(TemplateURLVector(), search_terms_data);
    728 
    729   TemplateURL* default_search_provider = NULL;
    730   // Force GetSearchProvidersUsingLoadedEngines() to include the prepopulated
    731   // engines in the list by claiming we are currently on version 0, ensuring
    732   // that the prepopulate data version will be newer.
    733   int new_resource_keyword_version = 0;
    734   GetSearchProvidersUsingLoadedEngines(service_.get(), profile_,
    735                                        &entries_to_process,
    736                                        &default_search_provider,
    737                                        &new_resource_keyword_version,
    738                                        &pre_sync_deletes_);
    739   // Setup search engines and a default one.
    740   base::AutoReset<DefaultSearchChangeOrigin> change_origin(
    741       &dsp_change_origin_, DSP_CHANGE_PROFILE_RESET);
    742   AddTemplateURLsAndSetupDefaultEngine(&entries_to_process,
    743                                        default_search_provider);
    744 
    745   // Repopulate extension keywords.
    746   std::vector<ExtensionKeyword> extension_keywords =
    747       GetExtensionKeywords(profile());
    748   for (size_t i = 0; i < extension_keywords.size(); ++i) {
    749     TemplateURL* extension_url =
    750         CreateTemplateURLForExtension(extension_keywords[i]);
    751     AddNoNotify(extension_url, true);
    752   }
    753 
    754   if (new_resource_keyword_version)
    755     service_->SetBuiltinKeywordVersion(new_resource_keyword_version);
    756 
    757   EnsureDefaultSearchProviderExists();
    758   NotifyObservers();
    759 }
    760 
    761 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) {
    762   model_observers_.AddObserver(observer);
    763 }
    764 
    765 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) {
    766   model_observers_.RemoveObserver(observer);
    767 }
    768 
    769 void TemplateURLService::Load() {
    770   if (loaded_ || load_handle_)
    771     return;
    772 
    773   if (!service_.get()) {
    774     service_ = WebDataService::FromBrowserContext(profile_);
    775   }
    776 
    777   if (service_.get()) {
    778     load_handle_ = service_->GetKeywords(this);
    779   } else {
    780     ChangeToLoadedState();
    781     NotifyLoaded();
    782   }
    783 }
    784 
    785 void TemplateURLService::OnWebDataServiceRequestDone(
    786     WebDataService::Handle h,
    787     const WDTypedResult* result) {
    788   // Reset the load_handle so that we don't try and cancel the load in
    789   // the destructor.
    790   load_handle_ = 0;
    791 
    792   if (!result) {
    793     // Results are null if the database went away or (most likely) wasn't
    794     // loaded.
    795     load_failed_ = true;
    796     ChangeToLoadedState();
    797     NotifyLoaded();
    798     return;
    799   }
    800 
    801   // initial_default_search_provider_ is only needed before we've finished
    802   // loading. Now that we've loaded we can nuke it.
    803   initial_default_search_provider_.reset();
    804 
    805   TemplateURLVector template_urls;
    806   TemplateURL* default_search_provider = NULL;
    807   int new_resource_keyword_version = 0;
    808   GetSearchProvidersUsingKeywordResult(*result, service_.get(), profile_,
    809       &template_urls, &default_search_provider, &new_resource_keyword_version,
    810       &pre_sync_deletes_);
    811 
    812   AddTemplateURLsAndSetupDefaultEngine(&template_urls, default_search_provider);
    813 
    814   // This initializes provider_map_ which should be done before
    815   // calling UpdateKeywordSearchTermsForURL.
    816   ChangeToLoadedState();
    817 
    818   // Index any visits that occurred before we finished loading.
    819   for (size_t i = 0; i < visits_to_add_.size(); ++i)
    820     UpdateKeywordSearchTermsForURL(visits_to_add_[i]);
    821   visits_to_add_.clear();
    822 
    823   if (new_resource_keyword_version)
    824     service_->SetBuiltinKeywordVersion(new_resource_keyword_version);
    825 
    826   EnsureDefaultSearchProviderExists();
    827 
    828   NotifyObservers();
    829   NotifyLoaded();
    830 }
    831 
    832 string16 TemplateURLService::GetKeywordShortName(const string16& keyword,
    833                                                  bool* is_extension_keyword) {
    834   const TemplateURL* template_url = GetTemplateURLForKeyword(keyword);
    835 
    836   // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService
    837   // to track changes to the model, this should become a DCHECK.
    838   if (template_url) {
    839     *is_extension_keyword = template_url->IsExtensionKeyword();
    840     return template_url->AdjustedShortNameForLocaleDirection();
    841   }
    842   *is_extension_keyword = false;
    843   return string16();
    844 }
    845 
    846 void TemplateURLService::Observe(int type,
    847                                  const content::NotificationSource& source,
    848                                  const content::NotificationDetails& details) {
    849   if (type == chrome::NOTIFICATION_HISTORY_URL_VISITED) {
    850     content::Details<history::URLVisitedDetails> visit_details(details);
    851     if (!loaded_)
    852       visits_to_add_.push_back(*visit_details.ptr());
    853     else
    854       UpdateKeywordSearchTermsForURL(*visit_details.ptr());
    855   } else if (type == chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED) {
    856     // Policy has been updated, so the default search prefs may be different.
    857     // Reload the default search provider from them.
    858     // TODO(pkasting): Rather than communicating via prefs, we should eventually
    859     // observe policy changes directly.
    860     UpdateDefaultSearch();
    861   } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) {
    862     if (loaded_) {
    863       GoogleBaseURLChanged(
    864           content::Details<GoogleURLTracker::UpdatedDetails>(details)->first);
    865     }
    866   } else {
    867     NOTREACHED();
    868   }
    869 }
    870 
    871 void TemplateURLService::Shutdown() {
    872   // This check has to be done at Shutdown() instead of in the dtor to ensure
    873   // that no clients of WebDataService are holding ptrs to it after the first
    874   // phase of the BrowserContextKeyedService Shutdown() process.
    875   if (load_handle_) {
    876     DCHECK(service_.get());
    877     service_->CancelRequest(load_handle_);
    878   }
    879   service_ = NULL;
    880 }
    881 
    882 void TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged() {
    883   // Listen for changes to the default search from Sync.
    884   PrefService* prefs = GetPrefs();
    885   TemplateURL* new_default_search = GetTemplateURLForGUID(
    886       prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
    887   if (new_default_search && !is_default_search_managed_) {
    888     if (new_default_search != GetDefaultSearchProvider()) {
    889       base::AutoReset<DefaultSearchChangeOrigin> change_origin(
    890           &dsp_change_origin_, DSP_CHANGE_SYNC_PREF);
    891       SetDefaultSearchProvider(new_default_search);
    892       pending_synced_default_search_ = false;
    893     }
    894   } else {
    895     // If it's not there, or if default search is currently managed, set a
    896     // flag to indicate that we waiting on the search engine entry to come
    897     // in through Sync.
    898     pending_synced_default_search_ = true;
    899   }
    900   UpdateDefaultSearch();
    901 }
    902 
    903 syncer::SyncDataList TemplateURLService::GetAllSyncData(
    904     syncer::ModelType type) const {
    905   DCHECK_EQ(syncer::SEARCH_ENGINES, type);
    906 
    907   syncer::SyncDataList current_data;
    908   for (TemplateURLVector::const_iterator iter = template_urls_.begin();
    909       iter != template_urls_.end(); ++iter) {
    910     // We don't sync keywords managed by policy.
    911     if ((*iter)->created_by_policy())
    912       continue;
    913     current_data.push_back(CreateSyncDataFromTemplateURL(**iter));
    914   }
    915 
    916   return current_data;
    917 }
    918 
    919 syncer::SyncError TemplateURLService::ProcessSyncChanges(
    920     const tracked_objects::Location& from_here,
    921     const syncer::SyncChangeList& change_list) {
    922   if (!models_associated_) {
    923     syncer::SyncError error(FROM_HERE,
    924                             syncer::SyncError::DATATYPE_ERROR,
    925                             "Models not yet associated.",
    926                             syncer::SEARCH_ENGINES);
    927     return error;
    928   }
    929   DCHECK(loaded_);
    930 
    931   base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
    932 
    933   // We've started syncing, so set our origin member to the base Sync value.
    934   // As we move through Sync Code, we may set this to increasingly specific
    935   // origins so we can tell what exactly caused a DSP change.
    936   base::AutoReset<DefaultSearchChangeOrigin> change_origin(&dsp_change_origin_,
    937       DSP_CHANGE_SYNC_UNINTENTIONAL);
    938 
    939   syncer::SyncChangeList new_changes;
    940   syncer::SyncError error;
    941   for (syncer::SyncChangeList::const_iterator iter = change_list.begin();
    942       iter != change_list.end(); ++iter) {
    943     DCHECK_EQ(syncer::SEARCH_ENGINES, iter->sync_data().GetDataType());
    944 
    945     std::string guid =
    946         iter->sync_data().GetSpecifics().search_engine().sync_guid();
    947     TemplateURL* existing_turl = GetTemplateURLForGUID(guid);
    948     scoped_ptr<TemplateURL> turl(CreateTemplateURLFromTemplateURLAndSyncData(
    949         profile_, existing_turl, iter->sync_data(), &new_changes));
    950     if (!turl.get())
    951       continue;
    952 
    953     // Explicitly don't check for conflicts against extension keywords; in this
    954     // case the functions which modify the keyword map know how to handle the
    955     // conflicts.
    956     // TODO(mpcomplete): If we allow editing extension keywords, then those will
    957     // need to undergo conflict resolution.
    958     TemplateURL* existing_keyword_turl =
    959         FindNonExtensionTemplateURLForKeyword(turl->keyword());
    960     if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
    961       if (!existing_turl) {
    962         NOTREACHED() << "Unexpected sync change state.";
    963         error = sync_error_factory_->CreateAndUploadError(
    964             FROM_HERE,
    965             "ProcessSyncChanges failed on ChangeType ACTION_DELETE");
    966         LOG(ERROR) << "Trying to delete a non-existent TemplateURL.";
    967         continue;
    968       }
    969       if (existing_turl == GetDefaultSearchProvider()) {
    970         // The only way Sync can attempt to delete the default search provider
    971         // is if we had changed the kSyncedDefaultSearchProviderGUID
    972         // preference, but perhaps it has not yet been received. To avoid
    973         // situations where this has come in erroneously, we will un-delete
    974         // the current default search from the Sync data. If the pref really
    975         // does arrive later, then default search will change to the correct
    976         // entry, but we'll have this extra entry sitting around. The result is
    977         // not ideal, but it prevents a far more severe bug where the default is
    978         // unexpectedly swapped to something else. The user can safely delete
    979         // the extra entry again later, if they choose. Most users who do not
    980         // look at the search engines UI will not notice this.
    981         // Note that we append a special character to the end of the keyword in
    982         // an attempt to avoid a ping-poinging situation where receiving clients
    983         // may try to continually delete the resurrected entry.
    984         string16 updated_keyword = UniquifyKeyword(*existing_turl, true);
    985         TemplateURLData data(existing_turl->data());
    986         data.SetKeyword(updated_keyword);
    987         TemplateURL new_turl(existing_turl->profile(), data);
    988         UIThreadSearchTermsData search_terms_data(existing_turl->profile());
    989         if (UpdateNoNotify(existing_turl, new_turl, search_terms_data))
    990           NotifyObservers();
    991 
    992         syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(new_turl);
    993         new_changes.push_back(syncer::SyncChange(FROM_HERE,
    994                                                  syncer::SyncChange::ACTION_ADD,
    995                                                  sync_data));
    996         // Ignore the delete attempt. This means we never end up reseting the
    997         // default search provider due to an ACTION_DELETE from sync.
    998         continue;
    999       }
   1000 
   1001       Remove(existing_turl);
   1002     } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) {
   1003       if (existing_turl) {
   1004         NOTREACHED() << "Unexpected sync change state.";
   1005         error = sync_error_factory_->CreateAndUploadError(
   1006             FROM_HERE,
   1007             "ProcessSyncChanges failed on ChangeType ACTION_ADD");
   1008         LOG(ERROR) << "Trying to add an existing TemplateURL.";
   1009         continue;
   1010       }
   1011       const std::string guid = turl->sync_guid();
   1012       if (existing_keyword_turl) {
   1013         // Resolve any conflicts so we can safely add the new entry.
   1014         ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl,
   1015                                    &new_changes);
   1016       }
   1017       // Force the local ID to kInvalidTemplateURLID so we can add it.
   1018       TemplateURLData data(turl->data());
   1019       data.id = kInvalidTemplateURLID;
   1020       Add(new TemplateURL(profile_, data));
   1021 
   1022       // Possibly set the newly added |turl| as the default search provider.
   1023       SetDefaultSearchProviderIfNewlySynced(guid);
   1024     } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) {
   1025       if (!existing_turl) {
   1026         NOTREACHED() << "Unexpected sync change state.";
   1027         error = sync_error_factory_->CreateAndUploadError(
   1028             FROM_HERE,
   1029             "ProcessSyncChanges failed on ChangeType ACTION_UPDATE");
   1030         LOG(ERROR) << "Trying to update a non-existent TemplateURL.";
   1031         continue;
   1032       }
   1033       if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) {
   1034         // Resolve any conflicts with other entries so we can safely update the
   1035         // keyword.
   1036         ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl,
   1037                                    &new_changes);
   1038       }
   1039       UIThreadSearchTermsData search_terms_data(existing_turl->profile());
   1040       if (UpdateNoNotify(existing_turl, *turl, search_terms_data))
   1041         NotifyObservers();
   1042     } else {
   1043       // We've unexpectedly received an ACTION_INVALID.
   1044       NOTREACHED() << "Unexpected sync change state.";
   1045       error = sync_error_factory_->CreateAndUploadError(
   1046           FROM_HERE,
   1047           "ProcessSyncChanges received an ACTION_INVALID");
   1048     }
   1049   }
   1050 
   1051   // If something went wrong, we want to prematurely exit to avoid pushing
   1052   // inconsistent data to Sync. We return the last error we received.
   1053   if (error.IsSet())
   1054     return error;
   1055 
   1056   error = sync_processor_->ProcessSyncChanges(from_here, new_changes);
   1057 
   1058   return error;
   1059 }
   1060 
   1061 syncer::SyncMergeResult TemplateURLService::MergeDataAndStartSyncing(
   1062     syncer::ModelType type,
   1063     const syncer::SyncDataList& initial_sync_data,
   1064     scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
   1065     scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
   1066   DCHECK(loaded_);
   1067   DCHECK_EQ(type, syncer::SEARCH_ENGINES);
   1068   DCHECK(!sync_processor_.get());
   1069   DCHECK(sync_processor.get());
   1070   DCHECK(sync_error_factory.get());
   1071   syncer::SyncMergeResult merge_result(type);
   1072   sync_processor_ = sync_processor.Pass();
   1073   sync_error_factory_ = sync_error_factory.Pass();
   1074 
   1075   // We just started syncing, so set our wait-for-default flag if we are
   1076   // expecting a default from Sync.
   1077   if (GetPrefs()) {
   1078     std::string default_guid = GetPrefs()->GetString(
   1079         prefs::kSyncedDefaultSearchProviderGUID);
   1080     const TemplateURL* current_default = GetDefaultSearchProvider();
   1081 
   1082     if (!default_guid.empty() &&
   1083         (!current_default || current_default->sync_guid() != default_guid))
   1084       pending_synced_default_search_ = true;
   1085   }
   1086 
   1087   // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we
   1088   // don't step on our own toes.
   1089   base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
   1090 
   1091   // We've started syncing, so set our origin member to the base Sync value.
   1092   // As we move through Sync Code, we may set this to increasingly specific
   1093   // origins so we can tell what exactly caused a DSP change.
   1094   base::AutoReset<DefaultSearchChangeOrigin> change_origin(&dsp_change_origin_,
   1095       DSP_CHANGE_SYNC_UNINTENTIONAL);
   1096 
   1097   syncer::SyncChangeList new_changes;
   1098 
   1099   // Build maps of our sync GUIDs to syncer::SyncData.
   1100   SyncDataMap local_data_map = CreateGUIDToSyncDataMap(
   1101       GetAllSyncData(syncer::SEARCH_ENGINES));
   1102   SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data);
   1103 
   1104   merge_result.set_num_items_before_association(local_data_map.size());
   1105   for (SyncDataMap::const_iterator iter = sync_data_map.begin();
   1106       iter != sync_data_map.end(); ++iter) {
   1107     TemplateURL* local_turl = GetTemplateURLForGUID(iter->first);
   1108     scoped_ptr<TemplateURL> sync_turl(
   1109         CreateTemplateURLFromTemplateURLAndSyncData(profile_, local_turl,
   1110             iter->second, &new_changes));
   1111     if (!sync_turl.get())
   1112       continue;
   1113 
   1114     if (pre_sync_deletes_.find(sync_turl->sync_guid()) !=
   1115         pre_sync_deletes_.end()) {
   1116       // This entry was deleted before the initial sync began (possibly through
   1117       // preprocessing in TemplateURLService's loading code). Ignore it and send
   1118       // an ACTION_DELETE up to the server.
   1119       new_changes.push_back(
   1120           syncer::SyncChange(FROM_HERE,
   1121                              syncer::SyncChange::ACTION_DELETE,
   1122                              iter->second));
   1123       UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
   1124           DELETE_ENGINE_PRE_SYNC, DELETE_ENGINE_MAX);
   1125       continue;
   1126     }
   1127 
   1128     if (local_turl) {
   1129       DCHECK(IsFromSync(local_turl, sync_data_map));
   1130       // This local search engine is already synced. If the timestamp differs
   1131       // from Sync, we need to update locally or to the cloud. Note that if the
   1132       // timestamps are equal, we touch neither.
   1133       if (sync_turl->last_modified() > local_turl->last_modified()) {
   1134         // We've received an update from Sync. We should replace all synced
   1135         // fields in the local TemplateURL. Note that this includes the
   1136         // TemplateURLID and the TemplateURL may have to be reparsed. This
   1137         // also makes the local data's last_modified timestamp equal to Sync's,
   1138         // avoiding an Update on the next MergeData call.
   1139         UIThreadSearchTermsData search_terms_data(local_turl->profile());
   1140         if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data))
   1141           NotifyObservers();
   1142         merge_result.set_num_items_modified(
   1143             merge_result.num_items_modified() + 1);
   1144       } else if (sync_turl->last_modified() < local_turl->last_modified()) {
   1145         // Otherwise, we know we have newer data, so update Sync with our
   1146         // data fields.
   1147         new_changes.push_back(
   1148             syncer::SyncChange(FROM_HERE,
   1149                                syncer::SyncChange::ACTION_UPDATE,
   1150                                local_data_map[local_turl->sync_guid()]));
   1151       }
   1152       local_data_map.erase(iter->first);
   1153     } else {
   1154       // The search engine from the cloud has not been synced locally. Merge it
   1155       // into our local model. This will handle any conflicts with local (and
   1156       // already-synced) TemplateURLs. It will prefer to keep entries from Sync
   1157       // over not-yet-synced TemplateURLs.
   1158       MergeInSyncTemplateURL(sync_turl.get(), sync_data_map, &new_changes,
   1159                              &local_data_map, &merge_result);
   1160     }
   1161   }
   1162 
   1163   // If there is a pending synced default search provider that was processed
   1164   // above, set it now.
   1165   TemplateURL* pending_default = GetPendingSyncedDefaultSearchProvider();
   1166   if (pending_default) {
   1167     base::AutoReset<DefaultSearchChangeOrigin> change_origin(
   1168         &dsp_change_origin_, DSP_CHANGE_SYNC_ADD);
   1169     SetDefaultSearchProvider(pending_default);
   1170   }
   1171 
   1172   // The remaining SyncData in local_data_map should be everything that needs to
   1173   // be pushed as ADDs to sync.
   1174   for (SyncDataMap::const_iterator iter = local_data_map.begin();
   1175       iter != local_data_map.end(); ++iter) {
   1176     new_changes.push_back(
   1177         syncer::SyncChange(FROM_HERE,
   1178                            syncer::SyncChange::ACTION_ADD,
   1179                            iter->second));
   1180   }
   1181 
   1182   // Do some post-processing on the change list to ensure that we are sending
   1183   // valid changes to sync_processor_.
   1184   PruneSyncChanges(&sync_data_map, &new_changes);
   1185 
   1186   LogDuplicatesHistogram(GetTemplateURLs());
   1187   merge_result.set_num_items_after_association(
   1188       GetAllSyncData(syncer::SEARCH_ENGINES).size());
   1189   merge_result.set_error(
   1190       sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes));
   1191   if (merge_result.error().IsSet())
   1192     return merge_result;
   1193 
   1194   // The ACTION_DELETEs from this set are processed. Empty it so we don't try to
   1195   // reuse them on the next call to MergeDataAndStartSyncing.
   1196   pre_sync_deletes_.clear();
   1197 
   1198   models_associated_ = true;
   1199   return merge_result;
   1200 }
   1201 
   1202 void TemplateURLService::StopSyncing(syncer::ModelType type) {
   1203   DCHECK_EQ(type, syncer::SEARCH_ENGINES);
   1204   models_associated_ = false;
   1205   sync_processor_.reset();
   1206   sync_error_factory_.reset();
   1207 }
   1208 
   1209 void TemplateURLService::ProcessTemplateURLChange(
   1210     const tracked_objects::Location& from_here,
   1211     const TemplateURL* turl,
   1212     syncer::SyncChange::SyncChangeType type) {
   1213   DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID);
   1214   DCHECK(turl);
   1215 
   1216   if (!models_associated_)
   1217     return;  // Not syncing.
   1218 
   1219   if (processing_syncer_changes_)
   1220     return;  // These are changes originating from us. Ignore.
   1221 
   1222   // Avoid syncing keywords managed by policy.
   1223   if (turl->created_by_policy())
   1224     return;
   1225 
   1226   syncer::SyncChangeList changes;
   1227 
   1228   syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
   1229   changes.push_back(syncer::SyncChange(from_here,
   1230                                        type,
   1231                                        sync_data));
   1232 
   1233   sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
   1234 }
   1235 
   1236 // static
   1237 syncer::SyncData TemplateURLService::CreateSyncDataFromTemplateURL(
   1238     const TemplateURL& turl) {
   1239   sync_pb::EntitySpecifics specifics;
   1240   sync_pb::SearchEngineSpecifics* se_specifics =
   1241       specifics.mutable_search_engine();
   1242   se_specifics->set_short_name(UTF16ToUTF8(turl.short_name()));
   1243   se_specifics->set_keyword(UTF16ToUTF8(turl.keyword()));
   1244   se_specifics->set_favicon_url(turl.favicon_url().spec());
   1245   se_specifics->set_url(turl.url());
   1246   se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace());
   1247   se_specifics->set_originating_url(turl.originating_url().spec());
   1248   se_specifics->set_date_created(turl.date_created().ToInternalValue());
   1249   se_specifics->set_input_encodings(JoinString(turl.input_encodings(), ';'));
   1250   se_specifics->set_show_in_default_list(turl.show_in_default_list());
   1251   se_specifics->set_suggestions_url(turl.suggestions_url());
   1252   se_specifics->set_prepopulate_id(turl.prepopulate_id());
   1253   se_specifics->set_instant_url(turl.instant_url());
   1254   if (!turl.image_url().empty())
   1255     se_specifics->set_image_url(turl.image_url());
   1256   if (!turl.search_url_post_params().empty())
   1257     se_specifics->set_search_url_post_params(turl.search_url_post_params());
   1258   if (!turl.suggestions_url_post_params().empty()) {
   1259     se_specifics->set_suggestions_url_post_params(
   1260         turl.suggestions_url_post_params());
   1261   }
   1262   if (!turl.instant_url_post_params().empty())
   1263     se_specifics->set_instant_url_post_params(turl.instant_url_post_params());
   1264   if (!turl.image_url_post_params().empty())
   1265     se_specifics->set_image_url_post_params(turl.image_url_post_params());
   1266   se_specifics->set_last_modified(turl.last_modified().ToInternalValue());
   1267   se_specifics->set_sync_guid(turl.sync_guid());
   1268   for (size_t i = 0; i < turl.alternate_urls().size(); ++i)
   1269     se_specifics->add_alternate_urls(turl.alternate_urls()[i]);
   1270   se_specifics->set_search_terms_replacement_key(
   1271       turl.search_terms_replacement_key());
   1272 
   1273   return syncer::SyncData::CreateLocalData(se_specifics->sync_guid(),
   1274                                            se_specifics->keyword(),
   1275                                            specifics);
   1276 }
   1277 
   1278 // static
   1279 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData(
   1280     Profile* profile,
   1281     TemplateURL* existing_turl,
   1282     const syncer::SyncData& sync_data,
   1283     syncer::SyncChangeList* change_list) {
   1284   DCHECK(change_list);
   1285 
   1286   sync_pb::SearchEngineSpecifics specifics =
   1287       sync_data.GetSpecifics().search_engine();
   1288 
   1289   // Past bugs might have caused either of these fields to be empty.  Just
   1290   // delete this data off the server.
   1291   if (specifics.url().empty() || specifics.sync_guid().empty()) {
   1292     change_list->push_back(
   1293         syncer::SyncChange(FROM_HERE,
   1294                            syncer::SyncChange::ACTION_DELETE,
   1295                            sync_data));
   1296     UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
   1297         DELETE_ENGINE_EMPTY_FIELD, DELETE_ENGINE_MAX);
   1298     return NULL;
   1299   }
   1300 
   1301   TemplateURLData data(existing_turl ?
   1302       existing_turl->data() : TemplateURLData());
   1303   data.short_name = UTF8ToUTF16(specifics.short_name());
   1304   data.originating_url = GURL(specifics.originating_url());
   1305   string16 keyword(UTF8ToUTF16(specifics.keyword()));
   1306   // NOTE: Once this code has shipped in a couple of stable releases, we can
   1307   // probably remove the migration portion, comment out the
   1308   // "autogenerate_keyword" field entirely in the .proto file, and fold the
   1309   // empty keyword case into the "delete data" block above.
   1310   bool reset_keyword =
   1311       specifics.autogenerate_keyword() || specifics.keyword().empty();
   1312   if (reset_keyword)
   1313     keyword = ASCIIToUTF16("dummy");  // Will be replaced below.
   1314   DCHECK(!keyword.empty());
   1315   data.SetKeyword(keyword);
   1316   data.SetURL(specifics.url());
   1317   data.suggestions_url = specifics.suggestions_url();
   1318   data.instant_url = specifics.instant_url();
   1319   data.image_url = specifics.image_url();
   1320   data.search_url_post_params = specifics.search_url_post_params();
   1321   data.suggestions_url_post_params = specifics.suggestions_url_post_params();
   1322   data.instant_url_post_params = specifics.instant_url_post_params();
   1323   data.image_url_post_params = specifics.image_url_post_params();
   1324   data.favicon_url = GURL(specifics.favicon_url());
   1325   data.show_in_default_list = specifics.show_in_default_list();
   1326   data.safe_for_autoreplace = specifics.safe_for_autoreplace();
   1327   base::SplitString(specifics.input_encodings(), ';', &data.input_encodings);
   1328   // If the server data has duplicate encodings, we'll want to push an update
   1329   // below to correct it.  Note that we also fix this in
   1330   // GetSearchProvidersUsingKeywordResult(), since otherwise we'd never correct
   1331   // local problems for clients which have disabled search engine sync.
   1332   bool deduped = DeDupeEncodings(&data.input_encodings);
   1333   data.date_created = base::Time::FromInternalValue(specifics.date_created());
   1334   data.last_modified = base::Time::FromInternalValue(specifics.last_modified());
   1335   data.prepopulate_id = specifics.prepopulate_id();
   1336   data.sync_guid = specifics.sync_guid();
   1337   data.alternate_urls.clear();
   1338   for (int i = 0; i < specifics.alternate_urls_size(); ++i)
   1339     data.alternate_urls.push_back(specifics.alternate_urls(i));
   1340   data.search_terms_replacement_key = specifics.search_terms_replacement_key();
   1341 
   1342   TemplateURL* turl = new TemplateURL(profile, data);
   1343   // If this TemplateURL matches a built-in prepopulated template URL, it's
   1344   // possible that sync is trying to modify fields that should not be touched.
   1345   // Revert these fields to the built-in values.
   1346   UpdateTemplateURLIfPrepopulated(turl, profile);
   1347   if (reset_keyword || deduped) {
   1348     if (reset_keyword)
   1349       turl->ResetKeywordIfNecessary(true);
   1350     syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
   1351     change_list->push_back(syncer::SyncChange(FROM_HERE,
   1352                                               syncer::SyncChange::ACTION_UPDATE,
   1353                                               sync_data));
   1354   } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) {
   1355     if (!existing_turl) {
   1356       // We're adding a new TemplateURL that uses the Google base URL, so set
   1357       // its keyword appropriately for the local environment.
   1358       turl->ResetKeywordIfNecessary(false);
   1359     } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) {
   1360       // Ignore keyword changes triggered by the Google base URL changing on
   1361       // another client.  If the base URL changes in this client as well, we'll
   1362       // pick that up separately at the appropriate time.  Otherwise, changing
   1363       // the keyword here could result in having the wrong keyword for the local
   1364       // environment.
   1365       turl->data_.SetKeyword(existing_turl->keyword());
   1366     }
   1367   }
   1368 
   1369   return turl;
   1370 }
   1371 
   1372 // static
   1373 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap(
   1374     const syncer::SyncDataList& sync_data) {
   1375   SyncDataMap data_map;
   1376   for (syncer::SyncDataList::const_iterator i(sync_data.begin());
   1377        i != sync_data.end();
   1378        ++i)
   1379     data_map[i->GetSpecifics().search_engine().sync_guid()] = *i;
   1380   return data_map;
   1381 }
   1382 
   1383 void TemplateURLService::SetKeywordSearchTermsForURL(const TemplateURL* t_url,
   1384                                                      const GURL& url,
   1385                                                      const string16& term) {
   1386   HistoryService* history = profile_  ?
   1387       HistoryServiceFactory::GetForProfile(profile_,
   1388                                            Profile::EXPLICIT_ACCESS) :
   1389       NULL;
   1390   if (!history)
   1391     return;
   1392   history->SetKeywordSearchTermsForURL(url, t_url->id(), term);
   1393 }
   1394 
   1395 void TemplateURLService::Init(const Initializer* initializers,
   1396                               int num_initializers) {
   1397   // Register for notifications.
   1398   if (profile_) {
   1399     // TODO(sky): bug 1166191. The keywords should be moved into the history
   1400     // db, which will mean we no longer need this notification and the history
   1401     // backend can handle automatically adding the search terms as the user
   1402     // navigates.
   1403     content::Source<Profile> profile_source(profile_->GetOriginalProfile());
   1404     notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED,
   1405                                 profile_source);
   1406     notification_registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
   1407                                 profile_source);
   1408     pref_change_registrar_.Init(GetPrefs());
   1409     pref_change_registrar_.Add(
   1410         prefs::kSyncedDefaultSearchProviderGUID,
   1411         base::Bind(
   1412             &TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged,
   1413             base::Unretained(this)));
   1414   }
   1415   notification_registrar_.Add(this,
   1416       chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
   1417       content::NotificationService::AllSources());
   1418 
   1419   if (num_initializers > 0) {
   1420     // This path is only hit by test code and is used to simulate a loaded
   1421     // TemplateURLService.
   1422     ChangeToLoadedState();
   1423 
   1424     // Add specific initializers, if any.
   1425     for (int i(0); i < num_initializers; ++i) {
   1426       DCHECK(initializers[i].keyword);
   1427       DCHECK(initializers[i].url);
   1428       DCHECK(initializers[i].content);
   1429 
   1430       // TemplateURLService ends up owning the TemplateURL, don't try and free
   1431       // it.
   1432       TemplateURLData data;
   1433       data.short_name = UTF8ToUTF16(initializers[i].content);
   1434       data.SetKeyword(UTF8ToUTF16(initializers[i].keyword));
   1435       data.SetURL(initializers[i].url);
   1436       TemplateURL* template_url = new TemplateURL(profile_, data);
   1437       AddNoNotify(template_url, true);
   1438 
   1439       // Set the first provided identifier to be the default.
   1440       if (i == 0)
   1441         SetDefaultSearchProviderNoNotify(template_url);
   1442     }
   1443   }
   1444 
   1445   // Initialize default search.
   1446   UpdateDefaultSearch();
   1447 
   1448   // Request a server check for the correct Google URL if Google is the
   1449   // default search engine, not in headless mode and not in Chrome Frame.
   1450   if (profile_ && initial_default_search_provider_.get() &&
   1451       initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) {
   1452     scoped_ptr<base::Environment> env(base::Environment::Create());
   1453     if (!env->HasVar(env_vars::kHeadless) &&
   1454         !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame))
   1455       GoogleURLTracker::RequestServerCheck(profile_, false);
   1456   }
   1457 }
   1458 
   1459 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) {
   1460   const string16& keyword = template_url->keyword();
   1461   DCHECK_NE(0U, keyword_to_template_map_.count(keyword));
   1462   if (keyword_to_template_map_[keyword] == template_url) {
   1463     // We need to check whether the keyword can now be provided by another
   1464     // TemplateURL.  See the comments in AddToMaps() for more information on
   1465     // extension keywords and how they can coexist with non-extension keywords.
   1466     // In the case of more than one extension, we use the most recently
   1467     // installed (which will be the most recently added, which will have the
   1468     // highest ID).
   1469     TemplateURL* best_fallback = NULL;
   1470     for (TemplateURLVector::const_iterator i(template_urls_.begin());
   1471          i != template_urls_.end(); ++i) {
   1472       TemplateURL* turl = *i;
   1473       // This next statement relies on the fact that there can only be one
   1474       // non-extension TemplateURL with a given keyword.
   1475       if ((turl != template_url) && (turl->keyword() == keyword) &&
   1476           (!best_fallback || !best_fallback->IsExtensionKeyword() ||
   1477            (turl->IsExtensionKeyword() && (turl->id() > best_fallback->id()))))
   1478         best_fallback = turl;
   1479     }
   1480     if (best_fallback)
   1481       keyword_to_template_map_[keyword] = best_fallback;
   1482     else
   1483       keyword_to_template_map_.erase(keyword);
   1484   }
   1485 
   1486   if (!template_url->sync_guid().empty())
   1487     guid_to_template_map_.erase(template_url->sync_guid());
   1488   if (loaded_) {
   1489     UIThreadSearchTermsData search_terms_data(template_url->profile());
   1490     provider_map_->Remove(template_url, search_terms_data);
   1491   }
   1492 }
   1493 
   1494 void TemplateURLService::RemoveFromKeywordMapByPointer(
   1495     TemplateURL* template_url) {
   1496   DCHECK(template_url);
   1497   for (KeywordToTemplateMap::iterator i = keyword_to_template_map_.begin();
   1498        i != keyword_to_template_map_.end(); ++i) {
   1499     if (i->second == template_url) {
   1500       keyword_to_template_map_.erase(i);
   1501       // A given TemplateURL only occurs once in the map. As soon as we find the
   1502       // entry, stop.
   1503       break;
   1504     }
   1505   }
   1506 }
   1507 
   1508 void TemplateURLService::AddToMaps(TemplateURL* template_url) {
   1509   bool template_extension = template_url->IsExtensionKeyword();
   1510   const string16& keyword = template_url->keyword();
   1511   KeywordToTemplateMap::const_iterator i =
   1512       keyword_to_template_map_.find(keyword);
   1513   if (i == keyword_to_template_map_.end()) {
   1514     keyword_to_template_map_[keyword] = template_url;
   1515   } else {
   1516     const TemplateURL* existing_url = i->second;
   1517     // We should only have overlapping keywords when at least one comes from
   1518     // an extension.  In that case, the ranking order is:
   1519     //   Manually-modified keywords > extension keywords > replaceable keywords
   1520     // When there are multiple extensions, the last-added wins.
   1521     bool existing_extension = existing_url->IsExtensionKeyword();
   1522     DCHECK(existing_extension || template_extension);
   1523     if (existing_extension ?
   1524         !CanReplace(template_url) : CanReplace(existing_url))
   1525       keyword_to_template_map_[keyword] = template_url;
   1526   }
   1527 
   1528   if (!template_url->sync_guid().empty())
   1529     guid_to_template_map_[template_url->sync_guid()] = template_url;
   1530   if (loaded_) {
   1531     UIThreadSearchTermsData search_terms_data(profile_);
   1532     provider_map_->Add(template_url, search_terms_data);
   1533   }
   1534 }
   1535 
   1536 // Helper for partition() call in next function.
   1537 bool HasValidID(TemplateURL* t_url) {
   1538   return t_url->id() != kInvalidTemplateURLID;
   1539 }
   1540 
   1541 void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) {
   1542   // Partition the URLs first, instead of implementing the loops below by simply
   1543   // scanning the input twice.  While it's not supposed to happen normally, it's
   1544   // possible for corrupt databases to return multiple entries with the same
   1545   // keyword.  In this case, the first loop may delete the first entry when
   1546   // adding the second.  If this happens, the second loop must not attempt to
   1547   // access the deleted entry.  Partitioning ensures this constraint.
   1548   TemplateURLVector::iterator first_invalid(
   1549       std::partition(urls->begin(), urls->end(), HasValidID));
   1550 
   1551   // First, add the items that already have id's, so that the next_id_ gets
   1552   // properly set.
   1553   for (TemplateURLVector::const_iterator i = urls->begin(); i != first_invalid;
   1554        ++i) {
   1555     next_id_ = std::max(next_id_, (*i)->id());
   1556     AddNoNotify(*i, false);
   1557   }
   1558 
   1559   // Next add the new items that don't have id's.
   1560   for (TemplateURLVector::const_iterator i = first_invalid; i != urls->end();
   1561        ++i)
   1562     AddNoNotify(*i, true);
   1563 
   1564   // Clear the input vector to reduce the chance callers will try to use a
   1565   // (possibly deleted) entry.
   1566   urls->clear();
   1567 }
   1568 
   1569 void TemplateURLService::ChangeToLoadedState() {
   1570   DCHECK(!loaded_);
   1571 
   1572   UIThreadSearchTermsData search_terms_data(profile_);
   1573   provider_map_->Init(template_urls_, search_terms_data);
   1574   loaded_ = true;
   1575 }
   1576 
   1577 void TemplateURLService::NotifyLoaded() {
   1578   content::NotificationService::current()->Notify(
   1579       chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED,
   1580       content::Source<TemplateURLService>(this),
   1581       content::NotificationService::NoDetails());
   1582 }
   1583 
   1584 void TemplateURLService::SaveDefaultSearchProviderToPrefs(
   1585     const TemplateURL* t_url) {
   1586   PrefService* prefs = GetPrefs();
   1587   if (!prefs)
   1588     return;
   1589 
   1590   bool enabled = false;
   1591   std::string search_url;
   1592   std::string suggest_url;
   1593   std::string instant_url;
   1594   std::string image_url;
   1595   std::string search_url_post_params;
   1596   std::string suggest_url_post_params;
   1597   std::string instant_url_post_params;
   1598   std::string image_url_post_params;
   1599   std::string icon_url;
   1600   std::string encodings;
   1601   std::string short_name;
   1602   std::string keyword;
   1603   std::string id_string;
   1604   std::string prepopulate_id;
   1605   ListValue alternate_urls;
   1606   std::string search_terms_replacement_key;
   1607   if (t_url) {
   1608     DCHECK(!t_url->IsExtensionKeyword());
   1609     enabled = true;
   1610     search_url = t_url->url();
   1611     suggest_url = t_url->suggestions_url();
   1612     instant_url = t_url->instant_url();
   1613     image_url = t_url->image_url();
   1614     search_url_post_params = t_url->search_url_post_params();
   1615     suggest_url_post_params = t_url->suggestions_url_post_params();
   1616     instant_url_post_params = t_url->instant_url_post_params();
   1617     image_url_post_params = t_url->image_url_post_params();
   1618     GURL icon_gurl = t_url->favicon_url();
   1619     if (!icon_gurl.is_empty())
   1620       icon_url = icon_gurl.spec();
   1621     encodings = JoinString(t_url->input_encodings(), ';');
   1622     short_name = UTF16ToUTF8(t_url->short_name());
   1623     keyword = UTF16ToUTF8(t_url->keyword());
   1624     id_string = base::Int64ToString(t_url->id());
   1625     prepopulate_id = base::Int64ToString(t_url->prepopulate_id());
   1626     for (size_t i = 0; i < t_url->alternate_urls().size(); ++i)
   1627       alternate_urls.AppendString(t_url->alternate_urls()[i]);
   1628     search_terms_replacement_key = t_url->search_terms_replacement_key();
   1629   }
   1630   prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled);
   1631   prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url);
   1632   prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url);
   1633   prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url);
   1634   prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url);
   1635   prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams,
   1636                    search_url_post_params);
   1637   prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams,
   1638                    suggest_url_post_params);
   1639   prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams,
   1640                    instant_url_post_params);
   1641   prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams,
   1642                    image_url_post_params);
   1643   prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url);
   1644   prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings);
   1645   prefs->SetString(prefs::kDefaultSearchProviderName, short_name);
   1646   prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword);
   1647   prefs->SetString(prefs::kDefaultSearchProviderID, id_string);
   1648   prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id);
   1649   prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls);
   1650   prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey,
   1651       search_terms_replacement_key);
   1652 }
   1653 
   1654 bool TemplateURLService::LoadDefaultSearchProviderFromPrefs(
   1655     scoped_ptr<TemplateURL>* default_provider,
   1656     bool* is_managed) {
   1657   PrefService* prefs = GetPrefs();
   1658   if (!prefs || !prefs->HasPrefPath(prefs::kDefaultSearchProviderSearchURL))
   1659     return false;
   1660 
   1661   const PrefService::Preference* pref =
   1662       prefs->FindPreference(prefs::kDefaultSearchProviderSearchURL);
   1663   *is_managed = pref && pref->IsManaged();
   1664 
   1665   if (!prefs->GetBoolean(prefs::kDefaultSearchProviderEnabled)) {
   1666     // The user doesn't want a default search provider.
   1667     default_provider->reset(NULL);
   1668     return true;
   1669   }
   1670 
   1671   string16 name =
   1672       UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderName));
   1673   string16 keyword =
   1674       UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderKeyword));
   1675   // Force keyword to be non-empty.
   1676   // TODO(pkasting): This is only necessary as long as we're potentially loading
   1677   // older prefs where empty keywords are theoretically possible.  Eventually
   1678   // this code can be replaced with a DCHECK(!keyword.empty());.
   1679   bool update_keyword = keyword.empty();
   1680   if (update_keyword)
   1681     keyword = ASCIIToUTF16("dummy");
   1682   std::string search_url =
   1683       prefs->GetString(prefs::kDefaultSearchProviderSearchURL);
   1684   // Force URL to be non-empty.  We've never supported this case, but past bugs
   1685   // might have resulted in it slipping through; eventually this code can be
   1686   // replaced with a DCHECK(!search_url.empty());.
   1687   if (search_url.empty())
   1688     return false;
   1689   std::string suggest_url =
   1690       prefs->GetString(prefs::kDefaultSearchProviderSuggestURL);
   1691   std::string instant_url =
   1692       prefs->GetString(prefs::kDefaultSearchProviderInstantURL);
   1693   std::string image_url =
   1694       prefs->GetString(prefs::kDefaultSearchProviderImageURL);
   1695   std::string search_url_post_params =
   1696       prefs->GetString(prefs::kDefaultSearchProviderSearchURLPostParams);
   1697   std::string suggest_url_post_params =
   1698       prefs->GetString(prefs::kDefaultSearchProviderSuggestURLPostParams);
   1699   std::string instant_url_post_params =
   1700       prefs->GetString(prefs::kDefaultSearchProviderInstantURLPostParams);
   1701   std::string image_url_post_params =
   1702       prefs->GetString(prefs::kDefaultSearchProviderImageURLPostParams);
   1703   std::string icon_url =
   1704       prefs->GetString(prefs::kDefaultSearchProviderIconURL);
   1705   std::string encodings =
   1706       prefs->GetString(prefs::kDefaultSearchProviderEncodings);
   1707   std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID);
   1708   std::string prepopulate_id =
   1709       prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID);
   1710   const ListValue* alternate_urls =
   1711       prefs->GetList(prefs::kDefaultSearchProviderAlternateURLs);
   1712   std::string search_terms_replacement_key = prefs->GetString(
   1713       prefs::kDefaultSearchProviderSearchTermsReplacementKey);
   1714 
   1715   TemplateURLData data;
   1716   data.short_name = name;
   1717   data.SetKeyword(keyword);
   1718   data.SetURL(search_url);
   1719   data.suggestions_url = suggest_url;
   1720   data.instant_url = instant_url;
   1721   data.image_url = image_url;
   1722   data.search_url_post_params = search_url_post_params;
   1723   data.suggestions_url_post_params = suggest_url_post_params;
   1724   data.instant_url_post_params = instant_url_post_params;
   1725   data.image_url_post_params = image_url_post_params;
   1726   data.favicon_url = GURL(icon_url);
   1727   data.show_in_default_list = true;
   1728   data.alternate_urls.clear();
   1729   for (size_t i = 0; i < alternate_urls->GetSize(); ++i) {
   1730     std::string alternate_url;
   1731     if (alternate_urls->GetString(i, &alternate_url))
   1732       data.alternate_urls.push_back(alternate_url);
   1733   }
   1734   data.search_terms_replacement_key = search_terms_replacement_key;
   1735   base::SplitString(encodings, ';', &data.input_encodings);
   1736   if (!id_string.empty() && !*is_managed) {
   1737     int64 value;
   1738     base::StringToInt64(id_string, &value);
   1739     data.id = value;
   1740   }
   1741   if (!prepopulate_id.empty() && !*is_managed) {
   1742     int value;
   1743     base::StringToInt(prepopulate_id, &value);
   1744     data.prepopulate_id = value;
   1745   }
   1746   default_provider->reset(new TemplateURL(profile_, data));
   1747   DCHECK(!(*default_provider)->IsExtensionKeyword());
   1748   if (update_keyword)
   1749     (*default_provider)->ResetKeywordIfNecessary(true);
   1750   return true;
   1751 }
   1752 
   1753 void TemplateURLService::ClearDefaultProviderFromPrefs() {
   1754   // We overwrite user preferences. If the default search engine is managed,
   1755   // there is no effect.
   1756   SaveDefaultSearchProviderToPrefs(NULL);
   1757   // Default value for kDefaultSearchProviderEnabled is true.
   1758   PrefService* prefs = GetPrefs();
   1759   if (prefs)
   1760     prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, true);
   1761 }
   1762 
   1763 bool TemplateURLService::CanReplaceKeywordForHost(
   1764     const std::string& host,
   1765     TemplateURL** to_replace) {
   1766   DCHECK(!to_replace || !*to_replace);
   1767   const TemplateURLSet* urls = provider_map_->GetURLsForHost(host);
   1768   if (!urls)
   1769     return true;
   1770   for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) {
   1771     if (CanReplace(*i)) {
   1772       if (to_replace)
   1773         *to_replace = *i;
   1774       return true;
   1775     }
   1776   }
   1777   return false;
   1778 }
   1779 
   1780 bool TemplateURLService::CanReplace(const TemplateURL* t_url) {
   1781   return (t_url != default_search_provider_ && !t_url->show_in_default_list() &&
   1782           t_url->safe_for_autoreplace());
   1783 }
   1784 
   1785 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword(
   1786     const string16& keyword) {
   1787   TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword);
   1788   if (!keyword_turl || !keyword_turl->IsExtensionKeyword())
   1789     return keyword_turl;
   1790   // The extension keyword in the model may be hiding a replaceable
   1791   // non-extension keyword.  Look for it.
   1792   for (TemplateURLVector::const_iterator i(template_urls_.begin());
   1793        i != template_urls_.end(); ++i) {
   1794     if (!(*i)->IsExtensionKeyword() && ((*i)->keyword() == keyword))
   1795       return *i;
   1796   }
   1797   return NULL;
   1798 }
   1799 
   1800 bool TemplateURLService::UpdateNoNotify(
   1801     TemplateURL* existing_turl,
   1802     const TemplateURL& new_values,
   1803     const SearchTermsData& old_search_terms_data) {
   1804   DCHECK(loaded_);
   1805   DCHECK(existing_turl);
   1806   if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) ==
   1807       template_urls_.end())
   1808     return false;
   1809 
   1810   string16 old_keyword(existing_turl->keyword());
   1811   keyword_to_template_map_.erase(old_keyword);
   1812   if (!existing_turl->sync_guid().empty())
   1813     guid_to_template_map_.erase(existing_turl->sync_guid());
   1814 
   1815   provider_map_->Remove(existing_turl, old_search_terms_data);
   1816   TemplateURLID previous_id = existing_turl->id();
   1817   existing_turl->CopyFrom(new_values);
   1818   existing_turl->data_.id = previous_id;
   1819   UIThreadSearchTermsData new_search_terms_data(profile_);
   1820   provider_map_->Add(existing_turl, new_search_terms_data);
   1821 
   1822   const string16& keyword = existing_turl->keyword();
   1823   KeywordToTemplateMap::const_iterator i =
   1824       keyword_to_template_map_.find(keyword);
   1825   if (i == keyword_to_template_map_.end()) {
   1826     keyword_to_template_map_[keyword] = existing_turl;
   1827   } else {
   1828     // We can theoretically reach here in two cases:
   1829     //   * There is an existing extension keyword and sync brings in a rename of
   1830     //     a non-extension keyword to match.  In this case we just need to pick
   1831     //     which keyword has priority to update the keyword map.
   1832     //   * Autogeneration of the keyword for a Google default search provider
   1833     //     at load time causes it to conflict with an existing keyword.  In this
   1834     //     case we delete the existing keyword if it's replaceable, or else undo
   1835     //     the change in keyword for |existing_turl|.
   1836     TemplateURL* existing_keyword_turl = i->second;
   1837     if (existing_keyword_turl->IsExtensionKeyword()) {
   1838       if (!CanReplace(existing_turl))
   1839         keyword_to_template_map_[keyword] = existing_turl;
   1840     } else {
   1841       if (CanReplace(existing_keyword_turl)) {
   1842         RemoveNoNotify(existing_keyword_turl);
   1843       } else {
   1844         existing_turl->data_.SetKeyword(old_keyword);
   1845         keyword_to_template_map_[old_keyword] = existing_turl;
   1846       }
   1847     }
   1848   }
   1849   if (!existing_turl->sync_guid().empty())
   1850     guid_to_template_map_[existing_turl->sync_guid()] = existing_turl;
   1851 
   1852   if (service_.get())
   1853     service_->UpdateKeyword(existing_turl->data());
   1854 
   1855   // Inform sync of the update.
   1856   ProcessTemplateURLChange(FROM_HERE,
   1857                            existing_turl,
   1858                            syncer::SyncChange::ACTION_UPDATE);
   1859 
   1860   if (default_search_provider_ == existing_turl) {
   1861     bool success = SetDefaultSearchProviderNoNotify(existing_turl);
   1862     DCHECK(success);
   1863   }
   1864   return true;
   1865 }
   1866 
   1867 // static
   1868 void TemplateURLService::UpdateTemplateURLIfPrepopulated(
   1869     TemplateURL* template_url,
   1870     Profile* profile) {
   1871   int prepopulate_id = template_url->prepopulate_id();
   1872   if (template_url->prepopulate_id() == 0)
   1873     return;
   1874 
   1875   ScopedVector<TemplateURL> prepopulated_urls;
   1876   size_t default_search_index;
   1877   TemplateURLPrepopulateData::GetPrepopulatedEngines(profile,
   1878       &prepopulated_urls.get(), &default_search_index);
   1879 
   1880   for (size_t i = 0; i < prepopulated_urls.size(); ++i) {
   1881     if (prepopulated_urls[i]->prepopulate_id() == prepopulate_id) {
   1882       MergeIntoPrepopulatedEngineData(&prepopulated_urls[i]->data_,
   1883                                       template_url);
   1884       template_url->CopyFrom(*prepopulated_urls[i]);
   1885     }
   1886   }
   1887 }
   1888 
   1889 PrefService* TemplateURLService::GetPrefs() {
   1890   return profile_ ? profile_->GetPrefs() : NULL;
   1891 }
   1892 
   1893 void TemplateURLService::UpdateKeywordSearchTermsForURL(
   1894     const history::URLVisitedDetails& details) {
   1895   const history::URLRow& row = details.row;
   1896   if (!row.url().is_valid())
   1897     return;
   1898 
   1899   const TemplateURLSet* urls_for_host =
   1900       provider_map_->GetURLsForHost(row.url().host());
   1901   if (!urls_for_host)
   1902     return;
   1903 
   1904   QueryTerms query_terms;
   1905   const std::string path = row.url().path();
   1906 
   1907   for (TemplateURLSet::const_iterator i = urls_for_host->begin();
   1908        i != urls_for_host->end(); ++i) {
   1909     string16 search_terms;
   1910     if ((*i)->ExtractSearchTermsFromURL(row.url(), &search_terms) &&
   1911         !search_terms.empty()) {
   1912       if (content::PageTransitionStripQualifier(details.transition) ==
   1913           content::PAGE_TRANSITION_KEYWORD) {
   1914         // The visit is the result of the user entering a keyword, generate a
   1915         // KEYWORD_GENERATED visit for the KEYWORD so that the keyword typed
   1916         // count is boosted.
   1917         AddTabToSearchVisit(**i);
   1918       }
   1919       SetKeywordSearchTermsForURL(*i, row.url(), search_terms);
   1920     }
   1921   }
   1922 }
   1923 
   1924 void TemplateURLService::AddTabToSearchVisit(const TemplateURL& t_url) {
   1925   // Only add visits for entries the user hasn't modified. If the user modified
   1926   // the entry the keyword may no longer correspond to the host name. It may be
   1927   // possible to do something more sophisticated here, but it's so rare as to
   1928   // not be worth it.
   1929   if (!t_url.safe_for_autoreplace())
   1930     return;
   1931 
   1932   if (!profile_)
   1933     return;
   1934 
   1935   HistoryService* history =
   1936       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
   1937   if (!history)
   1938     return;
   1939 
   1940   GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(t_url.keyword()),
   1941                                    std::string()));
   1942   if (!url.is_valid())
   1943     return;
   1944 
   1945   // Synthesize a visit for the keyword. This ensures the url for the keyword is
   1946   // autocompleted even if the user doesn't type the url in directly.
   1947   history->AddPage(url, base::Time::Now(), NULL, 0, GURL(),
   1948                    history::RedirectList(),
   1949                    content::PAGE_TRANSITION_KEYWORD_GENERATED,
   1950                    history::SOURCE_BROWSED, false);
   1951 }
   1952 
   1953 void TemplateURLService::GoogleBaseURLChanged(const GURL& old_base_url) {
   1954   bool something_changed = false;
   1955   for (TemplateURLVector::iterator i(template_urls_.begin());
   1956        i != template_urls_.end(); ++i) {
   1957     TemplateURL* t_url = *i;
   1958     if (t_url->url_ref().HasGoogleBaseURLs() ||
   1959         t_url->suggestions_url_ref().HasGoogleBaseURLs()) {
   1960       TemplateURL updated_turl(t_url->profile(), t_url->data());
   1961       updated_turl.ResetKeywordIfNecessary(false);
   1962       KeywordToTemplateMap::const_iterator existing_entry =
   1963           keyword_to_template_map_.find(updated_turl.keyword());
   1964       if ((existing_entry != keyword_to_template_map_.end()) &&
   1965           (existing_entry->second != t_url)) {
   1966         // The new autogenerated keyword conflicts with another TemplateURL.
   1967         // Overwrite it if it's replaceable; otherwise, leave |t_url| using its
   1968         // current keyword.  (This will not prevent |t_url| from auto-updating
   1969         // the keyword in the future if the conflicting TemplateURL disappears.)
   1970         // Note that we must still update |t_url| in this case, or the
   1971         // |provider_map_| will not be updated correctly.
   1972         if (CanReplace(existing_entry->second))
   1973           RemoveNoNotify(existing_entry->second);
   1974         else
   1975           updated_turl.data_.SetKeyword(t_url->keyword());
   1976       }
   1977       something_changed = true;
   1978       // This will send the keyword change to sync.  Note that other clients
   1979       // need to reset the keyword to an appropriate local value when this
   1980       // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData().
   1981       UpdateNoNotify(t_url, updated_turl,
   1982           OldBaseURLSearchTermsData(t_url->profile(), old_base_url.spec()));
   1983     }
   1984   }
   1985   if (something_changed)
   1986     NotifyObservers();
   1987 }
   1988 
   1989 void TemplateURLService::UpdateDefaultSearch() {
   1990   if (!loaded_) {
   1991     // Set |initial_default_search_provider_| from the preferences.  We use this
   1992     // value for default search provider until the database has been loaded.
   1993     if (!LoadDefaultSearchProviderFromPrefs(&initial_default_search_provider_,
   1994                                             &is_default_search_managed_)) {
   1995       // Prefs does not specify, so rely on the prepopulated engines.  This
   1996       // should happen only the first time Chrome is started.
   1997       initial_default_search_provider_.reset(
   1998           TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_));
   1999       is_default_search_managed_ = false;
   2000     }
   2001     return;
   2002   }
   2003   // Load the default search specified in prefs.
   2004   scoped_ptr<TemplateURL> new_default_from_prefs;
   2005   bool new_is_default_managed = false;
   2006   // Load the default from prefs.  It's possible that it won't succeed
   2007   // because we are in the middle of doing SaveDefaultSearchProviderToPrefs()
   2008   // and all the preference items have not been saved.  In that case, we
   2009   // don't have yet a default.  It would be much better if we could save
   2010   // preferences in batches and trigger notifications at the end.
   2011   LoadDefaultSearchProviderFromPrefs(&new_default_from_prefs,
   2012                                      &new_is_default_managed);
   2013   if (!is_default_search_managed_ && !new_is_default_managed) {
   2014     // We're not interested in cases where the default was and remains
   2015     // unmanaged.  In that case, preferences have no impact on the default.
   2016     return;
   2017   }
   2018   if (is_default_search_managed_ && new_is_default_managed) {
   2019     // The default was managed and remains managed.  Update the default only
   2020     // if it has changed; we don't want to respond to changes triggered by
   2021     // SaveDefaultSearchProviderToPrefs.
   2022     if (TemplateURLsHaveSamePrefs(default_search_provider_,
   2023                                   new_default_from_prefs.get()))
   2024       return;
   2025     if (new_default_from_prefs.get() == NULL) {
   2026       // default_search_provider_ can't be NULL otherwise
   2027       // TemplateURLsHaveSamePrefs would have returned true.  Remove this now
   2028       // invalid value.
   2029       TemplateURL* old_default = default_search_provider_;
   2030       bool success = SetDefaultSearchProviderNoNotify(NULL);
   2031       DCHECK(success);
   2032       RemoveNoNotify(old_default);
   2033     } else if (default_search_provider_) {
   2034       TemplateURLData data(new_default_from_prefs->data());
   2035       data.created_by_policy = true;
   2036       TemplateURL new_values(new_default_from_prefs->profile(), data);
   2037       UIThreadSearchTermsData search_terms_data(
   2038           default_search_provider_->profile());
   2039       UpdateNoNotify(default_search_provider_, new_values, search_terms_data);
   2040     } else {
   2041       TemplateURL* new_template = NULL;
   2042       if (new_default_from_prefs.get()) {
   2043         TemplateURLData data(new_default_from_prefs->data());
   2044         data.created_by_policy = true;
   2045         new_template = new TemplateURL(profile_, data);
   2046         if (!AddNoNotify(new_template, true))
   2047           return;
   2048       }
   2049       bool success = SetDefaultSearchProviderNoNotify(new_template);
   2050       DCHECK(success);
   2051     }
   2052   } else if (!is_default_search_managed_ && new_is_default_managed) {
   2053     // The default used to be unmanaged and is now managed.  Add the new
   2054     // managed default to the list of URLs and set it as default.
   2055     is_default_search_managed_ = new_is_default_managed;
   2056     TemplateURL* new_template = NULL;
   2057     if (new_default_from_prefs.get()) {
   2058       TemplateURLData data(new_default_from_prefs->data());
   2059       data.created_by_policy = true;
   2060       new_template = new TemplateURL(profile_, data);
   2061       if (!AddNoNotify(new_template, true))
   2062         return;
   2063     }
   2064     bool success = SetDefaultSearchProviderNoNotify(new_template);
   2065     DCHECK(success);
   2066   } else {
   2067     // The default was managed and is no longer.
   2068     DCHECK(is_default_search_managed_ && !new_is_default_managed);
   2069     is_default_search_managed_ = new_is_default_managed;
   2070     // If we had a default, delete the previous default if created by policy
   2071     // and set a likely default.
   2072     if ((default_search_provider_ != NULL) &&
   2073         default_search_provider_->created_by_policy()) {
   2074       TemplateURL* old_default = default_search_provider_;
   2075       default_search_provider_ = NULL;
   2076       RemoveNoNotify(old_default);
   2077     }
   2078 
   2079     // The likely default should be from Sync if we were waiting on Sync.
   2080     // Otherwise, it should be FindNewDefaultSearchProvider.
   2081     TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider();
   2082     if (synced_default) {
   2083       pending_synced_default_search_ = false;
   2084 
   2085       base::AutoReset<DefaultSearchChangeOrigin> change_origin(
   2086           &dsp_change_origin_, DSP_CHANGE_SYNC_NOT_MANAGED);
   2087       SetDefaultSearchProviderNoNotify(synced_default);
   2088     } else {
   2089       SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider());
   2090     }
   2091   }
   2092   NotifyObservers();
   2093 }
   2094 
   2095 bool TemplateURLService::SetDefaultSearchProviderNoNotify(TemplateURL* url) {
   2096   if (url) {
   2097     if (std::find(template_urls_.begin(), template_urls_.end(), url) ==
   2098         template_urls_.end())
   2099       return false;
   2100     // Extension keywords cannot be made default, as they're inherently async.
   2101     DCHECK(!url->IsExtensionKeyword());
   2102   }
   2103 
   2104   // Only bother reassigning |url| if it has changed. Notice that we don't just
   2105   // early exit if they are equal, because |url| may have had its fields
   2106   // changed, and needs to be persisted below (for example, when this is called
   2107   // from UpdateNoNotify).
   2108   if (default_search_provider_ != url) {
   2109     UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchChangeOrigin",
   2110                               dsp_change_origin_, DSP_CHANGE_MAX);
   2111     default_search_provider_ = url;
   2112   }
   2113 
   2114   if (url) {
   2115     // Don't mark the url as edited, otherwise we won't be able to rev the
   2116     // template urls we ship with.
   2117     url->data_.show_in_default_list = true;
   2118     if (service_.get())
   2119       service_->UpdateKeyword(url->data());
   2120 
   2121     if (url->url_ref().HasGoogleBaseURLs()) {
   2122       GoogleURLTracker::RequestServerCheck(profile_, false);
   2123 #if defined(ENABLE_RLZ)
   2124       RLZTracker::RecordProductEvent(rlz_lib::CHROME,
   2125                                      RLZTracker::CHROME_OMNIBOX,
   2126                                      rlz_lib::SET_TO_GOOGLE);
   2127 #endif
   2128     }
   2129   }
   2130 
   2131   if (!is_default_search_managed_) {
   2132     SaveDefaultSearchProviderToPrefs(url);
   2133 
   2134     // If we are syncing, we want to set the synced pref that will notify other
   2135     // instances to change their default to this new search provider.
   2136     // Note: we don't update the pref if we're currently in the middle of
   2137     // handling a sync operation. Sync operations from other clients are not
   2138     // guaranteed to arrive together, and any client that deletes the default
   2139     // needs to set a new default as well. If we update the default here, we're
   2140     // likely to race with the update from the other client, resulting in
   2141     // a possibly random default search provider.
   2142     if (sync_processor_.get() && url && !url->sync_guid().empty() &&
   2143         GetPrefs() && !processing_syncer_changes_) {
   2144       GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID,
   2145                             url->sync_guid());
   2146     }
   2147   }
   2148 
   2149   if (service_.get())
   2150     service_->SetDefaultSearchProvider(url);
   2151 
   2152   // Inform sync the change to the show_in_default_list flag.
   2153   if (url)
   2154     ProcessTemplateURLChange(FROM_HERE,
   2155                              url,
   2156                              syncer::SyncChange::ACTION_UPDATE);
   2157   return true;
   2158 }
   2159 
   2160 bool TemplateURLService::AddNoNotify(TemplateURL* template_url,
   2161                                      bool newly_adding) {
   2162   DCHECK(template_url);
   2163 
   2164   if (newly_adding) {
   2165     DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
   2166     DCHECK(std::find(template_urls_.begin(), template_urls_.end(),
   2167                      template_url) == template_urls_.end());
   2168     template_url->data_.id = ++next_id_;
   2169   }
   2170 
   2171   template_url->ResetKeywordIfNecessary(false);
   2172   // Check whether |template_url|'s keyword conflicts with any already in the
   2173   // model.
   2174   TemplateURL* existing_keyword_turl =
   2175       GetTemplateURLForKeyword(template_url->keyword());
   2176   if (existing_keyword_turl != NULL) {
   2177     DCHECK_NE(existing_keyword_turl, template_url);
   2178     // Only replace one of the TemplateURLs if they are either both extensions,
   2179     // or both not extensions.
   2180     bool are_same_type = existing_keyword_turl->IsExtensionKeyword() ==
   2181         template_url->IsExtensionKeyword();
   2182     if (CanReplace(existing_keyword_turl) && are_same_type) {
   2183       RemoveNoNotify(existing_keyword_turl);
   2184     } else if (CanReplace(template_url) && are_same_type) {
   2185       delete template_url;
   2186       return false;
   2187     } else {
   2188       string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false);
   2189       ResetTemplateURL(existing_keyword_turl,
   2190                        existing_keyword_turl->short_name(), new_keyword,
   2191                        existing_keyword_turl->url());
   2192     }
   2193   }
   2194   template_urls_.push_back(template_url);
   2195   AddToMaps(template_url);
   2196 
   2197   if (newly_adding) {
   2198     if (service_.get())
   2199       service_->AddKeyword(template_url->data());
   2200 
   2201     // Inform sync of the addition. Note that this will assign a GUID to
   2202     // template_url and add it to the guid_to_template_map_.
   2203     ProcessTemplateURLChange(FROM_HERE,
   2204                              template_url,
   2205                              syncer::SyncChange::ACTION_ADD);
   2206   }
   2207 
   2208   return true;
   2209 }
   2210 
   2211 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) {
   2212   TemplateURLVector::iterator i =
   2213       std::find(template_urls_.begin(), template_urls_.end(), template_url);
   2214   if (i == template_urls_.end())
   2215     return;
   2216 
   2217   if (template_url == default_search_provider_) {
   2218     // Should never delete the default search provider.
   2219     NOTREACHED();
   2220     return;
   2221   }
   2222 
   2223   RemoveFromMaps(template_url);
   2224 
   2225   // Remove it from the vector containing all TemplateURLs.
   2226   template_urls_.erase(i);
   2227 
   2228   if (service_.get())
   2229     service_->RemoveKeyword(template_url->id());
   2230 
   2231   // Inform sync of the deletion.
   2232   ProcessTemplateURLChange(FROM_HERE,
   2233                            template_url,
   2234                            syncer::SyncChange::ACTION_DELETE);
   2235   UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
   2236       DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX);
   2237 
   2238   if (profile_) {
   2239     content::Source<Profile> source(profile_);
   2240     TemplateURLID id = template_url->id();
   2241     content::NotificationService::current()->Notify(
   2242         chrome::NOTIFICATION_TEMPLATE_URL_REMOVED,
   2243         source,
   2244         content::Details<TemplateURLID>(&id));
   2245   }
   2246 
   2247   // We own the TemplateURL and need to delete it.
   2248   delete template_url;
   2249 }
   2250 
   2251 void TemplateURLService::NotifyObservers() {
   2252   if (!loaded_)
   2253     return;
   2254 
   2255   FOR_EACH_OBSERVER(TemplateURLServiceObserver, model_observers_,
   2256                     OnTemplateURLServiceChanged());
   2257 }
   2258 
   2259 // |template_urls| are the TemplateURLs loaded from the database.
   2260 // |default_search_provider| points to one of them, if it was set in the db.
   2261 // |default_from_prefs| is the default search provider from the preferences.
   2262 // Check |is_default_search_managed_| to determine if it was set by policy.
   2263 //
   2264 // This function removes from the vector and the database all the TemplateURLs
   2265 // that were set by policy, unless it is the current default search provider
   2266 // and matches what is set by a managed preference.
   2267 void TemplateURLService::RemoveProvidersCreatedByPolicy(
   2268     TemplateURLVector* template_urls,
   2269     TemplateURL** default_search_provider,
   2270     TemplateURL* default_from_prefs) {
   2271   DCHECK(template_urls);
   2272   DCHECK(default_search_provider);
   2273   for (TemplateURLVector::iterator i = template_urls->begin();
   2274        i != template_urls->end(); ) {
   2275     TemplateURL* template_url = *i;
   2276     if (template_url->created_by_policy()) {
   2277       if (template_url == *default_search_provider &&
   2278           is_default_search_managed_ &&
   2279           TemplateURLsHaveSamePrefs(template_url,
   2280                                     default_from_prefs)) {
   2281         // If the database specified a default search provider that was set
   2282         // by policy, and the default search provider from the preferences
   2283         // is also set by policy and they are the same, keep the entry in the
   2284         // database and the |default_search_provider|.
   2285         ++i;
   2286         continue;
   2287       }
   2288 
   2289       // The database loaded a managed |default_search_provider|, but it has
   2290       // been updated in the prefs. Remove it from the database, and update the
   2291       // |default_search_provider| pointer here.
   2292       if (*default_search_provider &&
   2293           (*default_search_provider)->id() == template_url->id())
   2294         *default_search_provider = NULL;
   2295 
   2296       i = template_urls->erase(i);
   2297       if (service_.get())
   2298         service_->RemoveKeyword(template_url->id());
   2299       delete template_url;
   2300     } else {
   2301       ++i;
   2302     }
   2303   }
   2304 }
   2305 
   2306 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url,
   2307                                               const std::string& guid) {
   2308   DCHECK(loaded_);
   2309   DCHECK(!guid.empty());
   2310 
   2311   TemplateURLData data(url->data());
   2312   data.sync_guid = guid;
   2313   TemplateURL new_url(url->profile(), data);
   2314   UIThreadSearchTermsData search_terms_data(url->profile());
   2315   UpdateNoNotify(url, new_url, search_terms_data);
   2316 }
   2317 
   2318 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl,
   2319                                              bool force) {
   2320   if (!force) {
   2321     // Already unique.
   2322     if (!GetTemplateURLForKeyword(turl.keyword()))
   2323       return turl.keyword();
   2324 
   2325     // First, try to return the generated keyword for the TemplateURL (except
   2326     // for extensions, as their keywords are not associated with their URLs).
   2327     GURL gurl(turl.url());
   2328     if (gurl.is_valid() && !turl.IsExtensionKeyword()) {
   2329       string16 keyword_candidate = GenerateKeyword(gurl);
   2330       if (!GetTemplateURLForKeyword(keyword_candidate))
   2331         return keyword_candidate;
   2332     }
   2333   }
   2334 
   2335   // We try to uniquify the keyword by appending a special character to the end.
   2336   // This is a best-effort approach where we try to preserve the original
   2337   // keyword and let the user do what they will after our attempt.
   2338   string16 keyword_candidate(turl.keyword());
   2339   do {
   2340     keyword_candidate.append(ASCIIToUTF16("_"));
   2341   } while (GetTemplateURLForKeyword(keyword_candidate));
   2342 
   2343   return keyword_candidate;
   2344 }
   2345 
   2346 bool TemplateURLService::IsLocalTemplateURLBetter(
   2347     const TemplateURL* local_turl,
   2348     const TemplateURL* sync_turl) {
   2349   DCHECK(GetTemplateURLForGUID(local_turl->sync_guid()));
   2350   return local_turl->last_modified() > sync_turl->last_modified() ||
   2351          local_turl->created_by_policy() ||
   2352          local_turl== GetDefaultSearchProvider();
   2353 }
   2354 
   2355 void TemplateURLService::ResolveSyncKeywordConflict(
   2356     TemplateURL* unapplied_sync_turl,
   2357     TemplateURL* applied_sync_turl,
   2358     syncer::SyncChangeList* change_list) {
   2359   DCHECK(loaded_);
   2360   DCHECK(unapplied_sync_turl);
   2361   DCHECK(applied_sync_turl);
   2362   DCHECK(change_list);
   2363   DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword());
   2364 
   2365   // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so
   2366   // don't delete either of them. Instead, determine which is "better" and
   2367   // uniquify the other one, sending an update to the server for the updated
   2368   // entry.
   2369   const bool applied_turl_is_better =
   2370       IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl);
   2371   TemplateURL* loser = applied_turl_is_better ?
   2372       unapplied_sync_turl : applied_sync_turl;
   2373   string16 new_keyword = UniquifyKeyword(*loser, false);
   2374   DCHECK(!GetTemplateURLForKeyword(new_keyword));
   2375   if (applied_turl_is_better) {
   2376     // Just set the keyword of |unapplied_sync_turl|. The caller is responsible
   2377     // for adding or updating unapplied_sync_turl in the local model.
   2378     unapplied_sync_turl->data_.SetKeyword(new_keyword);
   2379   } else {
   2380     // Update |applied_sync_turl| in the local model with the new keyword.
   2381     TemplateURLData data(applied_sync_turl->data());
   2382     data.SetKeyword(new_keyword);
   2383     TemplateURL new_turl(applied_sync_turl->profile(), data);
   2384     UIThreadSearchTermsData search_terms_data(applied_sync_turl->profile());
   2385     if (UpdateNoNotify(applied_sync_turl, new_turl, search_terms_data))
   2386       NotifyObservers();
   2387   }
   2388   // The losing TemplateURL should have their keyword updated. Send a change to
   2389   // the server to reflect this change.
   2390   syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*loser);
   2391   change_list->push_back(syncer::SyncChange(FROM_HERE,
   2392       syncer::SyncChange::ACTION_UPDATE,
   2393       sync_data));
   2394 }
   2395 
   2396 void TemplateURLService::MergeInSyncTemplateURL(
   2397     TemplateURL* sync_turl,
   2398     const SyncDataMap& sync_data,
   2399     syncer::SyncChangeList* change_list,
   2400     SyncDataMap* local_data,
   2401     syncer::SyncMergeResult* merge_result) {
   2402   DCHECK(sync_turl);
   2403   DCHECK(!GetTemplateURLForGUID(sync_turl->sync_guid()));
   2404   DCHECK(IsFromSync(sync_turl, sync_data));
   2405 
   2406   TemplateURL* conflicting_turl =
   2407       FindNonExtensionTemplateURLForKeyword(sync_turl->keyword());
   2408   bool should_add_sync_turl = true;
   2409 
   2410   // If there was no TemplateURL in the local model that conflicts with
   2411   // |sync_turl|, skip the following preparation steps and just add |sync_turl|
   2412   // directly. Otherwise, modify |conflicting_turl| to make room for
   2413   // |sync_turl|.
   2414   if (conflicting_turl) {
   2415     if (IsFromSync(conflicting_turl, sync_data)) {
   2416       // |conflicting_turl| is already known to Sync, so we're not allowed to
   2417       // remove it. In this case, we want to uniquify the worse one and send an
   2418       // update for the changed keyword to sync. We can reuse the logic from
   2419       // ResolveSyncKeywordConflict for this.
   2420       ResolveSyncKeywordConflict(sync_turl, conflicting_turl, change_list);
   2421       merge_result->set_num_items_modified(
   2422           merge_result->num_items_modified() + 1);
   2423     } else {
   2424       // |conflicting_turl| is not yet known to Sync. If it is better, then we
   2425       // want to transfer its values up to sync. Otherwise, we remove it and
   2426       // allow the entry from Sync to overtake it in the model.
   2427       const std::string guid = conflicting_turl->sync_guid();
   2428       if (IsLocalTemplateURLBetter(conflicting_turl, sync_turl)) {
   2429         ResetTemplateURLGUID(conflicting_turl, sync_turl->sync_guid());
   2430         syncer::SyncData sync_data =
   2431             CreateSyncDataFromTemplateURL(*conflicting_turl);
   2432         change_list->push_back(syncer::SyncChange(
   2433             FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data));
   2434         if (conflicting_turl == GetDefaultSearchProvider() &&
   2435             !pending_synced_default_search_) {
   2436           // If we're not waiting for the Synced default to come in, we should
   2437           // override the pref with our new GUID. If we are waiting for the
   2438           // arrival of a synced default, setting the pref here would cause us
   2439           // to lose the GUID we are waiting on.
   2440           PrefService* prefs = GetPrefs();
   2441           if (prefs) {
   2442             prefs->SetString(prefs::kSyncedDefaultSearchProviderGUID,
   2443                              conflicting_turl->sync_guid());
   2444           }
   2445         }
   2446         // Note that in this case we do not add the Sync TemplateURL to the
   2447         // local model, since we've effectively "merged" it in by updating the
   2448         // local conflicting entry with its sync_guid.
   2449         should_add_sync_turl = false;
   2450         merge_result->set_num_items_modified(
   2451             merge_result->num_items_modified() + 1);
   2452       } else {
   2453         // We guarantee that this isn't the local search provider. Otherwise,
   2454         // local would have won.
   2455         DCHECK(conflicting_turl != GetDefaultSearchProvider());
   2456         Remove(conflicting_turl);
   2457         merge_result->set_num_items_deleted(
   2458             merge_result->num_items_deleted() + 1);
   2459       }
   2460       // This TemplateURL was either removed or overwritten in the local model.
   2461       // Remove the entry from the local data so it isn't pushed up to Sync.
   2462       local_data->erase(guid);
   2463     }
   2464   }
   2465 
   2466   if (should_add_sync_turl) {
   2467     const std::string guid = sync_turl->sync_guid();
   2468     // Force the local ID to kInvalidTemplateURLID so we can add it.
   2469     TemplateURLData data(sync_turl->data());
   2470     data.id = kInvalidTemplateURLID;
   2471     Add(new TemplateURL(profile_, data));
   2472     merge_result->set_num_items_added(
   2473         merge_result->num_items_added() + 1);
   2474   }
   2475 }
   2476 
   2477 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced(
   2478     const std::string& guid) {
   2479   // If we're not syncing or if default search is managed by policy, ignore.
   2480   if (!sync_processor_.get() || is_default_search_managed_)
   2481     return;
   2482 
   2483   PrefService* prefs = GetPrefs();
   2484   if (prefs && pending_synced_default_search_ &&
   2485       prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID) == guid) {
   2486     // Make sure this actually exists. We should not be calling this unless we
   2487     // really just added this TemplateURL.
   2488     TemplateURL* turl_from_sync = GetTemplateURLForGUID(guid);
   2489     if (turl_from_sync && turl_from_sync->SupportsReplacement()) {
   2490       base::AutoReset<DefaultSearchChangeOrigin> change_origin(
   2491           &dsp_change_origin_, DSP_CHANGE_SYNC_ADD);
   2492       SetDefaultSearchProvider(turl_from_sync);
   2493     }
   2494     pending_synced_default_search_ = false;
   2495   }
   2496 }
   2497 
   2498 TemplateURL* TemplateURLService::GetPendingSyncedDefaultSearchProvider() {
   2499   PrefService* prefs = GetPrefs();
   2500   if (!prefs || !pending_synced_default_search_)
   2501     return NULL;
   2502 
   2503   // Could be NULL if no such thing exists.
   2504   return GetTemplateURLForGUID(
   2505       prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
   2506 }
   2507 
   2508 void TemplateURLService::PatchMissingSyncGUIDs(
   2509     TemplateURLVector* template_urls) {
   2510   DCHECK(template_urls);
   2511   for (TemplateURLVector::iterator i = template_urls->begin();
   2512        i != template_urls->end(); ++i) {
   2513     TemplateURL* template_url = *i;
   2514     DCHECK(template_url);
   2515     if (template_url->sync_guid().empty()) {
   2516       template_url->data_.sync_guid = base::GenerateGUID();
   2517       if (service_.get())
   2518         service_->UpdateKeyword(template_url->data());
   2519     }
   2520   }
   2521 }
   2522 
   2523 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine(
   2524     TemplateURLVector* template_urls,
   2525     TemplateURL* default_search_provider) {
   2526   DCHECK(template_urls);
   2527   is_default_search_managed_ = false;
   2528   bool database_specified_a_default = (default_search_provider != NULL);
   2529 
   2530   // Check if default search provider is now managed.
   2531   scoped_ptr<TemplateURL> default_from_prefs;
   2532   LoadDefaultSearchProviderFromPrefs(&default_from_prefs,
   2533                                      &is_default_search_managed_);
   2534 
   2535   // Remove entries that were created because of policy as they may have
   2536   // changed since the database was saved.
   2537   RemoveProvidersCreatedByPolicy(template_urls,
   2538                                  &default_search_provider,
   2539                                  default_from_prefs.get());
   2540 
   2541   PatchMissingSyncGUIDs(template_urls);
   2542 
   2543   if (is_default_search_managed_) {
   2544     SetTemplateURLs(template_urls);
   2545 
   2546     if (TemplateURLsHaveSamePrefs(default_search_provider,
   2547                                   default_from_prefs.get())) {
   2548       // The value from the preferences was previously stored in the database.
   2549       // Reuse it.
   2550     } else {
   2551       // The value from the preferences takes over.
   2552       default_search_provider = NULL;
   2553       if (default_from_prefs.get()) {
   2554         TemplateURLData data(default_from_prefs->data());
   2555         data.created_by_policy = true;
   2556         data.id = kInvalidTemplateURLID;
   2557         default_search_provider = new TemplateURL(profile_, data);
   2558         if (!AddNoNotify(default_search_provider, true))
   2559           default_search_provider = NULL;
   2560       }
   2561     }
   2562     // Note that this saves the default search provider to prefs.
   2563     if (!default_search_provider ||
   2564         (!default_search_provider->IsExtensionKeyword() &&
   2565             default_search_provider->SupportsReplacement())) {
   2566       bool success = SetDefaultSearchProviderNoNotify(default_search_provider);
   2567       DCHECK(success);
   2568     }
   2569   } else {
   2570     // If we had a managed default, replace it with the synced default if
   2571     // applicable, or the first provider of the list.
   2572     TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider();
   2573     if (synced_default) {
   2574       default_search_provider = synced_default;
   2575       pending_synced_default_search_ = false;
   2576     } else if (database_specified_a_default &&
   2577         default_search_provider == NULL) {
   2578       UMA_HISTOGRAM_ENUMERATION(kFirstPotentialEngineHistogramName,
   2579                                 FIRST_POTENTIAL_CALLSITE_ON_LOAD,
   2580                                 FIRST_POTENTIAL_CALLSITE_MAX);
   2581       default_search_provider = FirstPotentialDefaultEngine(*template_urls);
   2582     }
   2583 
   2584     // If the default search provider existed previously, then just
   2585     // set the member variable. Otherwise, we'll set it using the method
   2586     // to ensure that it is saved properly after its id is set.
   2587     if (default_search_provider &&
   2588         (default_search_provider->id() != kInvalidTemplateURLID)) {
   2589       default_search_provider_ = default_search_provider;
   2590       default_search_provider = NULL;
   2591     }
   2592     SetTemplateURLs(template_urls);
   2593 
   2594     if (default_search_provider) {
   2595       // Note that this saves the default search provider to prefs.
   2596       SetDefaultSearchProvider(default_search_provider);
   2597     } else {
   2598       // Always save the default search provider to prefs. That way we don't
   2599       // have to worry about it being out of sync.
   2600       if (default_search_provider_)
   2601         SaveDefaultSearchProviderToPrefs(default_search_provider_);
   2602     }
   2603   }
   2604 }
   2605 
   2606 void TemplateURLService::EnsureDefaultSearchProviderExists() {
   2607   if (!is_default_search_managed_) {
   2608     bool has_default_search_provider = default_search_provider_ &&
   2609         default_search_provider_->SupportsReplacement();
   2610     UMA_HISTOGRAM_BOOLEAN("Search.HasDefaultSearchProvider",
   2611                           has_default_search_provider);
   2612     // Ensure that default search provider exists. See http://crbug.com/116952.
   2613     if (!has_default_search_provider) {
   2614       bool success =
   2615           SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider());
   2616       DCHECK(success);
   2617     }
   2618     // Don't log anything if the user has a NULL default search provider.
   2619     if (default_search_provider_) {
   2620       // TODO(pkasting): This histogram obsoletes the next one.  Remove the next
   2621       // one in Chrome 32 or later.
   2622       UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchProviderType",
   2623           TemplateURLPrepopulateData::GetEngineType(*default_search_provider_),
   2624           SEARCH_ENGINE_MAX);
   2625       UMA_HISTOGRAM_ENUMERATION(
   2626           "Search.DefaultSearchProvider",
   2627           default_search_provider_->prepopulate_id(),
   2628           TemplateURLPrepopulateData::kMaxPrepopulatedEngineID);
   2629     }
   2630   }
   2631 }
   2632 
   2633 TemplateURL* TemplateURLService::CreateTemplateURLForExtension(
   2634     const ExtensionKeyword& extension_keyword) const {
   2635   TemplateURLData data;
   2636   data.short_name = UTF8ToUTF16(extension_keyword.extension_name);
   2637   data.SetKeyword(UTF8ToUTF16(extension_keyword.extension_keyword));
   2638   // This URL is not actually used for navigation. It holds the extension's
   2639   // ID, as well as forcing the TemplateURL to be treated as a search keyword.
   2640   data.SetURL(std::string(extensions::kExtensionScheme) + "://" +
   2641       extension_keyword.extension_id + "/?q={searchTerms}");
   2642   return new TemplateURL(profile_, data);
   2643 }
   2644