| Index: chrome/browser/protector/default_search_provider_change.cc
|
| diff --git a/chrome/browser/protector/default_search_provider_change.cc b/chrome/browser/protector/default_search_provider_change.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..d5ddebbb67e4522c6677075f1aa22d4abe7b7703
|
| --- /dev/null
|
| +++ b/chrome/browser/protector/default_search_provider_change.cc
|
| @@ -0,0 +1,113 @@
|
| +// Copyright (c) 2011 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.
|
| +
|
| +#include "base/basictypes.h"
|
| +#include "base/compiler_specific.h"
|
| +#include "base/logging.h"
|
| +#include "chrome/browser/protector/protector.h"
|
| +#include "chrome/browser/protector/setting_change.h"
|
| +#include "chrome/browser/search_engines/template_url.h"
|
| +#include "chrome/browser/search_engines/template_url_service.h"
|
| +#include "chrome/browser/webdata/keyword_table.h"
|
| +#include "chrome/common/url_constants.h"
|
| +#include "googleurl/src/gurl.h"
|
| +
|
| +namespace protector {
|
| +
|
| +class DefaultSearchProviderChange : public SettingChange {
|
| + public:
|
| + DefaultSearchProviderChange(const TemplateURL* old_url,
|
| + const TemplateURL* new_url);
|
| +
|
| + // SettingChange overrides:
|
| + virtual string16 GetOldSetting() const OVERRIDE;
|
| + virtual string16 GetNewSetting() const OVERRIDE;
|
| + virtual void Accept(Protector* protector) OVERRIDE;
|
| + virtual void Revert(Protector* protector) OVERRIDE;
|
| + virtual void DoDefault(Protector* protector) OVERRIDE;
|
| +
|
| + private:
|
| + ~DefaultSearchProviderChange();
|
| +
|
| + // Sets the given default search provider to profile that |protector| is
|
| + // guarding.
|
| + void SetDefaultSearchProvider(Protector* protector, const TemplateURL* url);
|
| +
|
| + const TemplateURL* old_url_;
|
| + const TemplateURL* new_url_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange);
|
| +};
|
| +
|
| +DefaultSearchProviderChange::DefaultSearchProviderChange(
|
| + const TemplateURL* old_url,
|
| + const TemplateURL* new_url)
|
| + : SettingChange(kSearchEngineChanged),
|
| + old_url_(old_url),
|
| + new_url_(new_url) {
|
| + DCHECK(new_url_);
|
| +}
|
| +
|
| +DefaultSearchProviderChange::~DefaultSearchProviderChange() {
|
| +}
|
| +
|
| +string16 DefaultSearchProviderChange::GetOldSetting() const {
|
| + if (old_url_)
|
| + return old_url_->short_name();
|
| + else
|
| + return string16();
|
| +}
|
| +
|
| +string16 DefaultSearchProviderChange::GetNewSetting() const {
|
| + DCHECK(new_url_);
|
| + return new_url_->short_name();
|
| +}
|
| +
|
| +void DefaultSearchProviderChange::Accept(Protector* protector) {
|
| + SetDefaultSearchProvider(protector, new_url_);
|
| + // TODO(avayvod): Add histrogram.
|
| +}
|
| +
|
| +void DefaultSearchProviderChange::Revert(Protector* protector) {
|
| + SetDefaultSearchProvider(protector, old_url_);
|
| + if (!old_url_) {
|
| + // Open settings page in case the original setting was lost.
|
| + protector->OpenTab(
|
| + GURL(std::string(chrome::kChromeUISettingsURL) +
|
| + chrome::kSearchEnginesSubPage));
|
| + }
|
| + // TODO(avayvod): Add histrogram.
|
| +}
|
| +
|
| +void DefaultSearchProviderChange::DoDefault(Protector* protector) {
|
| + SetDefaultSearchProvider(protector, old_url_);
|
| + // TODO(avayvod): Add histrogram.
|
| +}
|
| +
|
| +void DefaultSearchProviderChange::SetDefaultSearchProvider(
|
| + Protector* protector,
|
| + const TemplateURL* url) {
|
| + DCHECK(protector);
|
| + TemplateURLService* url_service = protector->GetTemplateURLService();
|
| + if (!url_service) {
|
| + LOG(WARNING) << "Can't get TemplateURLService object.";
|
| + return;
|
| + }
|
| + if (!url) {
|
| + // Select the first among search providers as default.
|
| + const TemplateURLService::TemplateURLVector& urls =
|
| + url_service->GetTemplateURLs();
|
| + if (!urls.empty())
|
| + url = urls.front();
|
| + }
|
| + url_service->SetVerifiedDefaultSearchProvider(url);
|
| +}
|
| +
|
| +SettingChange* CreateDefaultSearchProviderChange(
|
| + const TemplateURL* actual,
|
| + const TemplateURL* backup) {
|
| + return new DefaultSearchProviderChange(backup, actual);
|
| +}
|
| +
|
| +} // namespace protector
|
|
|