| Index: chrome/browser/search_engines/template_url_model.cc
|
| diff --git a/chrome/browser/search_engines/template_url_model.cc b/chrome/browser/search_engines/template_url_model.cc
|
| index 76a22f538bc7ba02457734b0e01d36491d03b7eb..a7f1a4a8776ed1cc0de164876bdb7946de322112 100644
|
| --- a/chrome/browser/search_engines/template_url_model.cc
|
| +++ b/chrome/browser/search_engines/template_url_model.cc
|
| @@ -94,53 +94,6 @@ TemplateURLModel::~TemplateURLModel() {
|
| STLDeleteElements(&template_urls_);
|
| }
|
|
|
| -void TemplateURLModel::Init(const Initializer* initializers,
|
| - int num_initializers) {
|
| - // Register for notifications.
|
| - if (profile_) {
|
| - // TODO(sky): bug 1166191. The keywords should be moved into the history
|
| - // db, which will mean we no longer need this notification and the history
|
| - // backend can handle automatically adding the search terms as the user
|
| - // navigates.
|
| - registrar_.Add(this, NotificationType::HISTORY_URL_VISITED,
|
| - Source<Profile>(profile_->GetOriginalProfile()));
|
| - }
|
| - registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
|
| - NotificationService::AllSources());
|
| -
|
| - // Add specific initializers, if any.
|
| - for (int i(0); i < num_initializers; ++i) {
|
| - DCHECK(initializers[i].keyword);
|
| - DCHECK(initializers[i].url);
|
| - DCHECK(initializers[i].content);
|
| -
|
| - size_t template_position =
|
| - std::string(initializers[i].url).find(kTemplateParameter);
|
| - DCHECK(template_position != std::wstring::npos);
|
| - std::string osd_url(initializers[i].url);
|
| - osd_url.replace(template_position, arraysize(kTemplateParameter) - 1,
|
| - kSearchTermParameter);
|
| -
|
| - // TemplateURLModel ends up owning the TemplateURL, don't try and free it.
|
| - TemplateURL* template_url = new TemplateURL();
|
| - template_url->set_keyword(initializers[i].keyword);
|
| - template_url->set_short_name(initializers[i].content);
|
| - template_url->SetURL(osd_url, 0, 0);
|
| - Add(template_url);
|
| - }
|
| -
|
| - // Request a server check for the correct Google URL if Google is the default
|
| - // search engine, not in headless mode and not in Chrome Frame.
|
| - const TemplateURL* default_provider = GetDefaultSearchProvider();
|
| - scoped_ptr<base::Environment> env(base::Environment::Create());
|
| - if (default_provider && !env->HasVar(env_vars::kHeadless) &&
|
| - !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) {
|
| - const TemplateURLRef* default_provider_ref = default_provider->url();
|
| - if (default_provider_ref && default_provider_ref->HasGoogleBaseURLs())
|
| - GoogleURLTracker::RequestServerCheck();
|
| - }
|
| -}
|
| -
|
| // static
|
| std::wstring TemplateURLModel::GenerateKeyword(const GURL& url,
|
| bool autodetected) {
|
| @@ -291,15 +244,6 @@ void TemplateURLModel::Add(TemplateURL* template_url) {
|
| }
|
| }
|
|
|
| -void TemplateURLModel::AddToMaps(const TemplateURL* template_url) {
|
| - if (!template_url->keyword().empty())
|
| - keyword_to_template_map_[template_url->keyword()] = template_url;
|
| -
|
| - const GURL url(GenerateSearchURL(template_url));
|
| - if (url.is_valid() && url.has_host())
|
| - host_to_urls_map_[url.host()].insert(template_url);
|
| -}
|
| -
|
| void TemplateURLModel::Remove(const TemplateURL* template_url) {
|
| TemplateURLVector::iterator i = find(template_urls_.begin(),
|
| template_urls_.end(),
|
| @@ -337,6 +281,20 @@ void TemplateURLModel::Remove(const TemplateURL* template_url) {
|
| delete template_url;
|
| }
|
|
|
| +void TemplateURLModel::RemoveAutoGeneratedBetween(Time created_after,
|
| + Time created_before) {
|
| + for (size_t i = 0; i < template_urls_.size();) {
|
| + if (template_urls_[i]->date_created() >= created_after &&
|
| + (created_before.is_null() ||
|
| + template_urls_[i]->date_created() < created_before) &&
|
| + CanReplace(template_urls_[i])) {
|
| + Remove(template_urls_[i]);
|
| + } else {
|
| + ++i;
|
| + }
|
| + }
|
| +}
|
| +
|
| void TemplateURLModel::Replace(const TemplateURL* existing_turl,
|
| TemplateURL* new_turl) {
|
| DCHECK(existing_turl && new_turl);
|
| @@ -367,88 +325,58 @@ void TemplateURLModel::Replace(const TemplateURL* existing_turl,
|
| delete existing_turl;
|
| }
|
|
|
| -void TemplateURLModel::RemoveAutoGeneratedBetween(Time created_after,
|
| - Time created_before) {
|
| - for (size_t i = 0; i < template_urls_.size();) {
|
| - if (template_urls_[i]->date_created() >= created_after &&
|
| - (created_before.is_null() ||
|
| - template_urls_[i]->date_created() < created_before) &&
|
| - CanReplace(template_urls_[i])) {
|
| - Remove(template_urls_[i]);
|
| - } else {
|
| - ++i;
|
| - }
|
| - }
|
| -}
|
| -
|
| void TemplateURLModel::RemoveAutoGeneratedSince(Time created_after) {
|
| RemoveAutoGeneratedBetween(created_after, Time());
|
| }
|
|
|
| -void TemplateURLModel::SetKeywordSearchTermsForURL(const TemplateURL* t_url,
|
| - const GURL& url,
|
| - const std::wstring& term) {
|
| - HistoryService* history = profile_ ?
|
| - profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
|
| - if (!history)
|
| +void TemplateURLModel::RegisterExtensionKeyword(Extension* extension) {
|
| + // TODO(mpcomplete): disable the keyword when the extension is disabled.
|
| + if (extension->omnibox_keyword().empty())
|
| return;
|
| - history->SetKeywordSearchTermsForURL(url, t_url->id(),
|
| - WideToUTF16Hack(term));
|
| -}
|
|
|
| -void TemplateURLModel::RemoveFromMaps(const TemplateURL* template_url) {
|
| - if (!template_url->keyword().empty()) {
|
| - keyword_to_template_map_.erase(template_url->keyword());
|
| + Load();
|
| + if (!loaded_) {
|
| + pending_extension_ids_.push_back(extension->id());
|
| + return;
|
| }
|
|
|
| - const GURL url(GenerateSearchURL(template_url));
|
| - if (url.is_valid() && url.has_host()) {
|
| - const std::string host(url.host());
|
| - DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end());
|
| - TemplateURLSet& urls = host_to_urls_map_[host];
|
| - DCHECK(urls.find(template_url) != urls.end());
|
| - urls.erase(urls.find(template_url));
|
| - if (urls.empty())
|
| - host_to_urls_map_.erase(host_to_urls_map_.find(host));
|
| - }
|
| -}
|
| + const TemplateURL* existing_url = GetTemplateURLForExtension(extension);
|
| + std::wstring keyword = UTF8ToWide(extension->omnibox_keyword());
|
|
|
| -void TemplateURLModel::RemoveFromMapsByPointer(
|
| - const TemplateURL* template_url) {
|
| - DCHECK(template_url);
|
| - for (KeywordToTemplateMap::iterator i = keyword_to_template_map_.begin();
|
| - i != keyword_to_template_map_.end(); ++i) {
|
| - if (i->second == template_url) {
|
| - keyword_to_template_map_.erase(i);
|
| - // A given TemplateURL only occurs once in the map. As soon as we find the
|
| - // entry, stop.
|
| - break;
|
| - }
|
| - }
|
| + TemplateURL* template_url = new TemplateURL;
|
| + template_url->set_short_name(UTF8ToWide(extension->name()));
|
| + template_url->set_keyword(keyword);
|
| + // This URL is not actually used for navigation. It holds the extension's
|
| + // ID, as well as forcing the TemplateURL to be treated as a search keyword.
|
| + template_url->SetURL(
|
| + std::string(chrome::kExtensionScheme) + "://" +
|
| + extension->id() + "/?q={searchTerms}", 0, 0);
|
| + template_url->set_safe_for_autoreplace(false);
|
|
|
| - for (HostToURLsMap::iterator i = host_to_urls_map_.begin();
|
| - i != host_to_urls_map_.end(); ++i) {
|
| - TemplateURLSet::iterator url_set_iterator = i->second.find(template_url);
|
| - if (url_set_iterator != i->second.end()) {
|
| - i->second.erase(url_set_iterator);
|
| - if (i->second.empty())
|
| - host_to_urls_map_.erase(i);
|
| - // A given TemplateURL only occurs once in the map. As soon as we find the
|
| - // entry, stop.
|
| - return;
|
| - }
|
| + if (existing_url) {
|
| + // TODO(mpcomplete): only replace if the user hasn't changed the keyword.
|
| + // (We don't have UI for that yet).
|
| + Replace(existing_url, template_url);
|
| + } else {
|
| + Add(template_url);
|
| }
|
| }
|
|
|
| -void TemplateURLModel::SetTemplateURLs(
|
| - const std::vector<const TemplateURL*>& urls) {
|
| - // Add mappings for the new items.
|
| - for (TemplateURLVector::const_iterator i = urls.begin(); i != urls.end();
|
| - ++i) {
|
| - next_id_ = std::max(next_id_, (*i)->id());
|
| - AddToMaps(*i);
|
| - template_urls_.push_back(*i);
|
| +void TemplateURLModel::UnregisterExtensionKeyword(Extension* extension) {
|
| + const TemplateURL* url = GetTemplateURLForExtension(extension);
|
| + if (url)
|
| + Remove(url);
|
| +}
|
| +
|
| +const TemplateURL* TemplateURLModel::GetTemplateURLForExtension(
|
| + Extension* extension) const {
|
| + for (TemplateURLVector::const_iterator i = template_urls_.begin();
|
| + i != template_urls_.end(); ++i) {
|
| + if ((*i)->IsExtensionKeyword() && (*i)->url()->GetHost() == extension->id())
|
| + return *i;
|
| }
|
| +
|
| + return NULL;
|
| }
|
|
|
| std::vector<const TemplateURL*> TemplateURLModel::GetTemplateURLs() const {
|
| @@ -711,6 +639,128 @@ void TemplateURLModel::Observe(NotificationType type,
|
| }
|
| }
|
|
|
| +void TemplateURLModel::SetKeywordSearchTermsForURL(const TemplateURL* t_url,
|
| + const GURL& url,
|
| + const std::wstring& term) {
|
| + HistoryService* history = profile_ ?
|
| + profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
|
| + if (!history)
|
| + return;
|
| + history->SetKeywordSearchTermsForURL(url, t_url->id(),
|
| + WideToUTF16Hack(term));
|
| +}
|
| +
|
| +void TemplateURLModel::Init(const Initializer* initializers,
|
| + int num_initializers) {
|
| + // Register for notifications.
|
| + if (profile_) {
|
| + // TODO(sky): bug 1166191. The keywords should be moved into the history
|
| + // db, which will mean we no longer need this notification and the history
|
| + // backend can handle automatically adding the search terms as the user
|
| + // navigates.
|
| + registrar_.Add(this, NotificationType::HISTORY_URL_VISITED,
|
| + Source<Profile>(profile_->GetOriginalProfile()));
|
| + }
|
| + registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
|
| + NotificationService::AllSources());
|
| +
|
| + // Add specific initializers, if any.
|
| + for (int i(0); i < num_initializers; ++i) {
|
| + DCHECK(initializers[i].keyword);
|
| + DCHECK(initializers[i].url);
|
| + DCHECK(initializers[i].content);
|
| +
|
| + size_t template_position =
|
| + std::string(initializers[i].url).find(kTemplateParameter);
|
| + DCHECK(template_position != std::wstring::npos);
|
| + std::string osd_url(initializers[i].url);
|
| + osd_url.replace(template_position, arraysize(kTemplateParameter) - 1,
|
| + kSearchTermParameter);
|
| +
|
| + // TemplateURLModel ends up owning the TemplateURL, don't try and free it.
|
| + TemplateURL* template_url = new TemplateURL();
|
| + template_url->set_keyword(initializers[i].keyword);
|
| + template_url->set_short_name(initializers[i].content);
|
| + template_url->SetURL(osd_url, 0, 0);
|
| + Add(template_url);
|
| + }
|
| +
|
| + // Request a server check for the correct Google URL if Google is the default
|
| + // search engine, not in headless mode and not in Chrome Frame.
|
| + const TemplateURL* default_provider = GetDefaultSearchProvider();
|
| + scoped_ptr<base::Environment> env(base::Environment::Create());
|
| + if (default_provider && !env->HasVar(env_vars::kHeadless) &&
|
| + !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) {
|
| + const TemplateURLRef* default_provider_ref = default_provider->url();
|
| + if (default_provider_ref && default_provider_ref->HasGoogleBaseURLs())
|
| + GoogleURLTracker::RequestServerCheck();
|
| + }
|
| +}
|
| +
|
| +void TemplateURLModel::RemoveFromMaps(const TemplateURL* template_url) {
|
| + if (!template_url->keyword().empty()) {
|
| + keyword_to_template_map_.erase(template_url->keyword());
|
| + }
|
| +
|
| + const GURL url(GenerateSearchURL(template_url));
|
| + if (url.is_valid() && url.has_host()) {
|
| + const std::string host(url.host());
|
| + DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end());
|
| + TemplateURLSet& urls = host_to_urls_map_[host];
|
| + DCHECK(urls.find(template_url) != urls.end());
|
| + urls.erase(urls.find(template_url));
|
| + if (urls.empty())
|
| + host_to_urls_map_.erase(host_to_urls_map_.find(host));
|
| + }
|
| +}
|
| +
|
| +void TemplateURLModel::RemoveFromMapsByPointer(
|
| + const TemplateURL* template_url) {
|
| + DCHECK(template_url);
|
| + for (KeywordToTemplateMap::iterator i = keyword_to_template_map_.begin();
|
| + i != keyword_to_template_map_.end(); ++i) {
|
| + if (i->second == template_url) {
|
| + keyword_to_template_map_.erase(i);
|
| + // A given TemplateURL only occurs once in the map. As soon as we find the
|
| + // entry, stop.
|
| + break;
|
| + }
|
| + }
|
| +
|
| + for (HostToURLsMap::iterator i = host_to_urls_map_.begin();
|
| + i != host_to_urls_map_.end(); ++i) {
|
| + TemplateURLSet::iterator url_set_iterator = i->second.find(template_url);
|
| + if (url_set_iterator != i->second.end()) {
|
| + i->second.erase(url_set_iterator);
|
| + if (i->second.empty())
|
| + host_to_urls_map_.erase(i);
|
| + // A given TemplateURL only occurs once in the map. As soon as we find the
|
| + // entry, stop.
|
| + return;
|
| + }
|
| + }
|
| +}
|
| +
|
| +void TemplateURLModel::AddToMaps(const TemplateURL* template_url) {
|
| + if (!template_url->keyword().empty())
|
| + keyword_to_template_map_[template_url->keyword()] = template_url;
|
| +
|
| + const GURL url(GenerateSearchURL(template_url));
|
| + if (url.is_valid() && url.has_host())
|
| + host_to_urls_map_[url.host()].insert(template_url);
|
| +}
|
| +
|
| +void TemplateURLModel::SetTemplateURLs(
|
| + const std::vector<const TemplateURL*>& urls) {
|
| + // Add mappings for the new items.
|
| + for (TemplateURLVector::const_iterator i = urls.begin(); i != urls.end();
|
| + ++i) {
|
| + next_id_ = std::max(next_id_, (*i)->id());
|
| + AddToMaps(*i);
|
| + template_urls_.push_back(*i);
|
| + }
|
| +}
|
| +
|
| void TemplateURLModel::DeleteGeneratedKeywordsMatchingHost(
|
| const std::wstring& host) {
|
| const std::wstring host_slash = host + L"/";
|
| @@ -1069,53 +1119,3 @@ void TemplateURLModel::GoogleBaseURLChanged() {
|
| OnTemplateURLModelChanged());
|
| }
|
| }
|
| -
|
| -void TemplateURLModel::RegisterExtensionKeyword(Extension* extension) {
|
| - // TODO(mpcomplete): disable the keyword when the extension is disabled.
|
| - if (extension->omnibox_keyword().empty())
|
| - return;
|
| -
|
| - Load();
|
| - if (!loaded_) {
|
| - pending_extension_ids_.push_back(extension->id());
|
| - return;
|
| - }
|
| -
|
| - const TemplateURL* existing_url = GetTemplateURLForExtension(extension);
|
| - std::wstring keyword = UTF8ToWide(extension->omnibox_keyword());
|
| -
|
| - TemplateURL* template_url = new TemplateURL;
|
| - template_url->set_short_name(UTF8ToWide(extension->name()));
|
| - template_url->set_keyword(keyword);
|
| - // This URL is not actually used for navigation. It holds the extension's
|
| - // ID, as well as forcing the TemplateURL to be treated as a search keyword.
|
| - template_url->SetURL(
|
| - std::string(chrome::kExtensionScheme) + "://" +
|
| - extension->id() + "/?q={searchTerms}", 0, 0);
|
| - template_url->set_safe_for_autoreplace(false);
|
| -
|
| - if (existing_url) {
|
| - // TODO(mpcomplete): only replace if the user hasn't changed the keyword.
|
| - // (We don't have UI for that yet).
|
| - Replace(existing_url, template_url);
|
| - } else {
|
| - Add(template_url);
|
| - }
|
| -}
|
| -
|
| -void TemplateURLModel::UnregisterExtensionKeyword(Extension* extension) {
|
| - const TemplateURL* url = GetTemplateURLForExtension(extension);
|
| - if (url)
|
| - Remove(url);
|
| -}
|
| -
|
| -const TemplateURL* TemplateURLModel::GetTemplateURLForExtension(
|
| - Extension* extension) const {
|
| - for (TemplateURLVector::const_iterator i = template_urls_.begin();
|
| - i != template_urls_.end(); ++i) {
|
| - if ((*i)->IsExtensionKeyword() && (*i)->url()->GetHost() == extension->id())
|
| - return *i;
|
| - }
|
| -
|
| - return NULL;
|
| -}
|
|
|