startup_pages_handler.cc revision d3868032626d59662ff73b372b5d584c1d144c53
1eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/ui/webui/options/startup_pages_handler.h" 6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/prefs/pref_service.h" 107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/autocomplete/autocomplete_classifier.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/autocomplete_controller.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/autocomplete_input.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/autocomplete_result.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chrome_notification_types.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/custom_home_pages_table_model.h" 167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/prefs/session_startup_pref.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#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_EQ(args->GetSize(), 1U); 155 CHECK(args->GetString(0, &url_string)); 156 157 GURL url = URLFixerUpper::FixupURL(url_string, std::string()); 158 if (!url.is_valid()) 159 return; 160 int index = startup_custom_pages_table_model_->RowCount(); 161 startup_custom_pages_table_model_->Add(index, url); 162} 163 164void StartupPagesHandler::EditStartupPage(const ListValue* args) { 165 std::string url_string; 166 int index; 167 CHECK_EQ(args->GetSize(), 2U); 168 CHECK(args->GetInteger(0, &index)); 169 CHECK(args->GetString(1, &url_string)); 170 171 if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) { 172 NOTREACHED(); 173 return; 174 } 175 176 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); 177 urls[index] = URLFixerUpper::FixupURL(url_string, std::string()); 178 startup_custom_pages_table_model_->SetURLs(urls); 179} 180 181void StartupPagesHandler::DragDropStartupPage(const ListValue* args) { 182 CHECK_EQ(args->GetSize(), 2U); 183 184 int to_index; 185 186 CHECK(args->GetInteger(0, &to_index)); 187 188 const ListValue* selected; 189 CHECK(args->GetList(1, &selected)); 190 191 std::vector<int> index_list; 192 for (size_t i = 0; i < selected->GetSize(); ++i) { 193 int index; 194 CHECK(selected->GetInteger(i, &index)); 195 index_list.push_back(index); 196 } 197 198 startup_custom_pages_table_model_->MoveURLs(to_index, index_list); 199} 200 201void StartupPagesHandler::SaveStartupPagesPref() { 202 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); 203 204 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); 205 pref.urls = startup_custom_pages_table_model_->GetURLs(); 206 207 if (pref.urls.empty()) 208 pref.type = SessionStartupPref::DEFAULT; 209 210 SessionStartupPref::SetStartupPref(prefs, pref); 211} 212 213void StartupPagesHandler::CommitChanges(const ListValue* args) { 214 SaveStartupPagesPref(); 215} 216 217void StartupPagesHandler::CancelChanges(const ListValue* args) { 218 UpdateStartupPages(); 219} 220 221void StartupPagesHandler::RequestAutocompleteSuggestions( 222 const ListValue* args) { 223 string16 input; 224 CHECK_EQ(args->GetSize(), 1U); 225 CHECK(args->GetString(0, &input)); 226 227 autocomplete_controller_->Start(AutocompleteInput( 228 input, string16::npos, string16(), GURL(), 229 AutocompleteInput::INVALID_SPEC, true, 230 false, false, AutocompleteInput::ALL_MATCHES)); 231} 232 233void StartupPagesHandler::OnResultChanged(bool default_match_changed) { 234 const AutocompleteResult& result = autocomplete_controller_->result(); 235 ListValue suggestions; 236 OptionsUI::ProcessAutocompleteSuggestions(result, &suggestions); 237 web_ui()->CallJavascriptFunction( 238 "StartupOverlay.updateAutocompleteSuggestions", suggestions); 239} 240 241} // namespace options 242