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"
8 #include "base/bind_helpers.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
11 #include "chrome/browser/autocomplete/autocomplete_controller.h"
12 #include "chrome/browser/autocomplete/autocomplete_input.h"
13 #include "chrome/browser/autocomplete/autocomplete_result.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/custom_home_pages_table_model.h"
16 #include "chrome/browser/prefs/session_startup_pref.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/net/url_fixer_upper.h"
19 #include "chrome/common/pref_names.h"
20 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/web_ui.h"
22 #include "grit/generated_resources.h"
26 StartupPagesHandler::StartupPagesHandler() {}
28 StartupPagesHandler::~StartupPagesHandler() {
32 void StartupPagesHandler::GetLocalizedValues(
33 base::DictionaryValue
* localized_strings
) {
34 DCHECK(localized_strings
);
36 static OptionsStringResource resources
[] = {
37 { "startupAddLabel", IDS_OPTIONS_STARTUP_ADD_LABEL
},
38 { "startupUseCurrent", IDS_OPTIONS_STARTUP_USE_CURRENT
},
39 { "startupPagesPlaceholder", IDS_OPTIONS_STARTUP_PAGES_PLACEHOLDER
},
42 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
43 RegisterTitle(localized_strings
, "startupPagesOverlay",
44 IDS_OPTIONS_STARTUP_PAGES_DIALOG_TITLE
);
47 void StartupPagesHandler::RegisterMessages() {
48 web_ui()->RegisterMessageCallback("removeStartupPages",
49 base::Bind(&StartupPagesHandler::RemoveStartupPages
,
50 base::Unretained(this)));
51 web_ui()->RegisterMessageCallback("addStartupPage",
52 base::Bind(&StartupPagesHandler::AddStartupPage
,
53 base::Unretained(this)));
54 web_ui()->RegisterMessageCallback("editStartupPage",
55 base::Bind(&StartupPagesHandler::EditStartupPage
,
56 base::Unretained(this)));
57 web_ui()->RegisterMessageCallback("setStartupPagesToCurrentPages",
58 base::Bind(&StartupPagesHandler::SetStartupPagesToCurrentPages
,
59 base::Unretained(this)));
60 web_ui()->RegisterMessageCallback("dragDropStartupPage",
61 base::Bind(&StartupPagesHandler::DragDropStartupPage
,
62 base::Unretained(this)));
63 web_ui()->RegisterMessageCallback(
64 "requestAutocompleteSuggestionsForStartupPages",
65 base::Bind(&StartupPagesHandler::RequestAutocompleteSuggestions
,
66 base::Unretained(this)));
67 web_ui()->RegisterMessageCallback("commitStartupPrefChanges",
68 base::Bind(&StartupPagesHandler::CommitChanges
,
69 base::Unretained(this)));
70 web_ui()->RegisterMessageCallback("cancelStartupPrefChanges",
71 base::Bind(&StartupPagesHandler::CancelChanges
,
72 base::Unretained(this)));
75 void StartupPagesHandler::UpdateStartupPages() {
76 Profile
* profile
= Profile::FromWebUI(web_ui());
77 const SessionStartupPref startup_pref
=
78 SessionStartupPref::GetStartupPref(profile
->GetPrefs());
79 startup_custom_pages_table_model_
->SetURLs(startup_pref
.urls
);
82 void StartupPagesHandler::InitializeHandler() {
83 Profile
* profile
= Profile::FromWebUI(web_ui());
85 startup_custom_pages_table_model_
.reset(
86 new CustomHomePagesTableModel(profile
));
87 startup_custom_pages_table_model_
->SetObserver(this);
89 pref_change_registrar_
.Init(profile
->GetPrefs());
90 pref_change_registrar_
.Add(
91 prefs::kURLsToRestoreOnStartup
,
92 base::Bind(&StartupPagesHandler::UpdateStartupPages
,
93 base::Unretained(this)));
95 autocomplete_controller_
.reset(new AutocompleteController(profile
, this,
96 AutocompleteClassifier::kDefaultOmniboxProviders
));
99 void StartupPagesHandler::InitializePage() {
100 UpdateStartupPages();
103 void StartupPagesHandler::OnModelChanged() {
104 base::ListValue startup_pages
;
105 int page_count
= startup_custom_pages_table_model_
->RowCount();
106 std::vector
<GURL
> urls
= startup_custom_pages_table_model_
->GetURLs();
107 for (int i
= 0; i
< page_count
; ++i
) {
108 base::DictionaryValue
* entry
= new base::DictionaryValue();
109 entry
->SetString("title", startup_custom_pages_table_model_
->GetText(i
, 0));
110 entry
->SetString("url", urls
[i
].spec());
111 entry
->SetString("tooltip",
112 startup_custom_pages_table_model_
->GetTooltip(i
));
113 entry
->SetInteger("modelIndex", i
);
114 startup_pages
.Append(entry
);
117 web_ui()->CallJavascriptFunction("StartupOverlay.updateStartupPages",
121 void StartupPagesHandler::OnItemsChanged(int start
, int length
) {
125 void StartupPagesHandler::OnItemsAdded(int start
, int length
) {
129 void StartupPagesHandler::OnItemsRemoved(int start
, int length
) {
133 void StartupPagesHandler::SetStartupPagesToCurrentPages(
134 const base::ListValue
* args
) {
135 startup_custom_pages_table_model_
->SetToCurrentlyOpenPages();
138 void StartupPagesHandler::RemoveStartupPages(const base::ListValue
* args
) {
139 for (int i
= args
->GetSize() - 1; i
>= 0; --i
) {
141 CHECK(args
->GetInteger(i
, &selected_index
));
143 if (selected_index
< 0 ||
144 selected_index
>= startup_custom_pages_table_model_
->RowCount()) {
148 startup_custom_pages_table_model_
->Remove(selected_index
);
152 void StartupPagesHandler::AddStartupPage(const base::ListValue
* args
) {
153 std::string url_string
;
154 CHECK(args
->GetString(0, &url_string
));
156 GURL url
= URLFixerUpper::FixupURL(url_string
, std::string());
160 int row_count
= startup_custom_pages_table_model_
->RowCount();
162 if (!args
->GetInteger(1, &index
) || index
> row_count
)
165 startup_custom_pages_table_model_
->Add(index
, url
);
168 void StartupPagesHandler::EditStartupPage(const base::ListValue
* args
) {
169 std::string url_string
;
171 CHECK_EQ(args
->GetSize(), 2U);
172 CHECK(args
->GetInteger(0, &index
));
173 CHECK(args
->GetString(1, &url_string
));
175 if (index
< 0 || index
> startup_custom_pages_table_model_
->RowCount()) {
180 std::vector
<GURL
> urls
= startup_custom_pages_table_model_
->GetURLs();
181 urls
[index
] = URLFixerUpper::FixupURL(url_string
, std::string());
182 startup_custom_pages_table_model_
->SetURLs(urls
);
185 void StartupPagesHandler::DragDropStartupPage(const base::ListValue
* args
) {
186 CHECK_EQ(args
->GetSize(), 2U);
190 CHECK(args
->GetInteger(0, &to_index
));
192 const base::ListValue
* selected
;
193 CHECK(args
->GetList(1, &selected
));
195 std::vector
<int> index_list
;
196 for (size_t i
= 0; i
< selected
->GetSize(); ++i
) {
198 CHECK(selected
->GetInteger(i
, &index
));
199 index_list
.push_back(index
);
202 startup_custom_pages_table_model_
->MoveURLs(to_index
, index_list
);
205 void StartupPagesHandler::SaveStartupPagesPref() {
206 PrefService
* prefs
= Profile::FromWebUI(web_ui())->GetPrefs();
208 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(prefs
);
209 pref
.urls
= startup_custom_pages_table_model_
->GetURLs();
211 if (pref
.urls
.empty())
212 pref
.type
= SessionStartupPref::DEFAULT
;
214 SessionStartupPref::SetStartupPref(prefs
, pref
);
217 void StartupPagesHandler::CommitChanges(const base::ListValue
* args
) {
218 SaveStartupPagesPref();
221 void StartupPagesHandler::CancelChanges(const base::ListValue
* args
) {
222 UpdateStartupPages();
225 void StartupPagesHandler::RequestAutocompleteSuggestions(
226 const base::ListValue
* args
) {
227 base::string16 input
;
228 CHECK_EQ(args
->GetSize(), 1U);
229 CHECK(args
->GetString(0, &input
));
231 autocomplete_controller_
->Start(AutocompleteInput(
232 input
, base::string16::npos
, base::string16(), GURL(),
233 AutocompleteInput::INVALID_SPEC
, true,
234 false, false, AutocompleteInput::ALL_MATCHES
));
237 void StartupPagesHandler::OnResultChanged(bool default_match_changed
) {
238 const AutocompleteResult
& result
= autocomplete_controller_
->result();
239 base::ListValue suggestions
;
240 OptionsUI::ProcessAutocompleteSuggestions(result
, &suggestions
);
241 web_ui()->CallJavascriptFunction(
242 "StartupOverlay.updateAutocompleteSuggestions", suggestions
);
245 } // namespace options