| 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(const TemplateURL* old_url, |
| 21 const TemplateURL* new_url); |
| 22 |
| 23 // SettingChange overrides: |
| 24 virtual string16 GetOldSetting() const OVERRIDE; |
| 25 virtual string16 GetNewSetting() const OVERRIDE; |
| 26 virtual void Accept(Protector* protector) OVERRIDE; |
| 27 virtual void Revert(Protector* protector) OVERRIDE; |
| 28 virtual void DoDefault(Protector* protector) OVERRIDE; |
| 29 |
| 30 private: |
| 31 ~DefaultSearchProviderChange(); |
| 32 |
| 33 // Sets the given default search provider to profile that |protector| is |
| 34 // guarding. |
| 35 void SetDefaultSearchProvider(Protector* protector, const TemplateURL* url); |
| 36 |
| 37 const TemplateURL* old_url_; |
| 38 const TemplateURL* new_url_; |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); |
| 41 }; |
| 42 |
| 43 DefaultSearchProviderChange::DefaultSearchProviderChange( |
| 44 const TemplateURL* old_url, |
| 45 const TemplateURL* new_url) |
| 46 : SettingChange(kSearchEngineChanged), |
| 47 old_url_(old_url), |
| 48 new_url_(new_url) { |
| 49 DCHECK(new_url_); |
| 50 } |
| 51 |
| 52 DefaultSearchProviderChange::~DefaultSearchProviderChange() { |
| 53 } |
| 54 |
| 55 string16 DefaultSearchProviderChange::GetOldSetting() const { |
| 56 if (old_url_) |
| 57 return old_url_->short_name(); |
| 58 else |
| 59 return string16(); |
| 60 } |
| 61 |
| 62 string16 DefaultSearchProviderChange::GetNewSetting() const { |
| 63 DCHECK(new_url_); |
| 64 return new_url_->short_name(); |
| 65 } |
| 66 |
| 67 void DefaultSearchProviderChange::Accept(Protector* protector) { |
| 68 SetDefaultSearchProvider(protector, new_url_); |
| 69 // TODO(avayvod): Add histrogram. |
| 70 } |
| 71 |
| 72 void DefaultSearchProviderChange::Revert(Protector* protector) { |
| 73 SetDefaultSearchProvider(protector, old_url_); |
| 74 if (!old_url_) { |
| 75 // Open settings page in case the original setting was lost. |
| 76 protector->OpenTab( |
| 77 GURL(std::string(chrome::kChromeUISettingsURL) + |
| 78 chrome::kSearchEnginesSubPage)); |
| 79 } |
| 80 // TODO(avayvod): Add histrogram. |
| 81 } |
| 82 |
| 83 void DefaultSearchProviderChange::DoDefault(Protector* protector) { |
| 84 SetDefaultSearchProvider(protector, old_url_); |
| 85 // TODO(avayvod): Add histrogram. |
| 86 } |
| 87 |
| 88 void DefaultSearchProviderChange::SetDefaultSearchProvider( |
| 89 Protector* protector, |
| 90 const TemplateURL* url) { |
| 91 DCHECK(protector); |
| 92 TemplateURLService* url_service = protector->GetTemplateURLService(); |
| 93 if (!url_service) { |
| 94 LOG(WARNING) << "Can't get TemplateURLService object."; |
| 95 return; |
| 96 } |
| 97 if (!url) { |
| 98 // Select the first among search providers as default. |
| 99 const TemplateURLService::TemplateURLVector& urls = |
| 100 url_service->GetTemplateURLs(); |
| 101 if (!urls.empty()) |
| 102 url = urls.front(); |
| 103 } |
| 104 url_service->SetVerifiedDefaultSearchProvider(url); |
| 105 } |
| 106 |
| 107 SettingChange* CreateDefaultSearchProviderChange( |
| 108 const TemplateURL* actual, |
| 109 const TemplateURL* backup) { |
| 110 return new DefaultSearchProviderChange(backup, actual); |
| 111 } |
| 112 |
| 113 } // namespace protector |
| OLD | NEW |