Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/basictypes.h" | |
| 6 #include "base/compiler_specific.h" | |
| 7 #include "base/logging.h" | |
| 8 #include "chrome/browser/protector/protector.h" | |
| 9 #include "chrome/browser/protector/setting_change.h" | |
| 10 #include "chrome/browser/search_engines/template_url.h" | |
| 11 #include "chrome/browser/search_engines/template_url_service.h" | |
| 12 #include "chrome/browser/webdata/keyword_table.h" | |
| 13 #include "chrome/common/url_constants.h" | |
| 14 #include "googleurl/src/gurl.h" | |
| 15 | |
| 16 namespace protector { | |
| 17 | |
| 18 class DefaultSearchProviderChange : public SettingChange { | |
| 19 public: | |
| 20 DefaultSearchProviderChange( | |
| 21 int64 new_id, | |
| 22 const string16& old_name, | |
| 23 const string16& new_name); | |
| 24 | |
| 25 // SettingChange overrides: | |
| 26 virtual const string16& GetOldSetting() const OVERRIDE; | |
| 27 virtual const string16& GetNewSetting() const OVERRIDE; | |
| 28 virtual void Accept(Protector* protector) OVERRIDE; | |
| 29 virtual void Revert(Protector* protector) OVERRIDE; | |
| 30 | |
| 31 private: | |
| 32 ~DefaultSearchProviderChange(); | |
| 33 | |
| 34 // ID of the new default search provider to be accepted. | |
| 35 int64 new_id_; | |
| 36 | |
| 37 // Short name of the old default search provider. | |
| 38 string16 old_name_; | |
| 39 | |
| 40 // Short name of the new default search provider. | |
| 41 string16 new_name_; | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); | |
| 44 }; | |
| 45 | |
| 46 DefaultSearchProviderChange::DefaultSearchProviderChange( | |
| 47 int64 new_id, | |
| 48 const string16& old_name, | |
| 49 const string16& new_name) | |
| 50 : SettingChange(kSearchEngineChanged), | |
| 51 new_id_(new_id), | |
| 52 old_name_(old_name), | |
| 53 new_name_(new_name) { | |
| 54 } | |
| 55 | |
| 56 DefaultSearchProviderChange::~DefaultSearchProviderChange() { | |
| 57 } | |
| 58 | |
| 59 const string16& DefaultSearchProviderChange::GetOldSetting() const { | |
| 60 return old_name_; | |
| 61 } | |
| 62 | |
| 63 const string16& DefaultSearchProviderChange::GetNewSetting() const { | |
| 64 return new_name_; | |
| 65 } | |
| 66 | |
| 67 void DefaultSearchProviderChange::Accept(Protector* protector) { | |
| 68 DCHECK(protector); | |
| 69 TemplateURLService* url_service = protector->GetTemplateURLService(); | |
| 70 if (!url_service) { | |
| 71 LOG(WARNING) << "Can't get TemplateURLService object."; | |
| 72 return; | |
| 73 } | |
| 74 const TemplateURLService::TemplateURLVector& urls = | |
| 75 url_service->GetTemplateURLs(); | |
| 76 for (size_t i = 0; i < urls.size(); ++i) { | |
| 77 if (urls[i]->id() == new_id_) { | |
| 78 url_service->SetDefaultSearchProvider(urls[i]); | |
| 79 return; | |
| 80 } | |
| 81 } | |
| 82 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
| |
| 83 // TODO(avayvod): Add histrogram. | |
| 84 } | |
| 85 | |
| 86 void DefaultSearchProviderChange::Revert(Protector* protector) { | |
| 87 DCHECK(protector); | |
| 88 if (old_name_.empty()) { | |
| 89 TemplateURLService* url_service = protector->GetTemplateURLService(); | |
| 90 if (url_service) | |
| 91 url_service->SetDefaultSearchProvider(NULL); | |
| 92 else | |
| 93 LOG(WARNING) << "Can't get TemplateURLService object."; | |
| 94 // Open settings page in case the original setting was lost. | |
| 95 protector->OpenTab( | |
| 96 GURL(std::string(chrome::kChromeUISettingsURL) + | |
| 97 chrome::kSearchEnginesSubPage)); | |
| 98 } | |
| 99 // TODO(avayvod): Add histrogram. | |
| 100 } | |
| 101 | |
| 102 SettingChange* CreateDefaultSearchProviderChange(KeywordTable* table) { | |
| 103 int64 new_id = table->GetDefaultSearchProviderID(); | |
| 104 int64 old_id = table->GetDefaultSearchProviderIDBackup(); | |
| 105 return new DefaultSearchProviderChange( | |
| 106 new_id, | |
| 107 table->GetKeywordShortName(old_id), | |
| 108 table->GetKeywordShortName(new_id)); | |
| 109 } | |
| 110 | |
| 111 } // namespace protector | |
| OLD | NEW |