diff options
author | levin@chromium.org <levin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-29 17:16:13 +0000 |
---|---|---|
committer | levin@chromium.org <levin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-29 17:16:13 +0000 |
commit | e36ee2522d4c0e980807c39403deafdc7739e9f1 (patch) | |
tree | 848deff63bcb353cf6142fe340320745250a87e8 /chrome/browser | |
parent | c679be260c4a87f9daad3ab3110bf44395732f8b (diff) | |
download | chromium_src-e36ee2522d4c0e980807c39403deafdc7739e9f1.zip chromium_src-e36ee2522d4c0e980807c39403deafdc7739e9f1.tar.gz chromium_src-e36ee2522d4c0e980807c39403deafdc7739e9f1.tar.bz2 |
Add code to get the search provider install state.
BUG=38475
TEST=unit_test --gtest_filter=SearchHost*
Review URL: http://codereview.chromium.org/3269001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57815 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
4 files changed, 130 insertions, 8 deletions
diff --git a/chrome/browser/search_engines/search_host_to_urls_map.cc b/chrome/browser/search_engines/search_host_to_urls_map.cc index ea24c72..3fc228b 100644 --- a/chrome/browser/search_engines/search_host_to_urls_map.cc +++ b/chrome/browser/search_engines/search_host_to_urls_map.cc @@ -10,6 +10,20 @@ #include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url_model.h" +// Indicates if the two inputs have the same security origin. +// |requested_origin| should only be a security origin (no path, etc.). +// It is ok if |template_url| is NULL. +static bool IsSameOrigin(const GURL& requested_origin, + const TemplateURL* template_url) { + // TODO(levin): This isn't safe for the I/O thread yet. + DCHECK(!ChromeThread::IsWellKnownThread(ChromeThread::UI) || + ChromeThread::CurrentlyOn(ChromeThread::UI)); + + DCHECK(requested_origin == requested_origin.GetOrigin()); + return template_url && requested_origin == + TemplateURLModel::GenerateSearchURL(template_url).GetOrigin(); +} + SearchHostToURLsMap::SearchHostToURLsMap() : initialized_(false) { } @@ -134,6 +148,34 @@ const SearchHostToURLsMap::TemplateURLSet* SearchHostToURLsMap::GetURLsForHost( return &urls_for_host->second; } +SearchProviderInstallData::State SearchHostToURLsMap::GetInstallState( + const GURL& requested_origin) { + AutoLock locker(lock_); + if (!initialized_) + return NOT_READY; + + // First check to see if the origin is the default search provider. + if (requested_origin.spec() == default_search_origin_) + return INSTALLED_AS_DEFAULT; + + // Is the url any search provider? + HostToURLsMap::const_iterator urls_for_host = + host_to_urls_map_.find(requested_origin.host()); + if (urls_for_host == host_to_urls_map_.end() || + urls_for_host->second.empty()) { + return NOT_INSTALLED; + } + + const TemplateURLSet& urls = urls_for_host->second; + for (TemplateURLSet::const_iterator i = urls.begin(); + i != urls.end(); ++i) { + const TemplateURL* template_url = *i; + if (IsSameOrigin(requested_origin, template_url)) + return INSTALLED_BUT_NOT_DEFAULT; + } + return NOT_INSTALLED; +} + SearchHostToURLsMap::~SearchHostToURLsMap() { } diff --git a/chrome/browser/search_engines/search_host_to_urls_map.h b/chrome/browser/search_engines/search_host_to_urls_map.h index 1a22ddc..90ae63d 100644 --- a/chrome/browser/search_engines/search_host_to_urls_map.h +++ b/chrome/browser/search_engines/search_host_to_urls_map.h @@ -15,6 +15,7 @@ #include "base/lock.h" #include "base/thread_checker.h" #include "base/ref_counted.h" +#include "chrome/browser/search_engines/search_provider_install_data.h" class FilePath; class GURL; @@ -23,13 +24,12 @@ class TemplateURL; class WebDataService; // Holds the host to template url mappings for the search providers. All public -// methods should happen from the same thread except for -// GetSearchProviderInstallState which may be invoked on any thread. -// WARNING: This class does not own any TemplateURLs passed to it and it is -// up to the caller to ensure the right lifetime of them. -// TODO(levin): Add GetSearchProviderInstallState. +// methods should happen from the same thread except for GetInstallState which +// may be invoked on any thread. WARNING: This class does not own any +// TemplateURLs passed to it and it is up to the caller to ensure the right +// lifetime of them. class SearchHostToURLsMap - : public base::RefCountedThreadSafe<SearchHostToURLsMap>, + : public SearchProviderInstallData, public ThreadChecker { public: typedef std::set<const TemplateURL*> TemplateURLSet; @@ -70,13 +70,17 @@ class SearchHostToURLsMap // none. const TemplateURLSet* GetURLsForHost(const std::string& host) const; + // Returns the search provider install state for the given origin. + // This method may be called on any thread. + // TODO(levin): Make the above statement true about "any thread". + virtual State GetInstallState(const GURL& requested_origin); + private: - friend class base::RefCountedThreadSafe<SearchHostToURLsMap>; friend class SearchHostToURLsMapTest; typedef std::map<std::string, TemplateURLSet> HostToURLsMap; - ~SearchHostToURLsMap(); + virtual ~SearchHostToURLsMap(); // Same as Add but the lock should already be taken. void AddNoLock(const TemplateURL* template_url); diff --git a/chrome/browser/search_engines/search_host_to_urls_map_unittest.cc b/chrome/browser/search_engines/search_host_to_urls_map_unittest.cc index f98fddb..52829d4 100644 --- a/chrome/browser/search_engines/search_host_to_urls_map_unittest.cc +++ b/chrome/browser/search_engines/search_host_to_urls_map_unittest.cc @@ -158,3 +158,38 @@ TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) { const TemplateURLSet* urls = provider_map_->GetURLsForHost("a" + host_); ASSERT_TRUE(urls == NULL); } + +TEST_F(SearchHostToURLsMapTest, GetInstallStateNotReady) { + scoped_refptr<SearchHostToURLsMap> not_init_map(new SearchHostToURLsMap); + ASSERT_EQ(SearchProviderInstallData::NOT_READY, + not_init_map->GetInstallState(GURL("http://" + host_ + "/"))); +} + +TEST_F(SearchHostToURLsMapTest, GetInstallStateNotDefault) { + ASSERT_EQ(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, + provider_map_->GetInstallState(GURL("http://" + host_ + "/"))); + ASSERT_EQ(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, + provider_map_->GetInstallState(GURL("http://" + host_ + ":80/"))); +} + +TEST_F(SearchHostToURLsMapTest, GetInstallStateNotInstalledDifferentPort) { + ASSERT_EQ(SearchProviderInstallData::NOT_INSTALLED, + provider_map_->GetInstallState(GURL("http://" + host_ + ":96/"))); +} + +TEST_F(SearchHostToURLsMapTest, GetInstallStateNotInstalledDifferentScheme) { + ASSERT_EQ(SearchProviderInstallData::NOT_INSTALLED, + provider_map_->GetInstallState(GURL("https://" + host_ + "/"))); +} + +TEST_F(SearchHostToURLsMapTest, GetInstallStateNotInstalled) { + ASSERT_EQ(SearchProviderInstallData::NOT_INSTALLED, + provider_map_->GetInstallState(GURL("http://a" + host_ + "/"))); +} + +TEST_F(SearchHostToURLsMapTest, GetInstallStateDefault) { + provider_map_->SetDefault(&t_urls_[0]); + ASSERT_EQ(SearchProviderInstallData::INSTALLED_AS_DEFAULT, + provider_map_->GetInstallState(GURL("http://" + host_ + "/"))); +} + diff --git a/chrome/browser/search_engines/search_provider_install_data.h b/chrome/browser/search_engines/search_provider_install_data.h new file mode 100644 index 0000000..02595db --- /dev/null +++ b/chrome/browser/search_engines/search_provider_install_data.h @@ -0,0 +1,41 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_ +#define CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_ + +#include "base/ref_counted.h" + +class GURL; + +// Provides the search provider install state for the I/O thread. +class SearchProviderInstallData + : public base::RefCountedThreadSafe<SearchProviderInstallData> { + public: + enum State { + // The install state cannot be determined at the moment. + NOT_READY = -2, + + // Equates to an access denied error. + DENIED = -1, + + // The search provider is not installed. + NOT_INSTALLED = 0, + + // The search provider is in the user's set but is not + INSTALLED_BUT_NOT_DEFAULT = 1, + + // The search provider is set as the user's default. + INSTALLED_AS_DEFAULT = 2 + }; + + // Returns the search provider install state for the given origin. + virtual State GetInstallState(const GURL& requested_origin) = 0; + + protected: + friend class base::RefCountedThreadSafe<SearchProviderInstallData>; + virtual ~SearchProviderInstallData() {} +}; + +#endif // CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_DATA_H_ |