| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/settings/settings_startup_pages_handler.h" | 5 #include "chrome/browser/ui/webui/settings/settings_startup_pages_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "chrome/browser/prefs/session_startup_pref.h" | 10 #include "chrome/browser/prefs/session_startup_pref.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 13 #include "components/url_formatter/url_fixer.h" | 13 #include "components/url_formatter/url_fixer.h" |
| 14 #include "content/public/browser/web_ui.h" | 14 #include "content/public/browser/web_ui.h" |
| 15 | 15 |
| 16 namespace settings { | 16 namespace settings { |
| 17 | 17 |
| 18 StartupPagesHandler::StartupPagesHandler(content::WebUI* webui) | 18 StartupPagesHandler::StartupPagesHandler(content::WebUI* webui) |
| 19 : startup_custom_pages_table_model_(Profile::FromWebUI(webui)) { | 19 : startup_custom_pages_table_model_(Profile::FromWebUI(webui)) { |
| 20 } | 20 } |
| 21 | 21 |
| 22 StartupPagesHandler::~StartupPagesHandler() { | 22 StartupPagesHandler::~StartupPagesHandler() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 void StartupPagesHandler::RegisterMessages() { | 25 void StartupPagesHandler::RegisterMessages() { |
| 26 if (Profile::FromWebUI(web_ui())->IsOffTheRecord()) | 26 if (Profile::FromWebUI(web_ui())->IsOffTheRecord()) |
| 27 return; | 27 return; |
| 28 | 28 |
| 29 web_ui()->RegisterMessageCallback("addStartupPage", | 29 web_ui()->RegisterMessageCallback("addStartupPage", |
| 30 base::Bind(&StartupPagesHandler::AddStartupPage, | 30 base::Bind(&StartupPagesHandler::HandleAddStartupPage, |
| 31 base::Unretained(this))); | 31 base::Unretained(this))); |
| 32 web_ui()->RegisterMessageCallback("onStartupPrefsPageLoad", | 32 web_ui()->RegisterMessageCallback("onStartupPrefsPageLoad", |
| 33 base::Bind(&StartupPagesHandler::OnStartupPrefsPageLoad, | 33 base::Bind(&StartupPagesHandler::HandleOnStartupPrefsPageLoad, |
| 34 base::Unretained(this))); | 34 base::Unretained(this))); |
| 35 web_ui()->RegisterMessageCallback("removeStartupPage", | 35 web_ui()->RegisterMessageCallback("removeStartupPage", |
| 36 base::Bind(&StartupPagesHandler::RemoveStartupPage, | 36 base::Bind(&StartupPagesHandler::HandleRemoveStartupPage, |
| 37 base::Unretained(this))); | 37 base::Unretained(this))); |
| 38 web_ui()->RegisterMessageCallback("setStartupPagesToCurrentPages", | 38 web_ui()->RegisterMessageCallback("setStartupPagesToCurrentPages", |
| 39 base::Bind(&StartupPagesHandler::SetStartupPagesToCurrentPages, | 39 base::Bind(&StartupPagesHandler::HandleSetStartupPagesToCurrentPages, |
| 40 base::Unretained(this))); | 40 base::Unretained(this))); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void StartupPagesHandler::RenderViewReused() { | 43 void StartupPagesHandler::RenderViewReused() { |
| 44 startup_custom_pages_table_model_.SetObserver(nullptr); | 44 startup_custom_pages_table_model_.SetObserver(nullptr); |
| 45 pref_change_registrar_.RemoveAll(); | 45 pref_change_registrar_.RemoveAll(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void StartupPagesHandler::OnModelChanged() { | 48 void StartupPagesHandler::OnModelChanged() { |
| 49 base::ListValue startup_pages; | 49 base::ListValue startup_pages; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 68 } | 68 } |
| 69 | 69 |
| 70 void StartupPagesHandler::OnItemsAdded(int start, int length) { | 70 void StartupPagesHandler::OnItemsAdded(int start, int length) { |
| 71 OnModelChanged(); | 71 OnModelChanged(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void StartupPagesHandler::OnItemsRemoved(int start, int length) { | 74 void StartupPagesHandler::OnItemsRemoved(int start, int length) { |
| 75 OnModelChanged(); | 75 OnModelChanged(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void StartupPagesHandler::AddStartupPage(const base::ListValue* args) { | 78 void StartupPagesHandler::HandleAddStartupPage(const base::ListValue* args) { |
| 79 std::string url_string; | 79 std::string url_string; |
| 80 if (!args->GetString(0, &url_string)) { | 80 if (!args->GetString(0, &url_string)) { |
| 81 DLOG(ERROR) << "Missing URL string parameter"; | 81 DLOG(ERROR) << "Missing URL string parameter"; |
| 82 return; | 82 return; |
| 83 } | 83 } |
| 84 | 84 |
| 85 GURL url = url_formatter::FixupURL(url_string, std::string()); | 85 GURL url = url_formatter::FixupURL(url_string, std::string()); |
| 86 if (!url.is_valid()) { | 86 if (!url.is_valid()) { |
| 87 LOG(ERROR) << "FixupURL failed on " << url_string; | 87 LOG(ERROR) << "FixupURL failed on " << url_string; |
| 88 return; | 88 return; |
| 89 } | 89 } |
| 90 | 90 |
| 91 int row_count = startup_custom_pages_table_model_.RowCount(); | 91 int row_count = startup_custom_pages_table_model_.RowCount(); |
| 92 int index; | 92 int index; |
| 93 if (!args->GetInteger(1, &index) || index > row_count) | 93 if (!args->GetInteger(1, &index) || index > row_count) |
| 94 index = row_count; | 94 index = row_count; |
| 95 | 95 |
| 96 startup_custom_pages_table_model_.Add(index, url); | 96 startup_custom_pages_table_model_.Add(index, url); |
| 97 SaveStartupPagesPref(); | 97 SaveStartupPagesPref(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void StartupPagesHandler::OnStartupPrefsPageLoad(const base::ListValue* args) { | 100 void StartupPagesHandler::HandleOnStartupPrefsPageLoad( |
| 101 const base::ListValue* args) { |
| 101 startup_custom_pages_table_model_.SetObserver(this); | 102 startup_custom_pages_table_model_.SetObserver(this); |
| 102 | 103 |
| 103 PrefService* prefService = Profile::FromWebUI(web_ui())->GetPrefs(); | 104 PrefService* prefService = Profile::FromWebUI(web_ui())->GetPrefs(); |
| 104 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 105 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 105 prefService); | 106 prefService); |
| 106 startup_custom_pages_table_model_.SetURLs(pref.urls); | 107 startup_custom_pages_table_model_.SetURLs(pref.urls); |
| 107 | 108 |
| 108 if (pref.urls.empty()) | 109 if (pref.urls.empty()) |
| 109 pref.type = SessionStartupPref::DEFAULT; | 110 pref.type = SessionStartupPref::DEFAULT; |
| 110 | 111 |
| 111 pref_change_registrar_.Init(prefService); | 112 pref_change_registrar_.Init(prefService); |
| 112 pref_change_registrar_.Add( | 113 pref_change_registrar_.Add( |
| 113 prefs::kURLsToRestoreOnStartup, | 114 prefs::kURLsToRestoreOnStartup, |
| 114 base::Bind(&StartupPagesHandler::UpdateStartupPages, | 115 base::Bind(&StartupPagesHandler::UpdateStartupPages, |
| 115 base::Unretained(this))); | 116 base::Unretained(this))); |
| 116 | 117 |
| 117 const SessionStartupPref startup_pref = SessionStartupPref::GetStartupPref( | 118 const SessionStartupPref startup_pref = SessionStartupPref::GetStartupPref( |
| 118 Profile::FromWebUI(web_ui())->GetPrefs()); | 119 Profile::FromWebUI(web_ui())->GetPrefs()); |
| 119 startup_custom_pages_table_model_.SetURLs(startup_pref.urls); | 120 startup_custom_pages_table_model_.SetURLs(startup_pref.urls); |
| 120 } | 121 } |
| 121 | 122 |
| 122 void StartupPagesHandler::RemoveStartupPage(const base::ListValue* args) { | 123 void StartupPagesHandler::HandleRemoveStartupPage(const base::ListValue* args) { |
| 123 int selected_index; | 124 int selected_index; |
| 124 if (!args->GetInteger(0, &selected_index)) { | 125 if (!args->GetInteger(0, &selected_index)) { |
| 125 DLOG(ERROR) << "Missing index parameter"; | 126 DLOG(ERROR) << "Missing index parameter"; |
| 126 return; | 127 return; |
| 127 } | 128 } |
| 128 | 129 |
| 129 if (selected_index < 0 || | 130 if (selected_index < 0 || |
| 130 selected_index >= startup_custom_pages_table_model_.RowCount()) { | 131 selected_index >= startup_custom_pages_table_model_.RowCount()) { |
| 131 LOG(ERROR) << "Index out of range " << selected_index; | 132 LOG(ERROR) << "Index out of range " << selected_index; |
| 132 return; | 133 return; |
| 133 } | 134 } |
| 134 | 135 |
| 135 startup_custom_pages_table_model_.Remove(selected_index); | 136 startup_custom_pages_table_model_.Remove(selected_index); |
| 136 SaveStartupPagesPref(); | 137 SaveStartupPagesPref(); |
| 137 } | 138 } |
| 138 | 139 |
| 140 void StartupPagesHandler::HandleSetStartupPagesToCurrentPages( |
| 141 const base::ListValue* args) { |
| 142 startup_custom_pages_table_model_.SetToCurrentlyOpenPages(); |
| 143 SaveStartupPagesPref(); |
| 144 } |
| 145 |
| 139 void StartupPagesHandler::SaveStartupPagesPref() { | 146 void StartupPagesHandler::SaveStartupPagesPref() { |
| 140 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | 147 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
| 141 | 148 |
| 142 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); | 149 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); |
| 143 pref.urls = startup_custom_pages_table_model_.GetURLs(); | 150 pref.urls = startup_custom_pages_table_model_.GetURLs(); |
| 144 | 151 |
| 145 if (pref.urls.empty()) | 152 if (pref.urls.empty()) |
| 146 pref.type = SessionStartupPref::DEFAULT; | 153 pref.type = SessionStartupPref::DEFAULT; |
| 147 | 154 |
| 148 SessionStartupPref::SetStartupPref(prefs, pref); | 155 SessionStartupPref::SetStartupPref(prefs, pref); |
| 149 } | 156 } |
| 150 | 157 |
| 151 void StartupPagesHandler::SetStartupPagesToCurrentPages( | |
| 152 const base::ListValue* args) { | |
| 153 startup_custom_pages_table_model_.SetToCurrentlyOpenPages(); | |
| 154 SaveStartupPagesPref(); | |
| 155 } | |
| 156 | |
| 157 void StartupPagesHandler::UpdateStartupPages() { | 158 void StartupPagesHandler::UpdateStartupPages() { |
| 158 const SessionStartupPref startup_pref = SessionStartupPref::GetStartupPref( | 159 const SessionStartupPref startup_pref = SessionStartupPref::GetStartupPref( |
| 159 Profile::FromWebUI(web_ui())->GetPrefs()); | 160 Profile::FromWebUI(web_ui())->GetPrefs()); |
| 160 startup_custom_pages_table_model_.SetURLs(startup_pref.urls); | 161 startup_custom_pages_table_model_.SetURLs(startup_pref.urls); |
| 161 // The change will go to the JS code in the | 162 // The change will go to the JS code in the |
| 162 // StartupPagesHandler::OnModelChanged() method. | 163 // StartupPagesHandler::OnModelChanged() method. |
| 163 } | 164 } |
| 164 | 165 |
| 165 } // namespace settings | 166 } // namespace settings |
| OLD | NEW |