| 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..6ffb7de569db72a23d5a5a0bcae1d5c513385429
|
| --- /dev/null
|
| +++ b/chrome/browser/protector/default_search_provider_change.cc
|
| @@ -0,0 +1,109 @@
|
| +// 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(
|
| + KeywordTable* table,
|
| + int64 old_id,
|
| + int64 new_id);
|
| +
|
| + // SettingChange overrides:
|
| + virtual const string16& GetOldSetting() const OVERRIDE;
|
| + virtual const string16& GetNewSetting() const OVERRIDE;
|
| + virtual void Accept(Protector* protector) OVERRIDE;
|
| + virtual void Revert(Protector* protector) OVERRIDE;
|
| +
|
| + private:
|
| + ~DefaultSearchProviderChange();
|
| +
|
| + // ID of the new default search provider to be accepted.
|
| + int64 new_id_;
|
| +
|
| + // Short name of the old default search provider.
|
| + string16 old_name_;
|
| +
|
| + // Short name of the new default search provider.
|
| + string16 new_name_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange);
|
| +};
|
| +
|
| +DefaultSearchProviderChange::DefaultSearchProviderChange(
|
| + KeywordTable* table,
|
| + int64 old_id,
|
| + int64 new_id)
|
| + : SettingChange(kSearchEngineChanged),
|
| + new_id_(new_id),
|
| + old_name_(table->GetKeywordShortName(old_id)),
|
| + new_name_(table->GetKeywordShortName(new_id)) {
|
| +}
|
| +
|
| +DefaultSearchProviderChange::~DefaultSearchProviderChange() {
|
| +}
|
| +
|
| +const string16& DefaultSearchProviderChange::GetOldSetting() const {
|
| + return old_name_;
|
| +}
|
| +
|
| +const string16& DefaultSearchProviderChange::GetNewSetting() const {
|
| + return new_name_;
|
| +}
|
| +
|
| +void DefaultSearchProviderChange::Accept(Protector* protector) {
|
| + DCHECK(protector);
|
| + TemplateURLService* url_service = protector->GetTemplateURLService();
|
| + if (!url_service) {
|
| + LOG(WARNING) << "Can't get TemplateURLService object.";
|
| + return;
|
| + }
|
| + const TemplateURLService::TemplateURLVector& urls =
|
| + url_service->GetTemplateURLs();
|
| + for (size_t i = 0; i < urls.size(); ++i) {
|
| + if (urls[i]->id() == new_id_) {
|
| + url_service->SetDefaultSearchProvider(urls[i]);
|
| + return;
|
| + }
|
| + }
|
| + LOG(WARNING) << "Didn't find search provider with id " << new_id_;
|
| + // TODO(avayvod): Add histrogram.
|
| +}
|
| +
|
| +void DefaultSearchProviderChange::Revert(Protector* protector) {
|
| + DCHECK(protector);
|
| + if (old_name_.empty()) {
|
| + TemplateURLService* url_service = protector->GetTemplateURLService();
|
| + if (url_service)
|
| + url_service->SetDefaultSearchProvider(NULL);
|
| + else
|
| + LOG(WARNING) << "Can't get TemplateURLService object.";
|
| + // Open settings page in case the original setting was lost.
|
| + protector->OpenTab(
|
| + GURL(std::string(chrome::kChromeUISettingsURL) +
|
| + chrome::kSearchEnginesSubPage));
|
| + }
|
| + // TODO(avayvod): Add histrogram.
|
| +}
|
| +
|
| +SettingChange* CreateDefaultSearchProviderChange(
|
| + KeywordTable* table,
|
| + int64 old_id,
|
| + int64 new_id) {
|
| + return new DefaultSearchProviderChange(table, old_id, new_id);
|
| +}
|
| +
|
| +} // namespace protector
|
|
|