Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / startup_pages_handler.cc
blob6f9946ccbc42755a1c2c8ccc1491eaa6e0f02b10
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/webui/options/startup_pages_handler.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
11 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/custom_home_pages_table_model.h"
14 #include "chrome/browser/prefs/session_startup_pref.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/metrics/proto/omnibox_event.pb.h"
20 #include "components/omnibox/browser/autocomplete_classifier.h"
21 #include "components/omnibox/browser/autocomplete_controller.h"
22 #include "components/omnibox/browser/autocomplete_input.h"
23 #include "components/omnibox/browser/autocomplete_result.h"
24 #include "components/url_formatter/url_fixer.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/web_ui.h"
28 namespace options {
30 StartupPagesHandler::StartupPagesHandler() {
33 StartupPagesHandler::~StartupPagesHandler() {
36 void StartupPagesHandler::GetLocalizedValues(
37 base::DictionaryValue* localized_strings) {
38 DCHECK(localized_strings);
40 static OptionsStringResource resources[] = {
41 { "startupAddLabel", IDS_OPTIONS_STARTUP_ADD_LABEL },
42 { "startupUseCurrent", IDS_OPTIONS_STARTUP_USE_CURRENT },
43 { "startupPagesPlaceholder", IDS_OPTIONS_STARTUP_PAGES_PLACEHOLDER },
46 RegisterStrings(localized_strings, resources, arraysize(resources));
47 RegisterTitle(localized_strings, "startupPagesOverlay",
48 IDS_OPTIONS_STARTUP_PAGES_DIALOG_TITLE);
51 void StartupPagesHandler::RegisterMessages() {
52 // Guest profiles should never have been displayed the option to set these
53 // values.
54 if (Profile::FromWebUI(web_ui())->IsOffTheRecord())
55 return;
57 web_ui()->RegisterMessageCallback("removeStartupPages",
58 base::Bind(&StartupPagesHandler::RemoveStartupPages,
59 base::Unretained(this)));
60 web_ui()->RegisterMessageCallback("addStartupPage",
61 base::Bind(&StartupPagesHandler::AddStartupPage,
62 base::Unretained(this)));
63 web_ui()->RegisterMessageCallback("editStartupPage",
64 base::Bind(&StartupPagesHandler::EditStartupPage,
65 base::Unretained(this)));
66 web_ui()->RegisterMessageCallback("setStartupPagesToCurrentPages",
67 base::Bind(&StartupPagesHandler::SetStartupPagesToCurrentPages,
68 base::Unretained(this)));
69 web_ui()->RegisterMessageCallback("dragDropStartupPage",
70 base::Bind(&StartupPagesHandler::DragDropStartupPage,
71 base::Unretained(this)));
72 web_ui()->RegisterMessageCallback(
73 "requestAutocompleteSuggestionsForStartupPages",
74 base::Bind(&StartupPagesHandler::RequestAutocompleteSuggestions,
75 base::Unretained(this)));
76 web_ui()->RegisterMessageCallback("commitStartupPrefChanges",
77 base::Bind(&StartupPagesHandler::CommitChanges,
78 base::Unretained(this)));
79 web_ui()->RegisterMessageCallback("cancelStartupPrefChanges",
80 base::Bind(&StartupPagesHandler::CancelChanges,
81 base::Unretained(this)));
84 void StartupPagesHandler::UpdateStartupPages() {
85 Profile* profile = Profile::FromWebUI(web_ui());
86 const SessionStartupPref startup_pref =
87 SessionStartupPref::GetStartupPref(profile->GetPrefs());
88 startup_custom_pages_table_model_->SetURLs(startup_pref.urls);
91 void StartupPagesHandler::InitializeHandler() {
92 Profile* profile = Profile::FromWebUI(web_ui());
94 startup_custom_pages_table_model_.reset(
95 new CustomHomePagesTableModel(profile));
96 startup_custom_pages_table_model_->SetObserver(this);
98 pref_change_registrar_.Init(profile->GetPrefs());
99 pref_change_registrar_.Add(
100 prefs::kURLsToRestoreOnStartup,
101 base::Bind(&StartupPagesHandler::UpdateStartupPages,
102 base::Unretained(this)));
104 autocomplete_controller_.reset(new AutocompleteController(
105 make_scoped_ptr(new ChromeAutocompleteProviderClient(profile)), this,
106 AutocompleteClassifier::kDefaultOmniboxProviders));
109 void StartupPagesHandler::InitializePage() {
110 UpdateStartupPages();
113 void StartupPagesHandler::OnModelChanged() {
114 base::ListValue startup_pages;
115 int page_count = startup_custom_pages_table_model_->RowCount();
116 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs();
117 for (int i = 0; i < page_count; ++i) {
118 base::DictionaryValue* entry = new base::DictionaryValue();
119 entry->SetString("title", startup_custom_pages_table_model_->GetText(i, 0));
120 entry->SetString("url", urls[i].spec());
121 entry->SetString("tooltip",
122 startup_custom_pages_table_model_->GetTooltip(i));
123 entry->SetInteger("modelIndex", i);
124 startup_pages.Append(entry);
127 web_ui()->CallJavascriptFunction("StartupOverlay.updateStartupPages",
128 startup_pages);
131 void StartupPagesHandler::OnItemsChanged(int start, int length) {
132 OnModelChanged();
135 void StartupPagesHandler::OnItemsAdded(int start, int length) {
136 OnModelChanged();
139 void StartupPagesHandler::OnItemsRemoved(int start, int length) {
140 OnModelChanged();
143 void StartupPagesHandler::SetStartupPagesToCurrentPages(
144 const base::ListValue* args) {
145 startup_custom_pages_table_model_->SetToCurrentlyOpenPages();
148 void StartupPagesHandler::RemoveStartupPages(const base::ListValue* args) {
149 for (int i = args->GetSize() - 1; i >= 0; --i) {
150 int selected_index;
151 CHECK(args->GetInteger(i, &selected_index));
153 if (selected_index < 0 ||
154 selected_index >= startup_custom_pages_table_model_->RowCount()) {
155 NOTREACHED();
156 return;
158 startup_custom_pages_table_model_->Remove(selected_index);
162 void StartupPagesHandler::AddStartupPage(const base::ListValue* args) {
163 std::string url_string;
164 CHECK(args->GetString(0, &url_string));
166 GURL url = url_formatter::FixupURL(url_string, std::string());
167 if (!url.is_valid())
168 return;
170 int row_count = startup_custom_pages_table_model_->RowCount();
171 int index;
172 if (!args->GetInteger(1, &index) || index > row_count)
173 index = row_count;
175 startup_custom_pages_table_model_->Add(index, url);
178 void StartupPagesHandler::EditStartupPage(const base::ListValue* args) {
179 std::string url_string;
180 GURL fixed_url;
181 int index;
182 CHECK_EQ(args->GetSize(), 2U);
183 CHECK(args->GetInteger(0, &index));
184 CHECK(args->GetString(1, &url_string));
186 if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) {
187 NOTREACHED();
188 return;
191 fixed_url = url_formatter::FixupURL(url_string, std::string());
192 if (!fixed_url.is_empty()) {
193 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs();
194 urls[index] = fixed_url;
195 startup_custom_pages_table_model_->SetURLs(urls);
196 } else {
197 startup_custom_pages_table_model_->Remove(index);
201 void StartupPagesHandler::DragDropStartupPage(const base::ListValue* args) {
202 CHECK_EQ(args->GetSize(), 2U);
204 int to_index;
206 CHECK(args->GetInteger(0, &to_index));
208 const base::ListValue* selected;
209 CHECK(args->GetList(1, &selected));
211 std::vector<int> index_list;
212 for (size_t i = 0; i < selected->GetSize(); ++i) {
213 int index;
214 CHECK(selected->GetInteger(i, &index));
215 index_list.push_back(index);
218 startup_custom_pages_table_model_->MoveURLs(to_index, index_list);
221 void StartupPagesHandler::SaveStartupPagesPref() {
222 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
224 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs);
225 pref.urls = startup_custom_pages_table_model_->GetURLs();
227 if (pref.urls.empty())
228 pref.type = SessionStartupPref::DEFAULT;
230 SessionStartupPref::SetStartupPref(prefs, pref);
233 void StartupPagesHandler::CommitChanges(const base::ListValue* args) {
234 SaveStartupPagesPref();
237 void StartupPagesHandler::CancelChanges(const base::ListValue* args) {
238 UpdateStartupPages();
241 void StartupPagesHandler::RequestAutocompleteSuggestions(
242 const base::ListValue* args) {
243 base::string16 input;
244 CHECK_EQ(args->GetSize(), 1U);
245 CHECK(args->GetString(0, &input));
247 autocomplete_controller_->Start(AutocompleteInput(
248 input, base::string16::npos, std::string(), GURL(),
249 metrics::OmniboxEventProto::INVALID_SPEC, true, false, false, true, false,
250 ChromeAutocompleteSchemeClassifier(Profile::FromWebUI(web_ui()))));
253 void StartupPagesHandler::OnResultChanged(bool default_match_changed) {
254 const AutocompleteResult& result = autocomplete_controller_->result();
255 base::ListValue suggestions;
256 OptionsUI::ProcessAutocompleteSuggestions(result, &suggestions);
257 web_ui()->CallJavascriptFunction(
258 "StartupOverlay.updateAutocompleteSuggestions", suggestions);
261 } // namespace options