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/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. | |
25 void EscapeDisplayName(const std::string& name, std::string* escaped) { | |
26 DCHECK(escaped); | |
27 std::string middle; | |
28 ReplaceChars(name, "%", "%25", &middle); | |
29 ReplaceChars(middle, ".", "%2E", escaped); | |
30 } | |
31 | |
32 // Unescape %-encoded characters. | |
33 std::string UnescapeDisplayName(const std::string& name) { | |
34 url_canon::RawCanonOutputT<char16> decoded; | |
35 url_util::DecodeURLEscapeSequences(name.data(), name.size(), &decoded); | |
36 // Display names are ASCII-only. | |
37 return UTF16ToASCII(string16(decoded.data(), decoded.length())); | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 using ash::internal::DisplayLayout; | |
43 | |
44 void RegisterDisplayPrefs(PrefService* pref_service) { | |
45 // The default secondary display layout. | |
46 pref_service->RegisterIntegerPref(prefs::kSecondaryDisplayLayout, | |
47 static_cast<int>(DisplayLayout::RIGHT), | |
48 PrefService::UNSYNCABLE_PREF); | |
49 // The default offset of the secondary display position from the primary | |
50 // display. | |
51 pref_service->RegisterIntegerPref(prefs::kSecondaryDisplayOffset, | |
52 0, | |
53 PrefService::UNSYNCABLE_PREF); | |
54 // Per-display preference. | |
55 pref_service->RegisterDictionaryPref(prefs::kSecondaryDisplays, | |
56 PrefService::UNSYNCABLE_PREF); | |
57 } | |
58 | |
59 void SetDisplayLayoutPref(PrefService* pref_service, | |
60 const gfx::Display& display, int layout, int offset) { | |
61 { | |
62 DictionaryPrefUpdate update(pref_service, prefs::kSecondaryDisplays); | |
63 DisplayLayout display_layout( | |
64 static_cast<DisplayLayout::Position>(layout), offset); | |
65 | |
66 aura::DisplayManager* display_manager = | |
67 aura::Env::GetInstance()->display_manager(); | |
68 std::string name; | |
69 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { | |
70 if (display_manager->GetDisplayAt(i)->id() == display.id()) { | |
71 EscapeDisplayName(display_manager->GetDisplayNameAt(i), &name); | |
72 break; | |
73 } | |
74 } | |
75 | |
76 DCHECK(!name.empty()); | |
77 | |
78 base::DictionaryValue* pref_data = update.Get(); | |
79 scoped_ptr<base::DictionaryValue>layout_value( | |
80 new base::DictionaryValue()); | |
81 if (pref_data->HasKey(name)) { | |
82 base::Value* value = NULL; | |
83 base::DictionaryValue* dict_value = NULL; | |
84 if (pref_data->Get(name, &value) && value != NULL && | |
85 value->GetAsDictionary(&dict_value) && dict_value != NULL) { | |
86 layout_value.reset(dict_value->DeepCopy()); | |
87 } | |
88 } | |
89 if (display_layout.GetValue(layout_value.get())) { | |
90 pref_data->Set(name, layout_value.release()); | |
91 } | |
oshima
2012/08/27 23:00:46
nuke {}
Jun Mukai
2012/08/28 08:12:42
Done.
| |
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::Position default_position = | |
105 static_cast<DisplayLayout::Position>(pref_service->GetInteger( | |
106 prefs::kSecondaryDisplayLayout)); | |
107 display_controller->SetSecondaryDisplayLayout(default_position); | |
108 int offset = pref_service->GetInteger(prefs::kSecondaryDisplayOffset); | |
109 display_controller->SetSecondaryDisplayOffset(offset); | |
oshima
2012/08/27 23:00:46
sanity check these values.
Jun Mukai
2012/08/28 08:12:42
Thanks. I changed the code to accept "SetDefaultD
| |
110 | |
111 const base::DictionaryValue* layouts = pref_service->GetDictionary( | |
112 prefs::kSecondaryDisplays); | |
113 for (base::DictionaryValue::key_iterator it = layouts->begin_keys(); | |
114 it != layouts->end_keys(); ++it) { | |
115 const base::Value* value = NULL; | |
oshima
2012/08/27 23:00:46
how this can be const?
Jun Mukai
2012/08/28 08:12:42
DictionaryValue::Get needs this const-ness. I gue
| |
116 if (!layouts->Get(*it, &value) || value == NULL) | |
117 continue; | |
118 | |
119 display_controller->SetDisplayLayoutForName( | |
120 UnescapeDisplayName(*it), *value); | |
121 } | |
122 } | |
123 | |
124 } // namespace chromeos | |
OLD | NEW |