| OLD | NEW |
| (Empty) |
| 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/options2/startup_pages_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "chrome/browser/autocomplete/autocomplete_controller.h" | |
| 11 #include "chrome/browser/autocomplete/autocomplete_input.h" | |
| 12 #include "chrome/browser/autocomplete/autocomplete_result.h" | |
| 13 #include "chrome/browser/custom_home_pages_table_model.h" | |
| 14 #include "chrome/browser/net/url_fixer_upper.h" | |
| 15 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/common/chrome_notification_types.h" | |
| 18 #include "chrome/common/pref_names.h" | |
| 19 #include "content/public/browser/notification_details.h" | |
| 20 #include "content/public/browser/web_ui.h" | |
| 21 #include "grit/generated_resources.h" | |
| 22 | |
| 23 namespace options { | |
| 24 | |
| 25 StartupPagesHandler::StartupPagesHandler() | |
| 26 : startup_custom_pages_table_model_(NULL) { | |
| 27 } | |
| 28 | |
| 29 StartupPagesHandler::~StartupPagesHandler() { | |
| 30 | |
| 31 } | |
| 32 | |
| 33 void StartupPagesHandler::GetLocalizedValues( | |
| 34 DictionaryValue* localized_strings) { | |
| 35 DCHECK(localized_strings); | |
| 36 | |
| 37 static OptionsStringResource resources[] = { | |
| 38 { "startupAddLabel", IDS_OPTIONS_STARTUP_ADD_LABEL }, | |
| 39 { "startupUseCurrent", IDS_OPTIONS_STARTUP_USE_CURRENT }, | |
| 40 { "startupPagesPlaceholder", IDS_OPTIONS_STARTUP_PAGES_PLACEHOLDER }, | |
| 41 }; | |
| 42 | |
| 43 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 44 RegisterTitle(localized_strings, "startupPagesOverlay", | |
| 45 IDS_OPTIONS2_STARTUP_PAGES_DIALOG_TITLE); | |
| 46 } | |
| 47 | |
| 48 void StartupPagesHandler::RegisterMessages() { | |
| 49 web_ui()->RegisterMessageCallback("removeStartupPages", | |
| 50 base::Bind(&StartupPagesHandler::RemoveStartupPages, | |
| 51 base::Unretained(this))); | |
| 52 web_ui()->RegisterMessageCallback("addStartupPage", | |
| 53 base::Bind(&StartupPagesHandler::AddStartupPage, | |
| 54 base::Unretained(this))); | |
| 55 web_ui()->RegisterMessageCallback("editStartupPage", | |
| 56 base::Bind(&StartupPagesHandler::EditStartupPage, | |
| 57 base::Unretained(this))); | |
| 58 web_ui()->RegisterMessageCallback("setStartupPagesToCurrentPages", | |
| 59 base::Bind(&StartupPagesHandler::SetStartupPagesToCurrentPages, | |
| 60 base::Unretained(this))); | |
| 61 web_ui()->RegisterMessageCallback("dragDropStartupPage", | |
| 62 base::Bind(&StartupPagesHandler::DragDropStartupPage, | |
| 63 base::Unretained(this))); | |
| 64 web_ui()->RegisterMessageCallback( | |
| 65 "requestAutocompleteSuggestionsForStartupPages", | |
| 66 base::Bind(&StartupPagesHandler::RequestAutocompleteSuggestions, | |
| 67 base::Unretained(this))); | |
| 68 web_ui()->RegisterMessageCallback("commitStartupPrefChanges", | |
| 69 base::Bind(&StartupPagesHandler::CommitChanges, | |
| 70 base::Unretained(this))); | |
| 71 web_ui()->RegisterMessageCallback("cancelStartupPrefChanges", | |
| 72 base::Bind(&StartupPagesHandler::CancelChanges, | |
| 73 base::Unretained(this))); | |
| 74 } | |
| 75 | |
| 76 void StartupPagesHandler::UpdateStartupPages() { | |
| 77 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 78 const SessionStartupPref startup_pref = | |
| 79 SessionStartupPref::GetStartupPref(profile->GetPrefs()); | |
| 80 startup_custom_pages_table_model_->SetURLs(startup_pref.urls); | |
| 81 } | |
| 82 | |
| 83 void StartupPagesHandler::InitializeHandler() { | |
| 84 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 85 | |
| 86 startup_custom_pages_table_model_.reset( | |
| 87 new CustomHomePagesTableModel(profile)); | |
| 88 startup_custom_pages_table_model_->SetObserver(this); | |
| 89 | |
| 90 pref_change_registrar_.Init(profile->GetPrefs()); | |
| 91 pref_change_registrar_.Add(prefs::kURLsToRestoreOnStartup, this); | |
| 92 | |
| 93 autocomplete_controller_.reset(new AutocompleteController(profile, this)); | |
| 94 } | |
| 95 | |
| 96 void StartupPagesHandler::InitializePage() { | |
| 97 UpdateStartupPages(); | |
| 98 } | |
| 99 | |
| 100 void StartupPagesHandler::OnModelChanged() { | |
| 101 ListValue startup_pages; | |
| 102 int page_count = startup_custom_pages_table_model_->RowCount(); | |
| 103 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); | |
| 104 for (int i = 0; i < page_count; ++i) { | |
| 105 DictionaryValue* entry = new DictionaryValue(); | |
| 106 entry->SetString("title", startup_custom_pages_table_model_->GetText(i, 0)); | |
| 107 entry->SetString("url", urls[i].spec()); | |
| 108 entry->SetString("tooltip", | |
| 109 startup_custom_pages_table_model_->GetTooltip(i)); | |
| 110 entry->SetString("modelIndex", base::IntToString(i)); | |
| 111 startup_pages.Append(entry); | |
| 112 } | |
| 113 | |
| 114 web_ui()->CallJavascriptFunction("StartupOverlay.updateStartupPages", | |
| 115 startup_pages); | |
| 116 } | |
| 117 | |
| 118 void StartupPagesHandler::OnItemsChanged(int start, int length) { | |
| 119 OnModelChanged(); | |
| 120 } | |
| 121 | |
| 122 void StartupPagesHandler::OnItemsAdded(int start, int length) { | |
| 123 OnModelChanged(); | |
| 124 } | |
| 125 | |
| 126 void StartupPagesHandler::OnItemsRemoved(int start, int length) { | |
| 127 OnModelChanged(); | |
| 128 } | |
| 129 | |
| 130 void StartupPagesHandler::Observe( | |
| 131 int type, | |
| 132 const content::NotificationSource& source, | |
| 133 const content::NotificationDetails& details) { | |
| 134 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
| 135 std::string* pref = content::Details<std::string>(details).ptr(); | |
| 136 if (*pref == prefs::kURLsToRestoreOnStartup) { | |
| 137 UpdateStartupPages(); | |
| 138 } else { | |
| 139 NOTREACHED(); | |
| 140 } | |
| 141 } else { | |
| 142 NOTREACHED(); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 void StartupPagesHandler::SetStartupPagesToCurrentPages( | |
| 147 const ListValue* args) { | |
| 148 startup_custom_pages_table_model_->SetToCurrentlyOpenPages(); | |
| 149 } | |
| 150 | |
| 151 void StartupPagesHandler::RemoveStartupPages(const ListValue* args) { | |
| 152 for (int i = args->GetSize() - 1; i >= 0; --i) { | |
| 153 std::string string_value; | |
| 154 CHECK(args->GetString(i, &string_value)); | |
| 155 | |
| 156 int selected_index; | |
| 157 base::StringToInt(string_value, &selected_index); | |
| 158 if (selected_index < 0 || | |
| 159 selected_index >= startup_custom_pages_table_model_->RowCount()) { | |
| 160 NOTREACHED(); | |
| 161 return; | |
| 162 } | |
| 163 startup_custom_pages_table_model_->Remove(selected_index); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 void StartupPagesHandler::AddStartupPage(const ListValue* args) { | |
| 168 std::string url_string; | |
| 169 CHECK_EQ(args->GetSize(), 1U); | |
| 170 CHECK(args->GetString(0, &url_string)); | |
| 171 | |
| 172 GURL url = URLFixerUpper::FixupURL(url_string, std::string()); | |
| 173 if (!url.is_valid()) | |
| 174 return; | |
| 175 int index = startup_custom_pages_table_model_->RowCount(); | |
| 176 startup_custom_pages_table_model_->Add(index, url); | |
| 177 } | |
| 178 | |
| 179 void StartupPagesHandler::EditStartupPage(const ListValue* args) { | |
| 180 std::string url_string; | |
| 181 std::string index_string; | |
| 182 int index; | |
| 183 CHECK_EQ(args->GetSize(), 2U); | |
| 184 CHECK(args->GetString(0, &index_string)); | |
| 185 CHECK(base::StringToInt(index_string, &index)); | |
| 186 CHECK(args->GetString(1, &url_string)); | |
| 187 | |
| 188 if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) { | |
| 189 NOTREACHED(); | |
| 190 return; | |
| 191 } | |
| 192 | |
| 193 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); | |
| 194 urls[index] = URLFixerUpper::FixupURL(url_string, std::string()); | |
| 195 startup_custom_pages_table_model_->SetURLs(urls); | |
| 196 } | |
| 197 | |
| 198 void StartupPagesHandler::DragDropStartupPage(const ListValue* args) { | |
| 199 CHECK_EQ(args->GetSize(), 2U); | |
| 200 | |
| 201 std::string value; | |
| 202 int to_index; | |
| 203 | |
| 204 CHECK(args->GetString(0, &value)); | |
| 205 base::StringToInt(value, &to_index); | |
| 206 | |
| 207 const ListValue* selected; | |
| 208 CHECK(args->GetList(1, &selected)); | |
| 209 | |
| 210 std::vector<int> index_list; | |
| 211 for (size_t i = 0; i < selected->GetSize(); ++i) { | |
| 212 int index; | |
| 213 CHECK(selected->GetString(i, &value)); | |
| 214 base::StringToInt(value, &index); | |
| 215 index_list.push_back(index); | |
| 216 } | |
| 217 | |
| 218 startup_custom_pages_table_model_->MoveURLs(to_index, index_list); | |
| 219 } | |
| 220 | |
| 221 void StartupPagesHandler::SaveStartupPagesPref() { | |
| 222 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | |
| 223 | |
| 224 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); | |
| 225 pref.urls = startup_custom_pages_table_model_->GetURLs(); | |
| 226 | |
| 227 if (pref.urls.empty()) | |
| 228 pref.type = SessionStartupPref::DEFAULT; | |
| 229 | |
| 230 SessionStartupPref::SetStartupPref(prefs, pref); | |
| 231 } | |
| 232 | |
| 233 void StartupPagesHandler::CommitChanges(const ListValue* args) { | |
| 234 SaveStartupPagesPref(); | |
| 235 } | |
| 236 | |
| 237 void StartupPagesHandler::CancelChanges(const ListValue* args) { | |
| 238 UpdateStartupPages(); | |
| 239 } | |
| 240 | |
| 241 void StartupPagesHandler::RequestAutocompleteSuggestions( | |
| 242 const ListValue* args) { | |
| 243 string16 input; | |
| 244 CHECK_EQ(args->GetSize(), 1U); | |
| 245 CHECK(args->GetString(0, &input)); | |
| 246 | |
| 247 autocomplete_controller_->Start(input, string16(), true, false, false, | |
| 248 AutocompleteInput::ALL_MATCHES); | |
| 249 } | |
| 250 | |
| 251 void StartupPagesHandler::OnResultChanged(bool default_match_changed) { | |
| 252 const AutocompleteResult& result = autocomplete_controller_->result(); | |
| 253 ListValue suggestions; | |
| 254 OptionsUI::ProcessAutocompleteSuggestions(result, &suggestions); | |
| 255 web_ui()->CallJavascriptFunction( | |
| 256 "StartupOverlay.updateAutocompleteSuggestions", suggestions); | |
| 257 } | |
| 258 | |
| 259 } // namespace options | |
| OLD | NEW |