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/md_history_ui.h" | 5 #include "chrome/browser/ui/webui/md_history_ui.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/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/values.h" | |
10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
13 #include "chrome/browser/ui/webui/browsing_history_handler.h" | 14 #include "chrome/browser/ui/webui/browsing_history_handler.h" |
14 #include "chrome/browser/ui/webui/foreign_session_handler.h" | 15 #include "chrome/browser/ui/webui/foreign_session_handler.h" |
15 #include "chrome/browser/ui/webui/history_login_handler.h" | 16 #include "chrome/browser/ui/webui/history_login_handler.h" |
16 #include "chrome/browser/ui/webui/metrics_handler.h" | 17 #include "chrome/browser/ui/webui/metrics_handler.h" |
17 #include "chrome/common/chrome_features.h" | 18 #include "chrome/common/chrome_features.h" |
18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
20 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
21 #include "chrome/grit/browser_resources.h" | 22 #include "chrome/grit/browser_resources.h" |
22 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
23 #include "chrome/grit/locale_settings.h" | 24 #include "chrome/grit/locale_settings.h" |
24 #include "chrome/grit/theme_resources.h" | 25 #include "chrome/grit/theme_resources.h" |
25 #include "components/grit/components_scaled_resources.h" | 26 #include "components/grit/components_scaled_resources.h" |
26 #include "components/pref_registry/pref_registry_syncable.h" | 27 #include "components/pref_registry/pref_registry_syncable.h" |
27 #include "components/prefs/pref_service.h" | 28 #include "components/prefs/pref_service.h" |
28 #include "components/search/search.h" | 29 #include "components/search/search.h" |
29 #include "components/signin/core/browser/signin_manager.h" | 30 #include "components/signin/core/browser/signin_manager.h" |
30 #include "components/strings/grit/components_strings.h" | 31 #include "components/strings/grit/components_strings.h" |
31 #include "content/public/browser/web_ui.h" | 32 #include "content/public/browser/web_ui.h" |
32 #include "content/public/browser/web_ui_data_source.h" | 33 #include "content/public/browser/web_ui_data_source.h" |
33 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
34 #include "ui/base/resource/resource_bundle.h" | 35 #include "ui/base/resource/resource_bundle.h" |
35 | 36 |
36 namespace { | 37 namespace { |
37 | 38 |
39 constexpr char kIsUserSignedInKey[] = "isUserSignedIn"; | |
40 constexpr char kShowMenuPromoKey[] = "showMenuPromo"; | |
41 | |
42 bool IsUserSignedIn(Profile* profile) { | |
43 SigninManagerBase* signin_manager = | |
44 SigninManagerFactory::GetForProfile(profile); | |
45 return signin_manager && signin_manager->IsAuthenticated(); | |
46 } | |
47 | |
48 bool MenuPromoShown(Profile* profile) { | |
49 return profile->GetPrefs()->GetBoolean(prefs::kMdHistoryMenuPromoShown); | |
50 } | |
51 | |
38 content::WebUIDataSource* CreateMdHistoryUIHTMLSource(Profile* profile, | 52 content::WebUIDataSource* CreateMdHistoryUIHTMLSource(Profile* profile, |
39 bool use_test_title) { | 53 bool use_test_title) { |
40 content::WebUIDataSource* source = | 54 content::WebUIDataSource* source = |
41 content::WebUIDataSource::Create(chrome::kChromeUIHistoryHost); | 55 content::WebUIDataSource::Create(chrome::kChromeUIHistoryHost); |
42 | 56 |
43 // Localized strings (alphabetical order). | 57 // Localized strings (alphabetical order). |
44 source->AddLocalizedString("bookmarked", IDS_HISTORY_ENTRY_BOOKMARKED); | 58 source->AddLocalizedString("bookmarked", IDS_HISTORY_ENTRY_BOOKMARKED); |
45 source->AddLocalizedString("cancel", IDS_CANCEL); | 59 source->AddLocalizedString("cancel", IDS_CANCEL); |
46 source->AddLocalizedString("clearBrowsingData", | 60 source->AddLocalizedString("clearBrowsingData", |
47 IDS_CLEAR_BROWSING_DATA_TITLE); | 61 IDS_CLEAR_BROWSING_DATA_TITLE); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 l10n_util::GetStringFUTF16( | 124 l10n_util::GetStringFUTF16( |
111 IDS_HISTORY_OTHER_FORMS_OF_HISTORY, | 125 IDS_HISTORY_OTHER_FORMS_OF_HISTORY, |
112 l10n_util::GetStringUTF16( | 126 l10n_util::GetStringUTF16( |
113 IDS_SETTINGS_CLEAR_DATA_WEB_HISTORY_URL_IN_HISTORY))); | 127 IDS_SETTINGS_CLEAR_DATA_WEB_HISTORY_URL_IN_HISTORY))); |
114 | 128 |
115 PrefService* prefs = profile->GetPrefs(); | 129 PrefService* prefs = profile->GetPrefs(); |
116 bool allow_deleting_history = | 130 bool allow_deleting_history = |
117 prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory); | 131 prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory); |
118 source->AddBoolean("allowDeletingHistory", allow_deleting_history); | 132 source->AddBoolean("allowDeletingHistory", allow_deleting_history); |
119 | 133 |
120 source->AddBoolean("showMenuPromo", | 134 source->AddBoolean(kShowMenuPromoKey, !MenuPromoShown(profile)); |
121 !prefs->GetBoolean(prefs::kMdHistoryMenuPromoShown)); | |
122 | 135 |
123 bool group_by_domain = base::CommandLine::ForCurrentProcess()->HasSwitch( | 136 bool group_by_domain = base::CommandLine::ForCurrentProcess()->HasSwitch( |
124 switches::kHistoryEnableGroupByDomain) || profile->IsSupervised(); | 137 switches::kHistoryEnableGroupByDomain) || profile->IsSupervised(); |
125 source->AddBoolean("groupByDomain", group_by_domain); | 138 source->AddBoolean("groupByDomain", group_by_domain); |
126 | 139 |
127 source->AddBoolean("isGuestSession", profile->IsGuestSession()); | 140 source->AddBoolean("isGuestSession", profile->IsGuestSession()); |
128 | 141 |
129 SigninManagerBase* signin_manager = | 142 source->AddBoolean(kIsUserSignedInKey, IsUserSignedIn(profile)); |
130 SigninManagerFactory::GetForProfile(profile); | |
131 bool is_authenticated = signin_manager != nullptr && | |
132 signin_manager->IsAuthenticated(); | |
133 source->AddBoolean("isUserSignedIn", is_authenticated); | |
134 | 143 |
135 source->AddResourcePath("constants.html", IDR_MD_HISTORY_CONSTANTS_HTML); | 144 source->AddResourcePath("constants.html", IDR_MD_HISTORY_CONSTANTS_HTML); |
136 source->AddResourcePath("constants.js", IDR_MD_HISTORY_CONSTANTS_JS); | 145 source->AddResourcePath("constants.js", IDR_MD_HISTORY_CONSTANTS_JS); |
137 source->AddResourcePath("images/100/sign_in_promo.png", | 146 source->AddResourcePath("images/100/sign_in_promo.png", |
138 IDR_MD_HISTORY_IMAGES_100_SIGN_IN_PROMO_PNG); | 147 IDR_MD_HISTORY_IMAGES_100_SIGN_IN_PROMO_PNG); |
139 source->AddResourcePath("images/200/sign_in_promo.png", | 148 source->AddResourcePath("images/200/sign_in_promo.png", |
140 IDR_MD_HISTORY_IMAGES_200_SIGN_IN_PROMO_PNG); | 149 IDR_MD_HISTORY_IMAGES_200_SIGN_IN_PROMO_PNG); |
141 source->AddResourcePath("history.js", IDR_MD_HISTORY_HISTORY_JS); | 150 source->AddResourcePath("history.js", IDR_MD_HISTORY_HISTORY_JS); |
142 | 151 |
143 #if BUILDFLAG(USE_VULCANIZE) | 152 #if BUILDFLAG(USE_VULCANIZE) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 source->SetJsonPath("strings.js"); | 216 source->SetJsonPath("strings.js"); |
208 | 217 |
209 return source; | 218 return source; |
210 } | 219 } |
211 | 220 |
212 } // namespace | 221 } // namespace |
213 | 222 |
214 bool MdHistoryUI::use_test_title_ = false; | 223 bool MdHistoryUI::use_test_title_ = false; |
215 | 224 |
216 MdHistoryUI::MdHistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 225 MdHistoryUI::MdHistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
226 Profile* profile = Profile::FromWebUI(web_ui); | |
227 content::WebUIDataSource* data_source = | |
228 CreateMdHistoryUIHTMLSource(profile, use_test_title_); | |
calamity
2016/11/16 05:36:13
Is there a reason we don't disable data source rep
Dan Beam
2016/11/16 06:02:03
so, right now these data sources live in the resou
| |
229 content::WebUIDataSource::Add(profile, data_source); | |
230 | |
217 web_ui->AddMessageHandler(new BrowsingHistoryHandler()); | 231 web_ui->AddMessageHandler(new BrowsingHistoryHandler()); |
218 web_ui->AddMessageHandler(new MetricsHandler()); | 232 web_ui->AddMessageHandler(new MetricsHandler()); |
219 | 233 |
220 if (search::IsInstantExtendedAPIEnabled()) { | 234 if (search::IsInstantExtendedAPIEnabled()) { |
221 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); | 235 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); |
222 web_ui->AddMessageHandler(new HistoryLoginHandler( | 236 web_ui->AddMessageHandler(new HistoryLoginHandler( |
223 base::Bind(&MdHistoryUI::CreateDataSource, base::Unretained(this)))); | 237 base::Bind(&MdHistoryUI::UpdateDataSource, base::Unretained(this)))); |
224 } | 238 } |
225 | 239 |
226 CreateDataSource(); | |
227 | |
228 web_ui->RegisterMessageCallback("menuPromoShown", | 240 web_ui->RegisterMessageCallback("menuPromoShown", |
229 base::Bind(&MdHistoryUI::HandleMenuPromoShown, base::Unretained(this))); | 241 base::Bind(&MdHistoryUI::HandleMenuPromoShown, base::Unretained(this))); |
230 } | 242 } |
231 | 243 |
232 MdHistoryUI::~MdHistoryUI() {} | 244 MdHistoryUI::~MdHistoryUI() {} |
233 | 245 |
234 // static | 246 // static |
235 bool MdHistoryUI::IsEnabled(Profile* profile) { | 247 bool MdHistoryUI::IsEnabled(Profile* profile) { |
236 return base::FeatureList::IsEnabled(features::kMaterialDesignHistory) && | 248 return base::FeatureList::IsEnabled(features::kMaterialDesignHistory) && |
237 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 249 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
(...skipping 18 matching lines...) Expand all Loading... | |
256 return ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 268 return ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
257 IDR_HISTORY_FAVICON, scale_factor); | 269 IDR_HISTORY_FAVICON, scale_factor); |
258 } | 270 } |
259 | 271 |
260 void MdHistoryUI::RegisterProfilePrefs( | 272 void MdHistoryUI::RegisterProfilePrefs( |
261 user_prefs::PrefRegistrySyncable* registry) { | 273 user_prefs::PrefRegistrySyncable* registry) { |
262 registry->RegisterBooleanPref(prefs::kMdHistoryMenuPromoShown, false, | 274 registry->RegisterBooleanPref(prefs::kMdHistoryMenuPromoShown, false, |
263 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 275 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
264 } | 276 } |
265 | 277 |
266 // TODO(lshang): Change to not re-create data source every time after we use | 278 void MdHistoryUI::UpdateDataSource() { |
267 // unique_ptr instead of raw pointers for data source. | 279 CHECK(web_ui()); |
268 void MdHistoryUI::CreateDataSource() { | 280 |
269 Profile* profile = Profile::FromWebUI(web_ui()); | 281 Profile* profile = Profile::FromWebUI(web_ui()); |
270 content::WebUIDataSource* data_source = | 282 |
271 CreateMdHistoryUIHTMLSource(profile, use_test_title_); | 283 std::unique_ptr<base::DictionaryValue> update(new base::DictionaryValue); |
272 content::WebUIDataSource::Add(profile, data_source); | 284 update->SetBoolean(kIsUserSignedInKey, IsUserSignedIn(profile)); |
285 update->SetBoolean(kShowMenuPromoKey, !MenuPromoShown(profile)); | |
286 | |
287 content::WebUIDataSource::Update(profile, chrome::kChromeUIHistoryHost, | |
288 std::move(update)); | |
273 } | 289 } |
274 | 290 |
275 void MdHistoryUI::HandleMenuPromoShown(const base::ListValue* args) { | 291 void MdHistoryUI::HandleMenuPromoShown(const base::ListValue* args) { |
276 Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean( | 292 Profile::FromWebUI(web_ui())->GetPrefs()->SetBoolean( |
277 prefs::kMdHistoryMenuPromoShown, true); | 293 prefs::kMdHistoryMenuPromoShown, true); |
278 CreateDataSource(); | 294 UpdateDataSource(); |
279 } | 295 } |
OLD | NEW |