Chromium Code Reviews| 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. | |
|
oshima
2012/08/28 08:55:50
it also converts % -> %25, correct?
Jun Mukai
2012/08/28 09:51:00
Added
| |
| 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 using ash::internal::DisplayLayout; | |
| 44 | |
| 45 void RegisterDisplayPrefs(PrefService* pref_service) { | |
| 46 // The default secondary display layout. | |
| 47 pref_service->RegisterIntegerPref(prefs::kSecondaryDisplayLayout, | |
| 48 static_cast<int>(DisplayLayout::RIGHT), | |
| 49 PrefService::UNSYNCABLE_PREF); | |
| 50 // The default offset of the secondary display position from the primary | |
| 51 // display. | |
| 52 pref_service->RegisterIntegerPref(prefs::kSecondaryDisplayOffset, | |
| 53 0, | |
| 54 PrefService::UNSYNCABLE_PREF); | |
| 55 // Per-display preference. | |
| 56 pref_service->RegisterDictionaryPref(prefs::kSecondaryDisplays, | |
| 57 PrefService::UNSYNCABLE_PREF); | |
| 58 } | |
| 59 | |
| 60 void SetDisplayLayoutPref(PrefService* pref_service, | |
| 61 const gfx::Display& display, int layout, int offset) { | |
| 62 { | |
| 63 DictionaryPrefUpdate update(pref_service, prefs::kSecondaryDisplays); | |
| 64 DisplayLayout display_layout( | |
| 65 static_cast<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::DictionaryValue>layout_value( | |
| 81 new base::DictionaryValue()); | |
| 82 if (pref_data->HasKey(name)) { | |
| 83 base::Value* value = NULL; | |
| 84 base::DictionaryValue* dict_value = NULL; | |
| 85 if (pref_data->Get(name, &value) && value != NULL && | |
| 86 value->GetAsDictionary(&dict_value) && dict_value != NULL) { | |
| 87 layout_value.reset(dict_value->DeepCopy()); | |
| 88 } | |
| 89 } | |
| 90 if (display_layout.ConvertToValue(layout_value.get())) | |
| 91 pref_data->Set(name, layout_value.release()); | |
| 92 } | |
| 93 | |
| 94 pref_service->SetInteger(prefs::kSecondaryDisplayLayout, layout); | |
| 95 pref_service->SetInteger(prefs::kSecondaryDisplayOffset, offset); | |
| 96 | |
| 97 NotifyDisplayPrefChanged(pref_service); | |
| 98 } | |
| 99 | |
| 100 void NotifyDisplayPrefChanged(PrefService* pref_service) { | |
| 101 ash::internal::DisplayController* display_controller = | |
| 102 ash::Shell::GetInstance()->display_controller(); | |
| 103 | |
| 104 DisplayLayout default_layout( | |
| 105 static_cast<DisplayLayout::Position>(pref_service->GetInteger( | |
| 106 prefs::kSecondaryDisplayLayout)), | |
| 107 pref_service->GetInteger(prefs::kSecondaryDisplayOffset)); | |
| 108 display_controller->SetDefaultDisplayLayout(default_layout); | |
| 109 | |
| 110 const base::DictionaryValue* layouts = pref_service->GetDictionary( | |
| 111 prefs::kSecondaryDisplays); | |
| 112 for (base::DictionaryValue::key_iterator it = layouts->begin_keys(); | |
| 113 it != layouts->end_keys(); ++it) { | |
| 114 const base::Value* value = NULL; | |
| 115 if (!layouts->Get(*it, &value) || value == NULL) | |
| 116 continue; | |
| 117 | |
| 118 base::JSONValueConverter<DisplayLayout> converter; | |
| 119 DisplayLayout layout; | |
| 120 if (!converter.Convert(*value, &layout)) | |
| 121 continue; | |
| 122 | |
| 123 std::string unescaped = UnescapeDisplayName(*it); | |
| 124 display_controller->SetLayoutForDisplayName( | |
| 125 UnescapeDisplayName(*it), layout); | |
|
oshima
2012/08/28 08:55:50
unescaped
(or you can just use UnescapeDisplayName
Jun Mukai
2012/08/28 09:51:00
Remove unescaped.
| |
| 126 } | |
| 127 } | |
| 128 | |
| 129 } // namespace chromeos | |
| OLD | NEW |