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