[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame^] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 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/host_zoom_map.h" |
| 6 | |
| 7 | #include "base/utf_string_conversions.h" |
| 8 | #include "chrome/browser/chrome_thread.h" |
| 9 | #include "chrome/browser/profile.h" |
| 10 | #include "chrome/common/pref_names.h" |
| 11 | #include "chrome/common/pref_service.h" |
| 12 | |
| 13 | HostZoomMap::HostZoomMap(Profile* profile) : profile_(profile) { |
| 14 | const DictionaryValue* host_zoom_dictionary = |
| 15 | profile_->GetPrefs()->GetDictionary(prefs::kPerHostZoomLevels); |
| 16 | // Careful: The returned value could be NULL if the pref has never been set. |
| 17 | if (host_zoom_dictionary != NULL) { |
| 18 | for (DictionaryValue::key_iterator i(host_zoom_dictionary->begin_keys()); |
| 19 | i != host_zoom_dictionary->end_keys(); ++i) { |
| 20 | std::wstring wide_host(*i); |
| 21 | int zoom_level = 0; |
| 22 | bool success = host_zoom_dictionary->GetIntegerWithoutPathExpansion( |
| 23 | wide_host, &zoom_level); |
| 24 | DCHECK(success); |
| 25 | host_zoom_levels_[WideToUTF8(wide_host)] = zoom_level; |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // static |
| 31 | void HostZoomMap::RegisterUserPrefs(PrefService* prefs) { |
| 32 | prefs->RegisterDictionaryPref(prefs::kPerHostZoomLevels); |
| 33 | } |
| 34 | |
| 35 | int HostZoomMap::GetZoomLevel(const std::string& host) const { |
| 36 | AutoLock auto_lock(lock_); |
| 37 | HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
| 38 | return (i == host_zoom_levels_.end()) ? 0 : i->second; |
| 39 | } |
| 40 | |
| 41 | void HostZoomMap::SetZoomLevel(const std::string& host, int level) { |
| 42 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 43 | if (host.empty()) |
| 44 | return; |
| 45 | |
| 46 | { |
| 47 | AutoLock auto_lock(lock_); |
| 48 | if (level == 0) |
| 49 | host_zoom_levels_.erase(host); |
| 50 | else |
| 51 | host_zoom_levels_[host] = level; |
| 52 | } |
| 53 | |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame^] | 54 | DictionaryValue* host_zoom_dictionary = |
| 55 | profile_->GetPrefs()->GetMutableDictionary(prefs::kPerHostZoomLevels); |
| 56 | std::wstring wide_host(UTF8ToWide(host)); |
| 57 | if (level == 0) { |
| 58 | host_zoom_dictionary->RemoveWithoutPathExpansion(wide_host, NULL); |
| 59 | } else { |
| 60 | host_zoom_dictionary->SetWithoutPathExpansion(wide_host, |
| 61 | Value::CreateIntegerValue(level)); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame^] | 65 | void HostZoomMap::ResetToDefaults() { |
| 66 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 67 | |
| 68 | host_zoom_levels_.clear(); |
| 69 | profile_->GetPrefs()->ClearPref(prefs::kPerHostZoomLevels); |
| 70 | } |
| 71 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 72 | HostZoomMap::~HostZoomMap() { |
| 73 | } |