| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/startup_pages_handler.h" | 5 #include "chrome/browser/ui/webui/options/startup_pages_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 10 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 11 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_input.h" | 12 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 14 #include "chrome/browser/autocomplete/autocomplete_result.h" | 13 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 15 #include "chrome/browser/custom_home_pages_table_model.h" | 14 #include "chrome/browser/custom_home_pages_table_model.h" |
| 16 #include "chrome/browser/net/url_fixer_upper.h" | 15 #include "chrome/browser/net/url_fixer_upper.h" |
| 17 #include "chrome/browser/prefs/session_startup_pref.h" | 16 #include "chrome/browser/prefs/session_startup_pref.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 20 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 void StartupPagesHandler::OnModelChanged() { | 105 void StartupPagesHandler::OnModelChanged() { |
| 107 ListValue startup_pages; | 106 ListValue startup_pages; |
| 108 int page_count = startup_custom_pages_table_model_->RowCount(); | 107 int page_count = startup_custom_pages_table_model_->RowCount(); |
| 109 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); | 108 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); |
| 110 for (int i = 0; i < page_count; ++i) { | 109 for (int i = 0; i < page_count; ++i) { |
| 111 DictionaryValue* entry = new DictionaryValue(); | 110 DictionaryValue* entry = new DictionaryValue(); |
| 112 entry->SetString("title", startup_custom_pages_table_model_->GetText(i, 0)); | 111 entry->SetString("title", startup_custom_pages_table_model_->GetText(i, 0)); |
| 113 entry->SetString("url", urls[i].spec()); | 112 entry->SetString("url", urls[i].spec()); |
| 114 entry->SetString("tooltip", | 113 entry->SetString("tooltip", |
| 115 startup_custom_pages_table_model_->GetTooltip(i)); | 114 startup_custom_pages_table_model_->GetTooltip(i)); |
| 116 entry->SetString("modelIndex", base::IntToString(i)); | 115 entry->SetInteger("modelIndex", i); |
| 117 startup_pages.Append(entry); | 116 startup_pages.Append(entry); |
| 118 } | 117 } |
| 119 | 118 |
| 120 web_ui()->CallJavascriptFunction("StartupOverlay.updateStartupPages", | 119 web_ui()->CallJavascriptFunction("StartupOverlay.updateStartupPages", |
| 121 startup_pages); | 120 startup_pages); |
| 122 } | 121 } |
| 123 | 122 |
| 124 void StartupPagesHandler::OnItemsChanged(int start, int length) { | 123 void StartupPagesHandler::OnItemsChanged(int start, int length) { |
| 125 OnModelChanged(); | 124 OnModelChanged(); |
| 126 } | 125 } |
| 127 | 126 |
| 128 void StartupPagesHandler::OnItemsAdded(int start, int length) { | 127 void StartupPagesHandler::OnItemsAdded(int start, int length) { |
| 129 OnModelChanged(); | 128 OnModelChanged(); |
| 130 } | 129 } |
| 131 | 130 |
| 132 void StartupPagesHandler::OnItemsRemoved(int start, int length) { | 131 void StartupPagesHandler::OnItemsRemoved(int start, int length) { |
| 133 OnModelChanged(); | 132 OnModelChanged(); |
| 134 } | 133 } |
| 135 | 134 |
| 136 void StartupPagesHandler::SetStartupPagesToCurrentPages( | 135 void StartupPagesHandler::SetStartupPagesToCurrentPages( |
| 137 const ListValue* args) { | 136 const ListValue* args) { |
| 138 startup_custom_pages_table_model_->SetToCurrentlyOpenPages(); | 137 startup_custom_pages_table_model_->SetToCurrentlyOpenPages(); |
| 139 } | 138 } |
| 140 | 139 |
| 141 void StartupPagesHandler::RemoveStartupPages(const ListValue* args) { | 140 void StartupPagesHandler::RemoveStartupPages(const ListValue* args) { |
| 142 for (int i = args->GetSize() - 1; i >= 0; --i) { | 141 for (int i = args->GetSize() - 1; i >= 0; --i) { |
| 143 std::string string_value; | 142 int selected_index; |
| 144 CHECK(args->GetString(i, &string_value)); | 143 CHECK(args->GetInteger(i, &selected_index)); |
| 145 | 144 |
| 146 int selected_index; | |
| 147 base::StringToInt(string_value, &selected_index); | |
| 148 if (selected_index < 0 || | 145 if (selected_index < 0 || |
| 149 selected_index >= startup_custom_pages_table_model_->RowCount()) { | 146 selected_index >= startup_custom_pages_table_model_->RowCount()) { |
| 150 NOTREACHED(); | 147 NOTREACHED(); |
| 151 return; | 148 return; |
| 152 } | 149 } |
| 153 startup_custom_pages_table_model_->Remove(selected_index); | 150 startup_custom_pages_table_model_->Remove(selected_index); |
| 154 } | 151 } |
| 155 } | 152 } |
| 156 | 153 |
| 157 void StartupPagesHandler::AddStartupPage(const ListValue* args) { | 154 void StartupPagesHandler::AddStartupPage(const ListValue* args) { |
| 158 std::string url_string; | 155 std::string url_string; |
| 159 CHECK_EQ(args->GetSize(), 1U); | 156 CHECK_EQ(args->GetSize(), 1U); |
| 160 CHECK(args->GetString(0, &url_string)); | 157 CHECK(args->GetString(0, &url_string)); |
| 161 | 158 |
| 162 GURL url = URLFixerUpper::FixupURL(url_string, std::string()); | 159 GURL url = URLFixerUpper::FixupURL(url_string, std::string()); |
| 163 if (!url.is_valid()) | 160 if (!url.is_valid()) |
| 164 return; | 161 return; |
| 165 int index = startup_custom_pages_table_model_->RowCount(); | 162 int index = startup_custom_pages_table_model_->RowCount(); |
| 166 startup_custom_pages_table_model_->Add(index, url); | 163 startup_custom_pages_table_model_->Add(index, url); |
| 167 } | 164 } |
| 168 | 165 |
| 169 void StartupPagesHandler::EditStartupPage(const ListValue* args) { | 166 void StartupPagesHandler::EditStartupPage(const ListValue* args) { |
| 170 std::string url_string; | 167 std::string url_string; |
| 171 std::string index_string; | |
| 172 int index; | 168 int index; |
| 173 CHECK_EQ(args->GetSize(), 2U); | 169 CHECK_EQ(args->GetSize(), 2U); |
| 174 CHECK(args->GetString(0, &index_string)); | 170 CHECK(args->GetInteger(0, &index)); |
| 175 CHECK(base::StringToInt(index_string, &index)); | |
| 176 CHECK(args->GetString(1, &url_string)); | 171 CHECK(args->GetString(1, &url_string)); |
| 177 | 172 |
| 178 if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) { | 173 if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) { |
| 179 NOTREACHED(); | 174 NOTREACHED(); |
| 180 return; | 175 return; |
| 181 } | 176 } |
| 182 | 177 |
| 183 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); | 178 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); |
| 184 urls[index] = URLFixerUpper::FixupURL(url_string, std::string()); | 179 urls[index] = URLFixerUpper::FixupURL(url_string, std::string()); |
| 185 startup_custom_pages_table_model_->SetURLs(urls); | 180 startup_custom_pages_table_model_->SetURLs(urls); |
| 186 } | 181 } |
| 187 | 182 |
| 188 void StartupPagesHandler::DragDropStartupPage(const ListValue* args) { | 183 void StartupPagesHandler::DragDropStartupPage(const ListValue* args) { |
| 189 CHECK_EQ(args->GetSize(), 2U); | 184 CHECK_EQ(args->GetSize(), 2U); |
| 190 | 185 |
| 191 // TODO(dcheng): Due to http://crbug.com/122102, we can receive drag and drop | |
| 192 // events even if there are no entries in the model. Remove this check once | |
| 193 // that bug is fixed. | |
| 194 if (startup_custom_pages_table_model_->RowCount() == 0) | |
| 195 return; | |
| 196 | |
| 197 std::string value; | |
| 198 int to_index; | 186 int to_index; |
| 199 | 187 |
| 200 CHECK(args->GetString(0, &value)); | 188 CHECK(args->GetInteger(0, &to_index)); |
| 201 base::StringToInt(value, &to_index); | |
| 202 | 189 |
| 203 const ListValue* selected; | 190 const ListValue* selected; |
| 204 CHECK(args->GetList(1, &selected)); | 191 CHECK(args->GetList(1, &selected)); |
| 205 | 192 |
| 206 std::vector<int> index_list; | 193 std::vector<int> index_list; |
| 207 for (size_t i = 0; i < selected->GetSize(); ++i) { | 194 for (size_t i = 0; i < selected->GetSize(); ++i) { |
| 208 int index; | 195 int index; |
| 209 CHECK(selected->GetString(i, &value)); | 196 CHECK(selected->GetInteger(i, &index)); |
| 210 base::StringToInt(value, &index); | |
| 211 index_list.push_back(index); | 197 index_list.push_back(index); |
| 212 } | 198 } |
| 213 | 199 |
| 214 startup_custom_pages_table_model_->MoveURLs(to_index, index_list); | 200 startup_custom_pages_table_model_->MoveURLs(to_index, index_list); |
| 215 } | 201 } |
| 216 | 202 |
| 217 void StartupPagesHandler::SaveStartupPagesPref() { | 203 void StartupPagesHandler::SaveStartupPagesPref() { |
| 218 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | 204 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
| 219 | 205 |
| 220 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); | 206 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 247 | 233 |
| 248 void StartupPagesHandler::OnResultChanged(bool default_match_changed) { | 234 void StartupPagesHandler::OnResultChanged(bool default_match_changed) { |
| 249 const AutocompleteResult& result = autocomplete_controller_->result(); | 235 const AutocompleteResult& result = autocomplete_controller_->result(); |
| 250 ListValue suggestions; | 236 ListValue suggestions; |
| 251 OptionsUI::ProcessAutocompleteSuggestions(result, &suggestions); | 237 OptionsUI::ProcessAutocompleteSuggestions(result, &suggestions); |
| 252 web_ui()->CallJavascriptFunction( | 238 web_ui()->CallJavascriptFunction( |
| 253 "StartupOverlay.updateAutocompleteSuggestions", suggestions); | 239 "StartupOverlay.updateAutocompleteSuggestions", suggestions); |
| 254 } | 240 } |
| 255 | 241 |
| 256 } // namespace options | 242 } // namespace options |
| OLD | NEW |