summaryrefslogtreecommitdiffstats
path: root/chrome/browser/protector/default_search_provider_change.cc
blob: 026788d49e8f2ec4cbb6cf4011eeec63b871ee54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// 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/base_setting_change.h"
#include "chrome/browser/protector/protector.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_observer.h"
#include "chrome/browser/webdata/keyword_table.h"
#include "chrome/common/url_constants.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "googleurl/src/gurl.h"
#include "ui/base/l10n/l10n_util.h"

namespace protector {

namespace {

// Maximum length of the search engine name to be displayed.
const size_t kMaxDisplayedNameLength = 10;

}  // namespace

class DefaultSearchProviderChange : public BaseSettingChange,
                                    public TemplateURLServiceObserver {
 public:
  DefaultSearchProviderChange(const TemplateURL* old_url,
                              const TemplateURL* new_url);

  // BaseSettingChange overrides:
  virtual bool Init(Protector* protector) OVERRIDE;
  virtual void Apply() OVERRIDE;
  virtual void Discard() OVERRIDE;
  virtual void OnBeforeRemoved() OVERRIDE;
  virtual string16 GetBubbleTitle() const OVERRIDE;
  virtual string16 GetBubbleMessage() const OVERRIDE;
  virtual string16 GetApplyButtonText() const OVERRIDE;
  virtual string16 GetDiscardButtonText() const OVERRIDE;

  // TemplateURLServiceObserver overrides:
  virtual void OnTemplateURLServiceChanged() OVERRIDE;

 private:
  virtual ~DefaultSearchProviderChange();

  // Sets the given default search provider to profile that this change is
  // related to. Returns the |TemplateURL| instance of the new default search
  // provider. If no search provider with |id| exists and |allow_fallback| is
  // true, sets one of the prepopulated search providers.
  const TemplateURL* SetDefaultSearchProvider(int64 id,
                                              bool allow_fallback);

  // Opens the Search engine settings page in a new tab.
  void OpenSearchEngineSettings();

  int64 old_id_;
  int64 new_id_;
  // ID of the search engine that we fall back to if the backup is lost.
  int64 fallback_id_;
  string16 old_name_;
  string16 new_name_;
  // Name of the search engine that we fall back to if the backup is lost.
  string16 fallback_name_;
  string16 product_name_;
  // Default search provider set by |Init| for the period until user makes a
  // choice and either |Apply| or |Discard| is performed. Should only be used
  // for comparison with the current default search provider and never
  // dereferenced other than in |Init| because it may be deallocated by
  // TemplateURLService at any time.
  const TemplateURL* default_search_provider_;

  DISALLOW_COPY_AND_ASSIGN(DefaultSearchProviderChange);
};

DefaultSearchProviderChange::DefaultSearchProviderChange(
    const TemplateURL* old_url,
    const TemplateURL* new_url)
    : old_id_(0),
      new_id_(0),
      fallback_id_(0),
      product_name_(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)),
      default_search_provider_(NULL) {
  if (new_url) {
    new_id_ = new_url->id();
    new_name_ = new_url->short_name();
  }
  if (old_url) {
    old_id_ = old_url->id();
    old_name_ = old_url->short_name();
  }
}

DefaultSearchProviderChange::~DefaultSearchProviderChange() {
}

bool DefaultSearchProviderChange::Init(Protector* protector) {
  BaseSettingChange::Init(protector);

  // Initially reset the search engine to its previous setting.
  default_search_provider_ = SetDefaultSearchProvider(old_id_, true);
  if (!default_search_provider_)
    return false;

  if (!old_id_ || default_search_provider_->id() != old_id_) {
    // Old settings is lost or invalid, so we had to fall back to one of the
    // prepopulated search engines.
    fallback_id_ = default_search_provider_->id();
    fallback_name_ = default_search_provider_->short_name();
    VLOG(1) << "Fallback to " << fallback_name_;
  }

  protector->GetTemplateURLService()->AddObserver(this);

  return true;
}

void DefaultSearchProviderChange::Apply() {
  // TODO(avayvod): Add histrogram.
  if (!new_id_) {
    // Open settings page in case the new setting is invalid.
    OpenSearchEngineSettings();
  } else {
    SetDefaultSearchProvider(new_id_, false);
  }
}

void DefaultSearchProviderChange::Discard() {
  // TODO(avayvod): Add histrogram.
  if (!old_id_) {
    // Open settings page in case the old setting is invalid.
    OpenSearchEngineSettings();
  }
  // Nothing to do otherwise since we have already set the search engine
  // to |old_id_| in |Init|.
}

void DefaultSearchProviderChange::OnBeforeRemoved() {
  protector()->GetTemplateURLService()->RemoveObserver(this);
}

string16 DefaultSearchProviderChange::GetBubbleTitle() const {
  return l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_TITLE);
}

string16 DefaultSearchProviderChange::GetBubbleMessage() const {
  if (fallback_name_.empty())
    return l10n_util::GetStringFUTF16(
        IDS_SEARCH_ENGINE_CHANGE_MESSAGE, product_name_);
  else
    return l10n_util::GetStringFUTF16(
        IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE,
        product_name_, fallback_name_);
}

string16 DefaultSearchProviderChange::GetApplyButtonText() const {
  if (new_id_) {
    if (new_id_ == fallback_id_) {
      // Old search engine is lost, the fallback search engine is the same as
      // the new one so no need to show this button.
      return string16();
    }
    if (new_name_.length() > kMaxDisplayedNameLength)
      return l10n_util::GetStringUTF16(IDS_CHANGE_SEARCH_ENGINE_NO_NAME);
    else
      return l10n_util::GetStringFUTF16(IDS_CHANGE_SEARCH_ENGINE, new_name_);
  } else if (old_id_) {
    // New setting is lost, offer to go to settings.
    return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE);
  } else {
    // Both settings are lost: don't show this button.
    return string16();
  }
}

string16 DefaultSearchProviderChange::GetDiscardButtonText() const {
  if (old_id_) {
    if (new_name_.length() > kMaxDisplayedNameLength)
      return l10n_util::GetStringUTF16(IDS_KEEP_SETTING);
    else
      return l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, old_name_);
  } else {
    // Old setting is lost, offer to go to settings.
    return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE);
  }
}

void DefaultSearchProviderChange::OnTemplateURLServiceChanged() {
  if (protector()->GetTemplateURLService()->GetDefaultSearchProvider() !=
      default_search_provider_) {
    default_search_provider_ = NULL;
    VLOG(1) << "Default search provider has been changed by user";
    // This will delete the Protector instance and |this|.
    protector()->DismissChange();
  }
}

const TemplateURL* DefaultSearchProviderChange::SetDefaultSearchProvider(
    int64 id,
    bool allow_fallback) {
  TemplateURLService* url_service = protector()->GetTemplateURLService();
  if (!url_service) {
    NOTREACHED() << "Can't get TemplateURLService object.";
    return NULL;
  }
  const TemplateURL* url = NULL;
  if (id) {
    const TemplateURLService::TemplateURLVector& urls =
        url_service->GetTemplateURLs();
    for (size_t i = 0; i < urls.size(); ++i) {
      if (urls[i]->id() == id) {
        url = urls[i];
        break;
      }
    }
  }
  if (!url && allow_fallback) {
    url = url_service->FindNewDefaultSearchProvider();
    DCHECK(url);
  }
  if (url) {
    url_service->SetDefaultSearchProvider(url);
    VLOG(1) << "Default search provider set to: " << url->short_name();
  }
  return url;
}

void DefaultSearchProviderChange::OpenSearchEngineSettings() {
  protector()->OpenTab(
      GURL(std::string(chrome::kChromeUISettingsURL) +
           chrome::kSearchEnginesSubPage));
}

BaseSettingChange* CreateDefaultSearchProviderChange(
    const TemplateURL* actual,
    const TemplateURL* backup) {
  return new DefaultSearchProviderChange(backup, actual);
}

}  // namespace protector