Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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/chromeos/policy/recommendation_restorer.h" | |
| 6 | |
| 7 #include "ash/shell.h" | |
| 8 #include "ash/wm/user_activity_detector.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/location.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/prefs/pref_service.h" | |
| 15 #include "base/stl_util.h" | |
| 16 #include "base/time.h" | |
| 17 #include "base/values.h" | |
| 18 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/common/chrome_notification_types.h" | |
| 21 #include "chrome/common/pref_names.h" | |
| 22 #include "chromeos/chromeos_switches.h" | |
| 23 #include "content/public/browser/notification_details.h" | |
| 24 #include "content/public/browser/notification_service.h" | |
| 25 #include "content/public/browser/notification_source.h" | |
| 26 | |
| 27 namespace policy { | |
| 28 | |
| 29 namespace { | |
| 30 // The amount of idle time after which recommended values are restored. | |
| 31 const int kRestoreDelayInMs = 60 * 1000; // 1 minute. | |
| 32 } // namespace | |
| 33 | |
| 34 RecommendationRestorerInternal::RecommendationRestorerInternal( | |
| 35 PrefService* prefs) { | |
| 36 registrar_.Init(prefs); | |
| 37 registrar_.Add(prefs::kLargeCursorEnabled, | |
| 38 base::Bind(&RecommendationRestorerInternal::CheckPref, | |
| 39 base::Unretained(this))); | |
| 40 registrar_.Add(prefs::kSpokenFeedbackEnabled, | |
| 41 base::Bind(&RecommendationRestorerInternal::CheckPref, | |
| 42 base::Unretained(this))); | |
| 43 registrar_.Add(prefs::kHighContrastEnabled, | |
| 44 base::Bind(&RecommendationRestorerInternal::CheckPref, | |
| 45 base::Unretained(this))); | |
| 46 registrar_.Add(prefs::kScreenMagnifierEnabled, | |
| 47 base::Bind(&RecommendationRestorerInternal::CheckPref, | |
| 48 base::Unretained(this))); | |
| 49 registrar_.Add(prefs::kScreenMagnifierType, | |
| 50 base::Bind(&RecommendationRestorerInternal::CheckPref, | |
| 51 base::Unretained(this))); | |
| 52 | |
| 53 RestoreAll(); | |
| 54 } | |
| 55 | |
| 56 RecommendationRestorerInternal::~RecommendationRestorerInternal() { | |
| 57 if (ash::Shell::HasInstance()) | |
| 58 ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this); | |
| 59 | |
| 60 STLDeleteValues(&recommended_values_); | |
| 61 } | |
| 62 | |
| 63 void RecommendationRestorerInternal::OnUserActivity() { | |
| 64 if (restore_timer_.IsRunning()) | |
| 65 restore_timer_.Reset(); | |
| 66 } | |
| 67 | |
| 68 void RecommendationRestorerInternal::CheckPref( | |
| 69 const std::string& pref_name) { | |
| 70 const PrefService::Preference* pref = | |
| 71 registrar_.prefs()->FindPreference(pref_name.c_str()); | |
| 72 if (!pref) { | |
| 73 NOTREACHED(); | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 const base::Value* recommended_value = pref->GetRecommendedValue(); | |
| 78 if (!recommended_value) { | |
| 79 std::map<std::string, base::Value*>::iterator recommended_value_entry = | |
| 80 recommended_values_.find(pref->name()); | |
| 81 if (recommended_value_entry != recommended_values_.end()) { | |
| 82 delete recommended_value_entry->second; | |
| 83 recommended_values_.erase(recommended_value_entry); | |
| 84 } | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 base::Value*& recommended_value_entry = recommended_values_[pref->name()]; | |
| 89 if (!base::Value::Equals(recommended_value, recommended_value_entry)) { | |
|
Mattias Nissler (ping if slow)
2013/06/12 13:55:16
So this recommended_values_ thing is just here to
bartfab (slow)
2013/06/12 18:57:49
No, the |recommended_values_| are here so that I c
Mattias Nissler (ping if slow)
2013/06/13 18:11:46
I think a better implementation for this would be
| |
| 90 delete recommended_value_entry; | |
| 91 recommended_value_entry = recommended_value->DeepCopy(); | |
| 92 PrefService* prefs = registrar_.prefs(); | |
| 93 prefs->ClearPref(pref->name().c_str()); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 if (!pref->HasUserSetting()) | |
| 98 return; | |
| 99 | |
| 100 // Listen for user activity so that the timer can be reset while the user is | |
| 101 // active, causing it to fire only when the user remains idle for | |
| 102 // |kRestoreDelayInMs|. | |
| 103 if (ash::Shell::HasInstance()) { | |
| 104 ash::UserActivityDetector* user_activity_detector = | |
| 105 ash::Shell::GetInstance()->user_activity_detector(); | |
| 106 if (!user_activity_detector->HasObserver(this)) | |
| 107 user_activity_detector->AddObserver(this); | |
| 108 } | |
| 109 | |
| 110 // A single timer is sufficient as every pref changed initiated by the user | |
| 111 // implies user activity, meaning that even if there was a separate timer per | |
| 112 // pref, they would all be reset at this point, causing them to fire at | |
| 113 // exactly the same time. | |
| 114 restore_timer_.Start( | |
| 115 FROM_HERE, | |
| 116 base::TimeDelta::FromMilliseconds(kRestoreDelayInMs), | |
| 117 base::Bind(&RecommendationRestorerInternal::RestoreAll, | |
| 118 base::Unretained(this))); | |
| 119 } | |
| 120 | |
| 121 void RecommendationRestorerInternal::RestoreAll() { | |
| 122 STLDeleteValues(&recommended_values_); | |
| 123 | |
| 124 CheckPref(prefs::kLargeCursorEnabled); | |
| 125 CheckPref(prefs::kSpokenFeedbackEnabled); | |
| 126 CheckPref(prefs::kHighContrastEnabled); | |
| 127 CheckPref(prefs::kScreenMagnifierEnabled); | |
| 128 CheckPref(prefs::kScreenMagnifierType); | |
| 129 } | |
| 130 | |
| 131 RecommendationRestorer::RecommendationRestorer(Profile* profile) { | |
| 132 if (!chromeos::ProfileHelper::IsSigninProfile(profile)) | |
| 133 return; | |
| 134 | |
| 135 // Recommended values are restored in the login profile while the login screen | |
| 136 // is being shown. If this is a restart after a browser crash inside a user | |
| 137 // session, do nothing. | |
| 138 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 139 chromeos::switches::kLoginManager)) { | |
|
Mattias Nissler (ping if slow)
2013/06/12 13:55:16
So IsIsgninProfile returns true in the restart cas
bartfab (slow)
2013/06/12 18:57:49
IsSigninProfile returns true when the BCKS is crea
Mattias Nissler (ping if slow)
2013/06/13 18:11:46
So you're saying the accessibility code picks up p
| |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 // Create an object that will handle the actual restoring of recommended | |
| 144 // values. | |
| 145 recommendation_restorer_internal_.reset(new RecommendationRestorerInternal( | |
| 146 profile->GetPrefs())); | |
| 147 | |
| 148 // Subscribe to login notifications so that the restoring can be stopped when | |
| 149 // a user session begins. | |
| 150 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_CHANGED, | |
| 151 content::NotificationService::AllSources()); | |
| 152 } | |
| 153 | |
| 154 RecommendationRestorer::~RecommendationRestorer() { | |
| 155 } | |
| 156 | |
| 157 void RecommendationRestorer::Shutdown() { | |
| 158 recommendation_restorer_internal_.reset(); | |
| 159 } | |
| 160 | |
| 161 void RecommendationRestorer::Observe( | |
| 162 int type, | |
| 163 const content::NotificationSource& source, | |
| 164 const content::NotificationDetails& details) { | |
| 165 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { | |
| 166 // Stop restoring recommended values in the login profile when a user | |
| 167 // session begins. | |
| 168 recommendation_restorer_internal_.reset(); | |
| 169 registrar_.RemoveAll(); | |
| 170 } else { | |
| 171 NOTREACHED(); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 } // namespace policy | |
| OLD | NEW |