1 // Copyright 2020 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/settings/chromeos/date_time_section.h"
6 
7 #include "base/no_destructor.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/system/timezone_util.h"
12 #include "chrome/browser/ui/webui/settings/chromeos/date_time_handler.h"
13 #include "chrome/browser/ui/webui/settings/chromeos/search/search_tag_registry.h"
14 #include "chrome/browser/ui/webui/webui_util.h"
15 #include "chrome/common/url_constants.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "chromeos/settings/cros_settings_names.h"
18 #include "chromeos/settings/system_settings_provider.h"
19 #include "chromeos/settings/timezone_settings.h"
20 #include "components/user_manager/user_manager.h"
21 #include "content/public/browser/web_ui_data_source.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/webui/web_ui_util.h"
24 
25 namespace chromeos {
26 namespace settings {
27 namespace {
28 
GetDateTimeSearchConcepts()29 const std::vector<SearchConcept>& GetDateTimeSearchConcepts() {
30   static const base::NoDestructor<std::vector<SearchConcept>> tags({
31       {IDS_OS_SETTINGS_TAG_DATE_TIME,
32        mojom::kDateAndTimeSectionPath,
33        mojom::SearchResultIcon::kClock,
34        mojom::SearchResultDefaultRank::kMedium,
35        mojom::SearchResultType::kSection,
36        {.section = mojom::Section::kDateAndTime}},
37       {IDS_OS_SETTINGS_TAG_DATE_TIME_MILITARY_CLOCK,
38        mojom::kDateAndTimeSectionPath,
39        mojom::SearchResultIcon::kClock,
40        mojom::SearchResultDefaultRank::kMedium,
41        mojom::SearchResultType::kSetting,
42        {.setting = mojom::Setting::k24HourClock},
43        {IDS_OS_SETTINGS_TAG_DATE_TIME_MILITARY_CLOCK_ALT1,
44         SearchConcept::kAltTagEnd}},
45   });
46   return *tags;
47 }
48 
GetFineGrainedTimeZoneSearchConcepts()49 const std::vector<SearchConcept>& GetFineGrainedTimeZoneSearchConcepts() {
50   static const base::NoDestructor<std::vector<SearchConcept>> tags({
51       {IDS_OS_SETTINGS_TAG_DATE_TIME_ZONE,
52        mojom::kTimeZoneSubpagePath,
53        mojom::SearchResultIcon::kClock,
54        mojom::SearchResultDefaultRank::kMedium,
55        mojom::SearchResultType::kSetting,
56        {.setting = mojom::Setting::kChangeTimeZone}},
57   });
58   return *tags;
59 }
60 
GetNoFineGrainedTimeZoneSearchConcepts()61 const std::vector<SearchConcept>& GetNoFineGrainedTimeZoneSearchConcepts() {
62   static const base::NoDestructor<std::vector<SearchConcept>> tags({
63       {IDS_OS_SETTINGS_TAG_DATE_TIME_ZONE,
64        mojom::kDateAndTimeSectionPath,
65        mojom::SearchResultIcon::kClock,
66        mojom::SearchResultDefaultRank::kMedium,
67        mojom::SearchResultType::kSetting,
68        {.setting = mojom::Setting::kChangeTimeZone}},
69   });
70   return *tags;
71 }
72 
IsFineGrainedTimeZoneEnabled()73 bool IsFineGrainedTimeZoneEnabled() {
74   SystemSettingsProvider provider;
75   return provider.Get(chromeos::kFineGrainedTimeZoneResolveEnabled)->GetBool();
76 }
77 
78 }  // namespace
79 
DateTimeSection(Profile * profile,SearchTagRegistry * search_tag_registry)80 DateTimeSection::DateTimeSection(Profile* profile,
81                                  SearchTagRegistry* search_tag_registry)
82     : OsSettingsSection(profile, search_tag_registry) {
83   SearchTagRegistry::ScopedTagUpdater updater = registry()->StartUpdate();
84   updater.AddSearchTags(GetDateTimeSearchConcepts());
85 
86   if (IsFineGrainedTimeZoneEnabled())
87     updater.AddSearchTags(GetFineGrainedTimeZoneSearchConcepts());
88   else
89     updater.AddSearchTags(GetNoFineGrainedTimeZoneSearchConcepts());
90 }
91 
92 DateTimeSection::~DateTimeSection() = default;
93 
AddLoadTimeData(content::WebUIDataSource * html_source)94 void DateTimeSection::AddLoadTimeData(content::WebUIDataSource* html_source) {
95   static constexpr webui::LocalizedString kLocalizedStrings[] = {
96       {"dateTimePageTitle", IDS_SETTINGS_DATE_TIME},
97       {"timeZone", IDS_SETTINGS_TIME_ZONE},
98       {"selectTimeZoneResolveMethod",
99        IDS_SETTINGS_SELECT_TIME_ZONE_RESOLVE_METHOD},
100       {"timeZoneGeolocation", IDS_SETTINGS_TIME_ZONE_GEOLOCATION},
101       {"timeZoneButton", IDS_SETTINGS_TIME_ZONE_BUTTON},
102       {"timeZoneSubpageTitle", IDS_SETTINGS_TIME_ZONE_SUBPAGE_TITLE},
103       {"setTimeZoneAutomaticallyDisabled",
104        IDS_SETTINGS_TIME_ZONE_DETECTION_MODE_DISABLED},
105       {"setTimeZoneAutomaticallyOn",
106        IDS_SETTINGS_TIME_ZONE_DETECTION_SET_AUTOMATICALLY},
107       {"setTimeZoneAutomaticallyOff",
108        IDS_SETTINGS_TIME_ZONE_DETECTION_CHOOSE_FROM_LIST},
109       {"setTimeZoneAutomaticallyIpOnlyDefault",
110        IDS_SETTINGS_TIME_ZONE_DETECTION_MODE_IP_ONLY_DEFAULT},
111       {"setTimeZoneAutomaticallyWithWiFiAccessPointsData",
112        IDS_SETTINGS_TIME_ZONE_DETECTION_MODE_SEND_WIFI_AP},
113       {"setTimeZoneAutomaticallyWithAllLocationInfo",
114        IDS_SETTINGS_TIME_ZONE_DETECTION_MODE_SEND_ALL_INFO},
115       {"use24HourClock", IDS_SETTINGS_USE_24_HOUR_CLOCK},
116       {"setDateTime", IDS_SETTINGS_SET_DATE_TIME},
117   };
118   AddLocalizedStringsBulk(html_source, kLocalizedStrings);
119 
120   html_source->AddString(
121       "timeZoneSettingsLearnMoreURL",
122       base::ASCIIToUTF16(base::StringPrintf(
123           chrome::kTimeZoneSettingsLearnMoreURL,
124           g_browser_process->GetApplicationLocale().c_str())));
125 
126   // Set the initial time zone to show.
127   html_source->AddString("timeZoneName", system::GetCurrentTimezoneName());
128   html_source->AddString(
129       "timeZoneID",
130       system::TimezoneSettings::GetInstance()->GetCurrentTimezoneID());
131 
132   bool is_child = user_manager::UserManager::Get()->GetActiveUser()->IsChild();
133   html_source->AddBoolean("isChild", is_child);
134 }
135 
AddHandlers(content::WebUI * web_ui)136 void DateTimeSection::AddHandlers(content::WebUI* web_ui) {
137   web_ui->AddMessageHandler(std::make_unique<DateTimeHandler>());
138 }
139 
GetSectionNameMessageId() const140 int DateTimeSection::GetSectionNameMessageId() const {
141   return IDS_SETTINGS_DATE_TIME;
142 }
143 
GetSection() const144 mojom::Section DateTimeSection::GetSection() const {
145   return mojom::Section::kDateAndTime;
146 }
147 
GetSectionIcon() const148 mojom::SearchResultIcon DateTimeSection::GetSectionIcon() const {
149   return mojom::SearchResultIcon::kClock;
150 }
151 
GetSectionPath() const152 std::string DateTimeSection::GetSectionPath() const {
153   return mojom::kDateAndTimeSectionPath;
154 }
155 
LogMetric(mojom::Setting setting,base::Value & value) const156 bool DateTimeSection::LogMetric(mojom::Setting setting,
157                                 base::Value& value) const {
158   // Unimplemented.
159   return false;
160 }
161 
RegisterHierarchy(HierarchyGenerator * generator) const162 void DateTimeSection::RegisterHierarchy(HierarchyGenerator* generator) const {
163   generator->RegisterTopLevelSetting(mojom::Setting::k24HourClock);
164 
165   generator->RegisterTopLevelSubpage(
166       IDS_SETTINGS_TIME_ZONE_SUBPAGE_TITLE, mojom::Subpage::kTimeZone,
167       mojom::SearchResultIcon::kClock, mojom::SearchResultDefaultRank::kMedium,
168       mojom::kTimeZoneSubpagePath);
169 
170   // When fine-grained time zone is enabled, users change the time zone on the
171   // time zone subpage; otherwise, the setting is directly embedded in the
172   // section.
173   if (IsFineGrainedTimeZoneEnabled()) {
174     generator->RegisterNestedSetting(mojom::Setting::kChangeTimeZone,
175                                      mojom::Subpage::kTimeZone);
176   } else {
177     generator->RegisterTopLevelSetting(mojom::Setting::kChangeTimeZone);
178   }
179 }
180 
181 }  // namespace settings
182 }  // namespace chromeos
183