| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/protector/base_setting_change.h" | 8 #include "chrome/browser/protector/base_setting_change.h" |
| 9 #include "chrome/browser/protector/protector.h" | 9 #include "chrome/browser/protector/protector.h" |
| 10 #include "chrome/browser/search_engines/template_url.h" | 10 #include "chrome/browser/search_engines/template_url.h" |
| 11 #include "chrome/browser/search_engines/template_url_service.h" | 11 #include "chrome/browser/search_engines/template_url_service.h" |
| 12 #include "chrome/browser/search_engines/template_url_service_observer.h" |
| 12 #include "chrome/browser/webdata/keyword_table.h" | 13 #include "chrome/browser/webdata/keyword_table.h" |
| 13 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 14 #include "grit/chromium_strings.h" | 15 #include "grit/chromium_strings.h" |
| 15 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 18 | 19 |
| 19 namespace protector { | 20 namespace protector { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Maximum length of the search engine name to be displayed. | 24 // Maximum length of the search engine name to be displayed. |
| 24 const size_t kMaxDisplayedNameLength = 10; | 25 const size_t kMaxDisplayedNameLength = 10; |
| 25 | 26 |
| 26 } // namespace | 27 } // namespace |
| 27 | 28 |
| 28 class DefaultSearchProviderChange : public BaseSettingChange { | 29 class DefaultSearchProviderChange : public BaseSettingChange, |
| 30 public TemplateURLServiceObserver { |
| 29 public: | 31 public: |
| 30 DefaultSearchProviderChange(const TemplateURL* old_url, | 32 DefaultSearchProviderChange(const TemplateURL* old_url, |
| 31 const TemplateURL* new_url); | 33 const TemplateURL* new_url); |
| 32 | 34 |
| 33 // BaseSettingChange overrides: | 35 // BaseSettingChange overrides: |
| 34 virtual bool Init(Protector* protector) OVERRIDE; | 36 virtual bool Init(Protector* protector) OVERRIDE; |
| 35 virtual void Apply(Protector* protector) OVERRIDE; | 37 virtual void Apply() OVERRIDE; |
| 36 virtual void Discard(Protector* protector) OVERRIDE; | 38 virtual void Discard() OVERRIDE; |
| 39 virtual void OnBeforeRemoved() OVERRIDE; |
| 37 virtual string16 GetBubbleTitle() const OVERRIDE; | 40 virtual string16 GetBubbleTitle() const OVERRIDE; |
| 38 virtual string16 GetBubbleMessage() const OVERRIDE; | 41 virtual string16 GetBubbleMessage() const OVERRIDE; |
| 39 virtual string16 GetApplyButtonText() const OVERRIDE; | 42 virtual string16 GetApplyButtonText() const OVERRIDE; |
| 40 virtual string16 GetDiscardButtonText() const OVERRIDE; | 43 virtual string16 GetDiscardButtonText() const OVERRIDE; |
| 41 | 44 |
| 45 // TemplateURLServiceObserver overrides: |
| 46 virtual void OnTemplateURLServiceChanged() OVERRIDE; |
| 47 |
| 42 private: | 48 private: |
| 43 virtual ~DefaultSearchProviderChange(); | 49 virtual ~DefaultSearchProviderChange(); |
| 44 | 50 |
| 45 // Sets the given default search provider to profile that |protector| is | 51 // Sets the given default search provider to profile that this change is |
| 46 // guarding. Returns the |TemplateURL| instance the default search provider | 52 // related to. Returns the |TemplateURL| instance of the new default search |
| 47 // has been set to. If no search provider with |id| exists and | 53 // provider. If no search provider with |id| exists and |allow_fallback| is |
| 48 // |allow_fallback| is true, sets one of the prepoluated search providers. | 54 // true, sets one of the prepopulated search providers. |
| 49 const TemplateURL* SetDefaultSearchProvider(Protector* protector, | 55 const TemplateURL* SetDefaultSearchProvider(int64 id, |
| 50 int64 id, | |
| 51 bool allow_fallback); | 56 bool allow_fallback); |
| 52 | 57 |
| 53 // Opens the Search engine settings page in a new tab. | 58 // Opens the Search engine settings page in a new tab. |
| 54 void OpenSearchEngineSettings(Protector* protector); | 59 void OpenSearchEngineSettings(); |
| 55 | 60 |
| 56 int64 old_id_; | 61 int64 old_id_; |
| 57 int64 new_id_; | 62 int64 new_id_; |
| 58 // ID of the search engine that we fall back to if the backup is lost. | 63 // ID of the search engine that we fall back to if the backup is lost. |
| 59 int64 fallback_id_; | 64 int64 fallback_id_; |
| 60 string16 old_name_; | 65 string16 old_name_; |
| 61 string16 new_name_; | 66 string16 new_name_; |
| 62 // Name of the search engine that we fall back to if the backup is lost. | 67 // Name of the search engine that we fall back to if the backup is lost. |
| 63 string16 fallback_name_; | 68 string16 fallback_name_; |
| 64 string16 product_name_; | 69 string16 product_name_; |
| 70 // Default search provider set by |Init| for the period until user makes a |
| 71 // choice and either |Apply| or |Discard| is performed. Should only be used |
| 72 // for comparison with the current default search provider and never |
| 73 // dereferenced other than in |Init| because it may be deallocated by |
| 74 // TemplateURLService at any time. |
| 75 const TemplateURL* default_search_provider_; |
| 65 | 76 |
| 66 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); | 77 DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange); |
| 67 }; | 78 }; |
| 68 | 79 |
| 69 DefaultSearchProviderChange::DefaultSearchProviderChange( | 80 DefaultSearchProviderChange::DefaultSearchProviderChange( |
| 70 const TemplateURL* old_url, | 81 const TemplateURL* old_url, |
| 71 const TemplateURL* new_url) | 82 const TemplateURL* new_url) |
| 72 : old_id_(0), | 83 : old_id_(0), |
| 73 new_id_(0), | 84 new_id_(0), |
| 74 fallback_id_(0), | 85 fallback_id_(0), |
| 75 product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)) { | 86 product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)), |
| 87 default_search_provider_(NULL) { |
| 76 if (new_url) { | 88 if (new_url) { |
| 77 new_id_ = new_url->id(); | 89 new_id_ = new_url->id(); |
| 78 new_name_ = new_url->short_name(); | 90 new_name_ = new_url->short_name(); |
| 79 } | 91 } |
| 80 if (old_url) { | 92 if (old_url) { |
| 81 old_id_ = old_url->id(); | 93 old_id_ = old_url->id(); |
| 82 old_name_ = old_url->short_name(); | 94 old_name_ = old_url->short_name(); |
| 83 } | 95 } |
| 84 } | 96 } |
| 85 | 97 |
| 86 DefaultSearchProviderChange::~DefaultSearchProviderChange() { | 98 DefaultSearchProviderChange::~DefaultSearchProviderChange() { |
| 87 } | 99 } |
| 88 | 100 |
| 89 bool DefaultSearchProviderChange::Init(Protector* protector) { | 101 bool DefaultSearchProviderChange::Init(Protector* protector) { |
| 102 BaseSettingChange::Init(protector); |
| 103 |
| 90 // Initially reset the search engine to its previous setting. | 104 // Initially reset the search engine to its previous setting. |
| 91 const TemplateURL* current_url = | 105 default_search_provider_ = SetDefaultSearchProvider(old_id_, true); |
| 92 SetDefaultSearchProvider(protector, old_id_, true); | 106 if (!default_search_provider_) |
| 93 if (!current_url) | |
| 94 return false; | 107 return false; |
| 95 if (!old_id_ || current_url->id() != old_id_) { | 108 |
| 109 if (!old_id_ || default_search_provider_->id() != old_id_) { |
| 96 // Old settings is lost or invalid, so we had to fall back to one of the | 110 // Old settings is lost or invalid, so we had to fall back to one of the |
| 97 // prepopulated search engines. | 111 // prepopulated search engines. |
| 98 fallback_id_ = current_url->id(); | 112 fallback_id_ = default_search_provider_->id(); |
| 99 fallback_name_ = current_url->short_name(); | 113 fallback_name_ = default_search_provider_->short_name(); |
| 100 VLOG(1) << "Fallback to " << fallback_name_; | 114 VLOG(1) << "Fallback to " << fallback_name_; |
| 101 } | 115 } |
| 116 |
| 117 protector->GetTemplateURLService()->AddObserver(this); |
| 118 |
| 102 return true; | 119 return true; |
| 103 } | 120 } |
| 104 | 121 |
| 105 void DefaultSearchProviderChange::Apply(Protector* protector) { | 122 void DefaultSearchProviderChange::Apply() { |
| 106 // TODO(avayvod): Add histrogram. | 123 // TODO(avayvod): Add histrogram. |
| 107 if (!new_id_) { | 124 if (!new_id_) { |
| 108 // Open settings page in case the new setting is invalid. | 125 // Open settings page in case the new setting is invalid. |
| 109 OpenSearchEngineSettings(protector); | 126 OpenSearchEngineSettings(); |
| 110 } else { | 127 } else { |
| 111 SetDefaultSearchProvider(protector, new_id_, false); | 128 SetDefaultSearchProvider(new_id_, false); |
| 112 } | 129 } |
| 113 } | 130 } |
| 114 | 131 |
| 115 void DefaultSearchProviderChange::Discard(Protector* protector) { | 132 void DefaultSearchProviderChange::Discard() { |
| 116 // TODO(avayvod): Add histrogram. | 133 // TODO(avayvod): Add histrogram. |
| 117 if (!old_id_) { | 134 if (!old_id_) { |
| 118 // Open settings page in case the old setting is invalid. | 135 // Open settings page in case the old setting is invalid. |
| 119 OpenSearchEngineSettings(protector); | 136 OpenSearchEngineSettings(); |
| 120 } | 137 } |
| 121 // Nothing to do otherwise since we have already set the search engine | 138 // Nothing to do otherwise since we have already set the search engine |
| 122 // to |old_id_| in |Init|. | 139 // to |old_id_| in |Init|. |
| 123 } | 140 } |
| 124 | 141 |
| 142 void DefaultSearchProviderChange::OnBeforeRemoved() { |
| 143 protector()->GetTemplateURLService()->RemoveObserver(this); |
| 144 } |
| 145 |
| 125 string16 DefaultSearchProviderChange::GetBubbleTitle() const { | 146 string16 DefaultSearchProviderChange::GetBubbleTitle() const { |
| 126 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE); | 147 return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE); |
| 127 } | 148 } |
| 128 | 149 |
| 129 string16 DefaultSearchProviderChange::GetBubbleMessage() const { | 150 string16 DefaultSearchProviderChange::GetBubbleMessage() const { |
| 130 if (fallback_name_.empty()) | 151 if (fallback_name_.empty()) |
| 131 return l10n_util::GetStringFUTF16( | 152 return l10n_util::GetStringFUTF16( |
| 132 IDS_SEARCH_ENGINE_CHANGE_MESSAGE, product_name_); | 153 IDS_SEARCH_ENGINE_CHANGE_MESSAGE, product_name_); |
| 133 else | 154 else |
| 134 return l10n_util::GetStringFUTF16( | 155 return l10n_util::GetStringFUTF16( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 161 if (new_name_.length() > kMaxDisplayedNameLength) | 182 if (new_name_.length() > kMaxDisplayedNameLength) |
| 162 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); | 183 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); |
| 163 else | 184 else |
| 164 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_); | 185 return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_); |
| 165 } else { | 186 } else { |
| 166 // Old setting is lost, offer to go to settings. | 187 // Old setting is lost, offer to go to settings. |
| 167 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); | 188 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); |
| 168 } | 189 } |
| 169 } | 190 } |
| 170 | 191 |
| 192 void DefaultSearchProviderChange::OnTemplateURLServiceChanged() { |
| 193 if (protector()->GetTemplateURLService()->GetDefaultSearchProvider() != |
| 194 default_search_provider_) { |
| 195 default_search_provider_ = NULL; |
| 196 VLOG(1) << "Default search provider has been changed by user"; |
| 197 // This will delete the Protector instance and |this|. |
| 198 protector()->DismissChange(); |
| 199 } |
| 200 } |
| 201 |
| 171 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider( | 202 const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider( |
| 172 Protector* protector, | |
| 173 int64 id, | 203 int64 id, |
| 174 bool allow_fallback) { | 204 bool allow_fallback) { |
| 175 TemplateURLService* url_service = protector->GetTemplateURLService(); | 205 TemplateURLService* url_service = protector()->GetTemplateURLService(); |
| 176 if (!url_service) { | 206 if (!url_service) { |
| 177 NOTREACHED() << "Can't get TemplateURLService object."; | 207 NOTREACHED() << "Can't get TemplateURLService object."; |
| 178 return NULL; | 208 return NULL; |
| 179 } | 209 } |
| 180 const TemplateURL* url = NULL; | 210 const TemplateURL* url = NULL; |
| 181 if (id) { | 211 if (id) { |
| 182 const TemplateURLService::TemplateURLVector& urls = | 212 const TemplateURLService::TemplateURLVector& urls = |
| 183 url_service->GetTemplateURLs(); | 213 url_service->GetTemplateURLs(); |
| 184 for (size_t i = 0; i < urls.size(); ++i) { | 214 for (size_t i = 0; i < urls.size(); ++i) { |
| 185 if (urls[i]->id() == id) { | 215 if (urls[i]->id() == id) { |
| 186 url = urls[i]; | 216 url = urls[i]; |
| 187 break; | 217 break; |
| 188 } | 218 } |
| 189 } | 219 } |
| 190 } | 220 } |
| 191 if (!url && allow_fallback) { | 221 if (!url && allow_fallback) { |
| 192 url = url_service->FindNewDefaultSearchProvider(); | 222 url = url_service->FindNewDefaultSearchProvider(); |
| 193 DCHECK(url); | 223 DCHECK(url); |
| 194 } | 224 } |
| 195 if (url) { | 225 if (url) { |
| 196 url_service->SetDefaultSearchProvider(url); | 226 url_service->SetDefaultSearchProvider(url); |
| 197 VLOG(1) << "Default search provider set to: " << url->short_name(); | 227 VLOG(1) << "Default search provider set to: " << url->short_name(); |
| 198 } | 228 } |
| 199 return url; | 229 return url; |
| 200 } | 230 } |
| 201 | 231 |
| 202 void DefaultSearchProviderChange::OpenSearchEngineSettings( | 232 void DefaultSearchProviderChange::OpenSearchEngineSettings() { |
| 203 Protector* protector) { | 233 protector()->OpenTab( |
| 204 protector->OpenTab( | |
| 205 GURL(std::string(chrome::kChromeUISettingsURL) + | 234 GURL(std::string(chrome::kChromeUISettingsURL) + |
| 206 chrome::kSearchEnginesSubPage)); | 235 chrome::kSearchEnginesSubPage)); |
| 207 } | 236 } |
| 208 | 237 |
| 209 BaseSettingChange* CreateDefaultSearchProviderChange( | 238 BaseSettingChange* CreateDefaultSearchProviderChange( |
| 210 const TemplateURL* actual, | 239 const TemplateURL* actual, |
| 211 const TemplateURL* backup) { | 240 const TemplateURL* backup) { |
| 212 return new DefaultSearchProviderChange(backup, actual); | 241 return new DefaultSearchProviderChange(backup, actual); |
| 213 } | 242 } |
| 214 | 243 |
| 215 } // namespace protector | 244 } // namespace protector |
| OLD | NEW |