startup_pages_handler.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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.
4
5#include "chrome/browser/ui/webui/options/startup_pages_handler.h"
6
7#include "base/bind.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"
23
24namespace options {
25
26StartupPagesHandler::StartupPagesHandler() {}
27
28StartupPagesHandler::~StartupPagesHandler() {
29
30}
31
32void StartupPagesHandler::GetLocalizedValues(
33    DictionaryValue* localized_strings) {
34  DCHECK(localized_strings);
35
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 },
40  };
41
42  RegisterStrings(localized_strings, resources, arraysize(resources));
43  RegisterTitle(localized_strings, "startupPagesOverlay",
44                IDS_OPTIONS_STARTUP_PAGES_DIALOG_TITLE);
45}
46
47void 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)));
73}
74
75void 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);
80}
81
82void StartupPagesHandler::InitializeHandler() {
83  Profile* profile = Profile::FromWebUI(web_ui());
84
85  startup_custom_pages_table_model_.reset(
86      new CustomHomePagesTableModel(profile));
87  startup_custom_pages_table_model_->SetObserver(this);
88
89  pref_change_registrar_.Init(profile->GetPrefs());
90  pref_change_registrar_.Add(
91      prefs::kURLsToRestoreOnStartup,
92      base::Bind(&StartupPagesHandler::UpdateStartupPages,
93                 base::Unretained(this)));
94
95  autocomplete_controller_.reset(new AutocompleteController(profile, this,
96      AutocompleteClassifier::kDefaultOmniboxProviders));
97}
98
99void StartupPagesHandler::InitializePage() {
100  UpdateStartupPages();
101}
102
103void StartupPagesHandler::OnModelChanged() {
104  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    DictionaryValue* entry = new 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);
115  }
116
117  web_ui()->CallJavascriptFunction("StartupOverlay.updateStartupPages",
118                                   startup_pages);
119}
120
121void StartupPagesHandler::OnItemsChanged(int start, int length) {
122  OnModelChanged();
123}
124
125void StartupPagesHandler::OnItemsAdded(int start, int length) {
126  OnModelChanged();
127}
128
129void StartupPagesHandler::OnItemsRemoved(int start, int length) {
130  OnModelChanged();
131}
132
133void StartupPagesHandler::SetStartupPagesToCurrentPages(
134    const ListValue* args) {
135  startup_custom_pages_table_model_->SetToCurrentlyOpenPages();
136}
137
138void StartupPagesHandler::RemoveStartupPages(const ListValue* args) {
139  for (int i = args->GetSize() - 1; i >= 0; --i) {
140    int selected_index;
141    CHECK(args->GetInteger(i, &selected_index));
142
143    if (selected_index < 0 ||
144        selected_index >= startup_custom_pages_table_model_->RowCount()) {
145      NOTREACHED();
146      return;
147    }
148    startup_custom_pages_table_model_->Remove(selected_index);
149  }
150}
151
152void StartupPagesHandler::AddStartupPage(const ListValue* args) {
153  std::string url_string;
154  CHECK(args->GetString(0, &url_string));
155
156  GURL url = URLFixerUpper::FixupURL(url_string, std::string());
157  if (!url.is_valid())
158    return;
159
160  int row_count = startup_custom_pages_table_model_->RowCount();
161  int index;
162  if (!args->GetInteger(1, &index) || index > row_count)
163    index = row_count;
164
165  startup_custom_pages_table_model_->Add(index, url);
166}
167
168void StartupPagesHandler::EditStartupPage(const ListValue* args) {
169  std::string url_string;
170  int index;
171  CHECK_EQ(args->GetSize(), 2U);
172  CHECK(args->GetInteger(0, &index));
173  CHECK(args->GetString(1, &url_string));
174
175  if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) {
176    NOTREACHED();
177    return;
178  }
179
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);
183}
184
185void StartupPagesHandler::DragDropStartupPage(const ListValue* args) {
186  CHECK_EQ(args->GetSize(), 2U);
187
188  int to_index;
189
190  CHECK(args->GetInteger(0, &to_index));
191
192  const ListValue* selected;
193  CHECK(args->GetList(1, &selected));
194
195  std::vector<int> index_list;
196  for (size_t i = 0; i < selected->GetSize(); ++i) {
197    int index;
198    CHECK(selected->GetInteger(i, &index));
199    index_list.push_back(index);
200  }
201
202  startup_custom_pages_table_model_->MoveURLs(to_index, index_list);
203}
204
205void StartupPagesHandler::SaveStartupPagesPref() {
206  PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
207
208  SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs);
209  pref.urls = startup_custom_pages_table_model_->GetURLs();
210
211  if (pref.urls.empty())
212    pref.type = SessionStartupPref::DEFAULT;
213
214  SessionStartupPref::SetStartupPref(prefs, pref);
215}
216
217void StartupPagesHandler::CommitChanges(const ListValue* args) {
218  SaveStartupPagesPref();
219}
220
221void StartupPagesHandler::CancelChanges(const ListValue* args) {
222  UpdateStartupPages();
223}
224
225void StartupPagesHandler::RequestAutocompleteSuggestions(
226    const ListValue* args) {
227  string16 input;
228  CHECK_EQ(args->GetSize(), 1U);
229  CHECK(args->GetString(0, &input));
230
231  autocomplete_controller_->Start(AutocompleteInput(
232      input, string16::npos, string16(), GURL(),
233      AutocompleteInput::INVALID_SPEC, true,
234      false, false, AutocompleteInput::ALL_MATCHES));
235}
236
237void StartupPagesHandler::OnResultChanged(bool default_match_changed) {
238  const AutocompleteResult& result = autocomplete_controller_->result();
239  ListValue suggestions;
240  OptionsUI::ProcessAutocompleteSuggestions(result, &suggestions);
241  web_ui()->CallJavascriptFunction(
242      "StartupOverlay.updateAutocompleteSuggestions", suggestions);
243}
244
245}  // namespace options
246