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