OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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/wake_lock/wake_lock_preference.h" |
| 6 |
| 7 #include "base/prefs/scoped_user_pref_update.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/tab_contents/tab_util.h" |
| 10 #include "components/pref_registry/pref_registry_syncable.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 |
| 13 const char kWakeLockEnabled[] = "profile.wake_lock_enabled"; |
| 14 |
| 15 // static |
| 16 scoped_ptr<WakeLockPreference> WakeLockPreference::createWakeLockPreference( |
| 17 int render_process_id, int routed_id) { |
| 18 return scoped_ptr<WakeLockPreference>(new WakeLockPreference( |
| 19 render_process_id, routed_id)); |
| 20 } |
| 21 |
| 22 // static |
| 23 void WakeLockPreference::RegisterProfilePrefs( |
| 24 user_prefs::PrefRegistrySyncable* registry) { |
| 25 registry->RegisterDictionaryPref(kWakeLockEnabled, |
| 26 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 27 } |
| 28 |
| 29 WakeLockPreference::WakeLockPreference(int render_process_id, int routed_id) { |
| 30 content::WebContents* web_contents = |
| 31 tab_util::GetWebContentsByID(render_process_id, routed_id); |
| 32 DCHECK(web_contents); |
| 33 Profile* profile = Profile::FromBrowserContext( |
| 34 web_contents->GetBrowserContext()); |
| 35 DCHECK(profile); |
| 36 pref_service_ = profile->GetPrefs(); |
| 37 DCHECK(pref_service_); |
| 38 } |
| 39 |
| 40 bool WakeLockPreference::isSavedAllow(const std::string& key) { |
| 41 return (getAllowed(key) != WakeLockPreferenceNotFound); |
| 42 } |
| 43 |
| 44 bool WakeLockPreference::isAllowed(const std::string& key) { |
| 45 return (getAllowed(key) == WakeLockPreferenceYes); |
| 46 } |
| 47 |
| 48 void WakeLockPreference::setAllowed(const std::string& key, bool allowed) { |
| 49 DictionaryPrefUpdate update(pref_service_, kWakeLockEnabled); |
| 50 base::DictionaryValue* inspector_settings = update.Get(); |
| 51 inspector_settings->SetWithoutPathExpansion( |
| 52 key, base::Value::CreateBooleanValue(allowed)); |
| 53 } |
| 54 |
| 55 WakeLockPreference::WakeLockPreferenceStatus WakeLockPreference::getAllowed( |
| 56 const std::string& key) { |
| 57 const base::DictionaryValue* dict = |
| 58 pref_service_->GetDictionary(kWakeLockEnabled); |
| 59 bool result; |
| 60 if (dict && dict->GetBooleanWithoutPathExpansion(key, &result)) |
| 61 return result ? WakeLockPreferenceYes : WakeLockPreferenceNo; |
| 62 return WakeLockPreferenceNotFound; |
| 63 } |
OLD | NEW |