OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/display/display_preferences.h" |
| 6 |
| 7 #include "ash/display/display_controller.h" |
| 8 #include "ash/shell.h" |
| 9 #include "base/string16.h" |
| 10 #include "base/string_util.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "googleurl/src/url_canon.h" |
| 16 #include "googleurl/src/url_util.h" |
| 17 #include "ui/aura/display_manager.h" |
| 18 #include "ui/aura/env.h" |
| 19 #include "ui/gfx/display.h" |
| 20 |
| 21 namespace chromeos { |
| 22 |
| 23 namespace { |
| 24 // Replaces dot "." by "%2E" since it's the path separater of base::Value. Also |
| 25 // replaces "%" by "%25" for unescaping. |
| 26 void EscapeDisplayName(const std::string& name, std::string* escaped) { |
| 27 DCHECK(escaped); |
| 28 std::string middle; |
| 29 ReplaceChars(name, "%", "%25", &middle); |
| 30 ReplaceChars(middle, ".", "%2E", escaped); |
| 31 } |
| 32 |
| 33 // Unescape %-encoded characters. |
| 34 std::string UnescapeDisplayName(const std::string& name) { |
| 35 url_canon::RawCanonOutputT<char16> decoded; |
| 36 url_util::DecodeURLEscapeSequences(name.data(), name.size(), &decoded); |
| 37 // Display names are ASCII-only. |
| 38 return UTF16ToASCII(string16(decoded.data(), decoded.length())); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 void RegisterDisplayPrefs(PrefService* pref_service) { |
| 44 // The default secondary display layout. |
| 45 pref_service->RegisterIntegerPref(prefs::kSecondaryDisplayLayout, |
| 46 static_cast<int>(ash::DisplayLayout::RIGHT), |
| 47 PrefService::UNSYNCABLE_PREF); |
| 48 // The default offset of the secondary display position from the primary |
| 49 // display. |
| 50 pref_service->RegisterIntegerPref(prefs::kSecondaryDisplayOffset, |
| 51 0, |
| 52 PrefService::UNSYNCABLE_PREF); |
| 53 // Per-display preference. |
| 54 pref_service->RegisterDictionaryPref(prefs::kSecondaryDisplays, |
| 55 PrefService::UNSYNCABLE_PREF); |
| 56 } |
| 57 |
| 58 void SetDisplayLayoutPref(PrefService* pref_service, |
| 59 const gfx::Display& display, |
| 60 int layout, |
| 61 int offset) { |
| 62 { |
| 63 DictionaryPrefUpdate update(pref_service, prefs::kSecondaryDisplays); |
| 64 ash::DisplayLayout display_layout( |
| 65 static_cast<ash::DisplayLayout::Position>(layout), offset); |
| 66 |
| 67 aura::DisplayManager* display_manager = |
| 68 aura::Env::GetInstance()->display_manager(); |
| 69 std::string name; |
| 70 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { |
| 71 if (display_manager->GetDisplayAt(i)->id() == display.id()) { |
| 72 EscapeDisplayName(display_manager->GetDisplayNameAt(i), &name); |
| 73 break; |
| 74 } |
| 75 } |
| 76 |
| 77 DCHECK(!name.empty()); |
| 78 |
| 79 base::DictionaryValue* pref_data = update.Get(); |
| 80 scoped_ptr<base::Value>layout_value(new base::DictionaryValue()); |
| 81 if (pref_data->HasKey(name)) { |
| 82 base::Value* value = NULL; |
| 83 if (pref_data->Get(name, &value) && value != NULL) |
| 84 layout_value.reset(value->DeepCopy()); |
| 85 } |
| 86 if (ash::DisplayLayout::ConvertToValue(display_layout, layout_value.get())) |
| 87 pref_data->Set(name, layout_value.release()); |
| 88 } |
| 89 |
| 90 pref_service->SetInteger(prefs::kSecondaryDisplayLayout, layout); |
| 91 pref_service->SetInteger(prefs::kSecondaryDisplayOffset, offset); |
| 92 |
| 93 NotifyDisplayPrefChanged(pref_service); |
| 94 } |
| 95 |
| 96 void NotifyDisplayPrefChanged(PrefService* pref_service) { |
| 97 ash::DisplayController* display_controller = |
| 98 ash::Shell::GetInstance()->display_controller(); |
| 99 |
| 100 ash::DisplayLayout default_layout( |
| 101 static_cast<ash::DisplayLayout::Position>(pref_service->GetInteger( |
| 102 prefs::kSecondaryDisplayLayout)), |
| 103 pref_service->GetInteger(prefs::kSecondaryDisplayOffset)); |
| 104 display_controller->SetDefaultDisplayLayout(default_layout); |
| 105 |
| 106 const base::DictionaryValue* layouts = pref_service->GetDictionary( |
| 107 prefs::kSecondaryDisplays); |
| 108 for (base::DictionaryValue::key_iterator it = layouts->begin_keys(); |
| 109 it != layouts->end_keys(); ++it) { |
| 110 const base::Value* value = NULL; |
| 111 if (!layouts->Get(*it, &value) || value == NULL) |
| 112 continue; |
| 113 |
| 114 ash::DisplayLayout layout; |
| 115 if (!ash::DisplayLayout::ConvertFromValue(*value, &layout)) |
| 116 continue; |
| 117 |
| 118 display_controller->SetLayoutForDisplayName( |
| 119 UnescapeDisplayName(*it), layout); |
| 120 } |
| 121 } |
| 122 |
| 123 } // namespace chromeos |
OLD | NEW |