OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/chromeos/user_cros_settings_provider.h" | 5 #include "chrome/browser/chromeos/user_cros_settings_provider.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/hash_tables.h" | 13 #include "base/hash_tables.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/chromeos/cros/cros_library.h" | 19 #include "chrome/browser/chromeos/cros/cros_library.h" |
20 #include "chrome/browser/chromeos/cros/login_library.h" | 20 #include "chrome/browser/chromeos/cros/login_library.h" |
21 #include "chrome/browser/chromeos/cros/network_library.h" | 21 #include "chrome/browser/chromeos/cros/network_library.h" |
22 #include "chrome/browser/chromeos/cros_settings.h" | 22 #include "chrome/browser/chromeos/cros_settings.h" |
23 #include "chrome/browser/chromeos/cros_settings_names.h" | 23 #include "chrome/browser/chromeos/cros_settings_names.h" |
24 #include "chrome/browser/chromeos/login/ownership_service.h" | 24 #include "chrome/browser/chromeos/login/ownership_service.h" |
25 #include "chrome/browser/chromeos/login/ownership_status_checker.h" | 25 #include "chrome/browser/chromeos/login/ownership_status_checker.h" |
26 #include "chrome/browser/chromeos/login/user_manager.h" | 26 #include "chrome/browser/chromeos/login/user_manager.h" |
27 #include "chrome/browser/policy/browser_policy_connector.h" | |
28 #include "chrome/browser/prefs/pref_service.h" | 27 #include "chrome/browser/prefs/pref_service.h" |
29 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 28 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
30 #include "chrome/browser/ui/options/options_util.h" | 29 #include "chrome/browser/ui/options/options_util.h" |
31 #include "chrome/common/chrome_notification_types.h" | 30 #include "chrome/common/chrome_notification_types.h" |
32 #include "chrome/installer/util/google_update_settings.h" | 31 #include "chrome/installer/util/google_update_settings.h" |
33 #include "content/browser/browser_thread.h" | 32 #include "content/browser/browser_thread.h" |
34 | 33 |
35 namespace chromeos { | 34 namespace chromeos { |
36 | 35 |
37 namespace { | 36 namespace { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 local_state->RegisterStringPref(pref_path.c_str(), | 166 local_state->RegisterStringPref(pref_path.c_str(), |
168 "", | 167 "", |
169 PrefService::UNSYNCABLE_PREF); | 168 PrefService::UNSYNCABLE_PREF); |
170 } else { | 169 } else { |
171 DCHECK(IsControlledListSetting(pref_path)); | 170 DCHECK(IsControlledListSetting(pref_path)); |
172 local_state->RegisterListPref(pref_path.c_str(), | 171 local_state->RegisterListPref(pref_path.c_str(), |
173 PrefService::UNSYNCABLE_PREF); | 172 PrefService::UNSYNCABLE_PREF); |
174 } | 173 } |
175 } | 174 } |
176 | 175 |
177 // Create a settings value with "managed" and "disabled" property. | |
178 // "managed" property is true if the setting is managed by administrator. | |
179 // "disabled" property is true if the UI for the setting should be disabled. | |
180 Value* CreateSettingsValue(Value *value, bool managed, bool disabled) { | |
181 DictionaryValue* dict = new DictionaryValue; | |
182 dict->Set("value", value); | |
183 dict->Set("managed", Value::CreateBooleanValue(managed)); | |
184 dict->Set("disabled", Value::CreateBooleanValue(disabled)); | |
185 return dict; | |
186 } | |
187 | |
188 enum UseValue { | 176 enum UseValue { |
189 USE_VALUE_SUPPLIED, | 177 USE_VALUE_SUPPLIED, |
190 USE_VALUE_DEFAULT | 178 USE_VALUE_DEFAULT |
191 }; | 179 }; |
192 | 180 |
193 void UpdateCacheBool(const std::string& name, | 181 void UpdateCacheBool(const std::string& name, |
194 bool value, | 182 bool value, |
195 UseValue use_value) { | 183 UseValue use_value) { |
196 PrefService* prefs = g_browser_process->local_state(); | 184 PrefService* prefs = g_browser_process->local_state(); |
197 if (use_value == USE_VALUE_DEFAULT) | 185 if (use_value == USE_VALUE_DEFAULT) |
198 prefs->ClearPref(name.c_str()); | 186 prefs->ClearPref(name.c_str()); |
199 else | 187 else |
200 prefs->SetBoolean(name.c_str(), value); | 188 prefs->SetBoolean(name.c_str(), value); |
201 prefs->ScheduleSavePersistentPrefs(); | 189 prefs->ScheduleSavePersistentPrefs(); |
202 } | 190 } |
203 | 191 |
204 void UpdateCacheString(const std::string& name, | 192 void UpdateCacheString(const std::string& name, |
205 const std::string& value, | 193 const std::string& value, |
206 UseValue use_value) { | 194 UseValue use_value) { |
207 PrefService* prefs = g_browser_process->local_state(); | 195 PrefService* prefs = g_browser_process->local_state(); |
208 if (use_value == USE_VALUE_DEFAULT) | 196 if (use_value == USE_VALUE_DEFAULT) |
209 prefs->ClearPref(name.c_str()); | 197 prefs->ClearPref(name.c_str()); |
210 else | 198 else |
211 prefs->SetString(name.c_str(), value); | 199 prefs->SetString(name.c_str(), value); |
212 prefs->ScheduleSavePersistentPrefs(); | 200 prefs->ScheduleSavePersistentPrefs(); |
213 } | 201 } |
214 | 202 |
| 203 // Helper function to parse the whitelist from the policy cache into the local |
| 204 // state. |
| 205 // TODO(pastarmovj): This function will disappear in step two of the refactoring |
| 206 // as per design doc. (Contact pastarmovj@chromium.org for a link to it.) |
215 bool GetUserWhitelist(ListValue* user_list) { | 207 bool GetUserWhitelist(ListValue* user_list) { |
216 PrefService* prefs = g_browser_process->local_state(); | 208 PrefService* prefs = g_browser_process->local_state(); |
217 DCHECK(!prefs->IsManagedPreference(kAccountsPrefUsers)); | 209 DCHECK(!prefs->IsManagedPreference(kAccountsPrefUsers)); |
218 | 210 |
219 std::vector<std::string> whitelist; | 211 std::vector<std::string> whitelist; |
220 if (!SignedSettings::EnumerateWhitelist(&whitelist)) { | 212 if (!SignedSettings::EnumerateWhitelist(&whitelist)) { |
221 LOG(WARNING) << "Failed to retrieve user whitelist."; | 213 LOG(WARNING) << "Failed to retrieve user whitelist."; |
222 return false; | 214 return false; |
223 } | 215 } |
224 | 216 |
225 ListPrefUpdate cached_whitelist_update(prefs, kAccountsPrefUsers); | 217 ListPrefUpdate cached_whitelist_update(prefs, kAccountsPrefUsers); |
226 cached_whitelist_update->Clear(); | 218 cached_whitelist_update->Clear(); |
227 | 219 |
228 const UserManager::User& self = UserManager::Get()->logged_in_user(); | 220 for (size_t i = 0; i < whitelist.size(); ++i) |
229 bool is_owner = UserManager::Get()->current_user_is_owner(); | 221 cached_whitelist_update->Append(Value::CreateStringValue(whitelist[i])); |
230 | |
231 for (size_t i = 0; i < whitelist.size(); ++i) { | |
232 const std::string& email = whitelist[i]; | |
233 | |
234 if (user_list) { | |
235 DictionaryValue* user = new DictionaryValue; | |
236 user->SetString("email", email); | |
237 user->SetString("name", ""); | |
238 user->SetBoolean("owner", is_owner && email == self.email()); | |
239 user_list->Append(user); | |
240 } | |
241 | |
242 cached_whitelist_update->Append(Value::CreateStringValue(email)); | |
243 } | |
244 | 222 |
245 prefs->ScheduleSavePersistentPrefs(); | 223 prefs->ScheduleSavePersistentPrefs(); |
246 return true; | 224 return true; |
247 } | 225 } |
248 | 226 |
249 class UserCrosSettingsTrust : public SignedSettingsHelper::Callback { | 227 class UserCrosSettingsTrust : public SignedSettingsHelper::Callback { |
250 public: | 228 public: |
251 static UserCrosSettingsTrust* GetInstance() { | 229 static UserCrosSettingsTrust* GetInstance() { |
252 return Singleton<UserCrosSettingsTrust>::get(); | 230 return Singleton<UserCrosSettingsTrust>::get(); |
253 } | 231 } |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 // static | 542 // static |
565 void UserCrosSettingsProvider::RegisterPrefs(PrefService* local_state) { | 543 void UserCrosSettingsProvider::RegisterPrefs(PrefService* local_state) { |
566 for (size_t i = 0; i < arraysize(kBooleanSettings); ++i) | 544 for (size_t i = 0; i < arraysize(kBooleanSettings); ++i) |
567 RegisterSetting(local_state, kBooleanSettings[i]); | 545 RegisterSetting(local_state, kBooleanSettings[i]); |
568 for (size_t i = 0; i < arraysize(kStringSettings); ++i) | 546 for (size_t i = 0; i < arraysize(kStringSettings); ++i) |
569 RegisterSetting(local_state, kStringSettings[i]); | 547 RegisterSetting(local_state, kStringSettings[i]); |
570 for (size_t i = 0; i < arraysize(kListSettings); ++i) | 548 for (size_t i = 0; i < arraysize(kListSettings); ++i) |
571 RegisterSetting(local_state, kListSettings[i]); | 549 RegisterSetting(local_state, kListSettings[i]); |
572 } | 550 } |
573 | 551 |
574 bool UserCrosSettingsProvider::RequestTrustedAllowGuest( | |
575 const base::Closure& callback) { | |
576 return UserCrosSettingsTrust::GetInstance()->RequestTrustedEntity( | |
577 kAccountsPrefAllowGuest, callback); | |
578 } | |
579 | |
580 bool UserCrosSettingsProvider::RequestTrustedAllowNewUser( | |
581 const base::Closure& callback) { | |
582 return UserCrosSettingsTrust::GetInstance()->RequestTrustedEntity( | |
583 kAccountsPrefAllowNewUser, callback); | |
584 } | |
585 | |
586 bool UserCrosSettingsProvider::RequestTrustedShowUsersOnSignin( | |
587 const base::Closure& callback) { | |
588 return UserCrosSettingsTrust::GetInstance()->RequestTrustedEntity( | |
589 kAccountsPrefShowUserNamesOnSignIn, callback); | |
590 } | |
591 | |
592 bool UserCrosSettingsProvider::RequestTrustedDataRoamingEnabled( | |
593 const base::Closure& callback) { | |
594 return UserCrosSettingsTrust::GetInstance()->RequestTrustedEntity( | |
595 kSignedDataRoamingEnabled, callback); | |
596 } | |
597 | |
598 bool UserCrosSettingsProvider::RequestTrustedOwner( | |
599 const base::Closure& callback) { | |
600 return UserCrosSettingsTrust::GetInstance()->RequestTrustedEntity( | |
601 kDeviceOwner, callback); | |
602 } | |
603 | |
604 bool UserCrosSettingsProvider::RequestTrustedReportingEnabled( | |
605 const base::Closure& callback) { | |
606 return UserCrosSettingsTrust::GetInstance()->RequestTrustedEntity( | |
607 kStatsReportingPref, callback); | |
608 } | |
609 | |
610 void UserCrosSettingsProvider::Reload() { | 552 void UserCrosSettingsProvider::Reload() { |
611 UserCrosSettingsTrust::GetInstance()->Reload(); | 553 UserCrosSettingsTrust::GetInstance()->Reload(); |
612 } | 554 } |
613 | 555 |
614 // static | |
615 bool UserCrosSettingsProvider::cached_allow_guest() { | |
616 // Trigger prefetching if singleton object still does not exist. | |
617 UserCrosSettingsTrust::GetInstance(); | |
618 return g_browser_process->local_state()->GetBoolean(kAccountsPrefAllowGuest); | |
619 } | |
620 | |
621 // static | |
622 bool UserCrosSettingsProvider::cached_allow_new_user() { | |
623 // Trigger prefetching if singleton object still does not exist. | |
624 UserCrosSettingsTrust::GetInstance(); | |
625 return g_browser_process->local_state()->GetBoolean( | |
626 kAccountsPrefAllowNewUser); | |
627 } | |
628 | |
629 // static | |
630 bool UserCrosSettingsProvider::cached_data_roaming_enabled() { | |
631 // Trigger prefetching if singleton object still does not exist. | |
632 UserCrosSettingsTrust::GetInstance(); | |
633 return g_browser_process->local_state()->GetBoolean( | |
634 kSignedDataRoamingEnabled); | |
635 } | |
636 | |
637 // static | |
638 bool UserCrosSettingsProvider::cached_show_users_on_signin() { | |
639 // Trigger prefetching if singleton object still does not exist. | |
640 UserCrosSettingsTrust::GetInstance(); | |
641 return g_browser_process->local_state()->GetBoolean( | |
642 kAccountsPrefShowUserNamesOnSignIn); | |
643 } | |
644 | |
645 // static | |
646 bool UserCrosSettingsProvider::cached_reporting_enabled() { | |
647 // Trigger prefetching if singleton object still does not exist. | |
648 UserCrosSettingsTrust::GetInstance(); | |
649 return g_browser_process->local_state()->GetBoolean( | |
650 kStatsReportingPref); | |
651 } | |
652 | |
653 // static | |
654 const ListValue* UserCrosSettingsProvider::cached_whitelist() { | |
655 PrefService* prefs = g_browser_process->local_state(); | |
656 const ListValue* cached_users = prefs->GetList(kAccountsPrefUsers); | |
657 if (!prefs->IsManagedPreference(kAccountsPrefUsers)) { | |
658 if (cached_users == NULL) { | |
659 // Update whitelist cache. | |
660 GetUserWhitelist(NULL); | |
661 cached_users = prefs->GetList(kAccountsPrefUsers); | |
662 } | |
663 } | |
664 if (cached_users == NULL) { | |
665 NOTREACHED(); | |
666 cached_users = new ListValue; | |
667 } | |
668 return cached_users; | |
669 } | |
670 | |
671 // static | |
672 std::string UserCrosSettingsProvider::cached_owner() { | |
673 // Trigger prefetching if singleton object still does not exist. | |
674 UserCrosSettingsTrust::GetInstance(); | |
675 if (!g_browser_process || !g_browser_process->local_state()) | |
676 return std::string(); | |
677 return g_browser_process->local_state()->GetString(kDeviceOwner); | |
678 } | |
679 | |
680 // static | |
681 bool UserCrosSettingsProvider::IsEmailInCachedWhitelist( | |
682 const std::string& email) { | |
683 const ListValue* whitelist = cached_whitelist(); | |
684 if (whitelist) { | |
685 StringValue email_value(email); | |
686 for (ListValue::const_iterator i(whitelist->begin()); | |
687 i != whitelist->end(); ++i) { | |
688 if ((*i)->Equals(&email_value)) | |
689 return true; | |
690 } | |
691 } | |
692 return false; | |
693 } | |
694 | |
695 void UserCrosSettingsProvider::DoSet(const std::string& path, | 556 void UserCrosSettingsProvider::DoSet(const std::string& path, |
696 Value* in_value) { | 557 Value* in_value) { |
697 UserCrosSettingsTrust::GetInstance()->Set(path, in_value); | 558 UserCrosSettingsTrust::GetInstance()->Set(path, in_value); |
698 } | 559 } |
699 | 560 |
700 bool UserCrosSettingsProvider::Get(const std::string& path, | 561 const base::Value* UserCrosSettingsProvider::Get( |
701 Value** out_value) const { | 562 const std::string& path) const { |
702 if (path == kAccountsPrefUsers) { | 563 if (HandlesSetting(path)) { |
703 ListValue* user_list = new ListValue; | 564 PrefService* prefs = g_browser_process->local_state(); |
704 GetUserWhitelist(user_list); | 565 // TODO(pastarmovj): Temporary hack until we refactor the user whitelisting |
705 *out_value = user_list; | 566 // handling to completely coincide with the rest of the settings. |
706 return true; | 567 if (path == kAccountsPrefUsers && |
| 568 prefs->GetList(kAccountsPrefUsers) == NULL) { |
| 569 GetUserWhitelist(NULL); |
| 570 } |
| 571 const PrefService::Preference* pref = prefs->FindPreference(path.c_str()); |
| 572 return pref->GetValue(); |
707 } | 573 } |
| 574 return NULL; |
| 575 } |
708 | 576 |
709 if (IsControlledBooleanSetting(path) || IsControlledStringSetting(path)) { | 577 bool UserCrosSettingsProvider::GetTrusted(const std::string& path, |
710 PrefService* prefs = g_browser_process->local_state(); | 578 const base::Closure& callback) const { |
711 const PrefService::Preference* pref = prefs->FindPreference(path.c_str()); | 579 return UserCrosSettingsTrust::GetInstance()->RequestTrustedEntity( |
712 const Value *pref_value = pref->GetValue(); | 580 path, callback); |
713 | |
714 *out_value = CreateSettingsValue( | |
715 pref_value->DeepCopy(), | |
716 g_browser_process->browser_policy_connector()->IsEnterpriseManaged(), | |
717 !UserManager::Get()->current_user_is_owner()); | |
718 return true; | |
719 } | |
720 | |
721 return false; | |
722 } | 581 } |
723 | 582 |
724 bool UserCrosSettingsProvider::HandlesSetting(const std::string& path) const { | 583 bool UserCrosSettingsProvider::HandlesSetting(const std::string& path) const { |
725 return ::StartsWithASCII(path, "cros.accounts.", true) || | 584 return ::StartsWithASCII(path, "cros.accounts.", true) || |
726 ::StartsWithASCII(path, "cros.signed.", true) || | 585 ::StartsWithASCII(path, "cros.signed.", true) || |
727 ::StartsWithASCII(path, "cros.metrics.", true) || | 586 ::StartsWithASCII(path, "cros.metrics.", true) || |
| 587 path == kDeviceOwner || |
728 path == kReleaseChannel; | 588 path == kReleaseChannel; |
729 } | 589 } |
730 | 590 |
| 591 // static |
731 void UserCrosSettingsProvider::WhitelistUser(const std::string& email) { | 592 void UserCrosSettingsProvider::WhitelistUser(const std::string& email) { |
732 SignedSettingsHelper::Get()->StartWhitelistOp( | 593 SignedSettingsHelper::Get()->StartWhitelistOp( |
733 email, true, UserCrosSettingsTrust::GetInstance()); | 594 email, true, UserCrosSettingsTrust::GetInstance()); |
734 PrefService* prefs = g_browser_process->local_state(); | 595 PrefService* prefs = g_browser_process->local_state(); |
735 ListPrefUpdate cached_whitelist_update(prefs, kAccountsPrefUsers); | 596 ListPrefUpdate cached_whitelist_update(prefs, kAccountsPrefUsers); |
736 cached_whitelist_update->Append(Value::CreateStringValue(email)); | 597 cached_whitelist_update->Append(Value::CreateStringValue(email)); |
737 prefs->ScheduleSavePersistentPrefs(); | 598 prefs->ScheduleSavePersistentPrefs(); |
738 } | 599 } |
739 | 600 |
| 601 // static |
740 void UserCrosSettingsProvider::UnwhitelistUser(const std::string& email) { | 602 void UserCrosSettingsProvider::UnwhitelistUser(const std::string& email) { |
741 SignedSettingsHelper::Get()->StartWhitelistOp( | 603 SignedSettingsHelper::Get()->StartWhitelistOp( |
742 email, false, UserCrosSettingsTrust::GetInstance()); | 604 email, false, UserCrosSettingsTrust::GetInstance()); |
743 | 605 |
744 PrefService* prefs = g_browser_process->local_state(); | 606 PrefService* prefs = g_browser_process->local_state(); |
745 ListPrefUpdate cached_whitelist_update(prefs, kAccountsPrefUsers); | 607 ListPrefUpdate cached_whitelist_update(prefs, kAccountsPrefUsers); |
746 StringValue email_value(email); | 608 StringValue email_value(email); |
747 if (cached_whitelist_update->Remove(email_value, NULL)) | 609 if (cached_whitelist_update->Remove(email_value, NULL)) |
748 prefs->ScheduleSavePersistentPrefs(); | 610 prefs->ScheduleSavePersistentPrefs(); |
749 } | 611 } |
750 | 612 |
751 // static | 613 // static |
752 void UserCrosSettingsProvider::UpdateCachedOwner(const std::string& email) { | 614 void UserCrosSettingsProvider::UpdateCachedOwner(const std::string& email) { |
753 UpdateCacheString(kDeviceOwner, email, USE_VALUE_SUPPLIED); | 615 UpdateCacheString(kDeviceOwner, email, USE_VALUE_SUPPLIED); |
754 } | 616 } |
755 | 617 |
756 } // namespace chromeos | 618 } // namespace chromeos |
OLD | NEW |