Chromium Code Reviews| 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..01115975392fcee8af83976a50bd4f19a676e7e7 |
| --- /dev/null |
| +++ b/chrome/browser/protector/default_search_provider_change.cc |
| @@ -0,0 +1,111 @@ |
| +// 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( |
| + int64 new_id, |
| + const string16& old_name, |
| + const string16& new_name); |
| + |
| + // 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( |
| + int64 new_id, |
| + const string16& old_name, |
| + const string16& new_name) |
| + : SettingChange(kSearchEngineChanged), |
| + new_id_(new_id), |
| + old_name_(old_name), |
| + new_name_(new_name) { |
| +} |
| + |
| +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_; |
|
whywhat
2011/10/20 23:02:39
After moving the check to WebDataService, Template
|
| + // 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 new_id = table->GetDefaultSearchProviderID(); |
| + int64 old_id = table->GetDefaultSearchProviderIDBackup(); |
| + return new DefaultSearchProviderChange( |
| + new_id, |
| + table->GetKeywordShortName(old_id), |
| + table->GetKeywordShortName(new_id)); |
| +} |
| + |
| +} // namespace protector |