Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: chrome/browser/ui/ash/chrome_launcher_prefs.cc

Issue 1881263002: Generalize support for per-display shelf prefs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h" 5 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/app_mode/app_mode_utils.h"
15 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/extension_constants.h" 16 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
15 #include "components/pref_registry/pref_registry_syncable.h" 18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "components/prefs/pref_service.h"
20 #include "components/prefs/scoped_user_pref_update.h"
21 #include "ui/gfx/screen.h"
22
23 namespace ash {
16 24
17 namespace { 25 namespace {
18 26
19 // App ID of default pinned apps. 27 // App ID of default pinned apps.
20 const char* kDefaultPinnedApps[] = { 28 const char* kDefaultPinnedApps[] = {
21 extension_misc::kGmailAppId, 29 extension_misc::kGmailAppId,
22 extension_misc::kGoogleDocAppId, 30 extension_misc::kGoogleDocAppId,
23 extension_misc::kYoutubeAppId, 31 extension_misc::kYoutubeAppId,
24 }; 32 };
25 33
26 base::ListValue* CreateDefaultPinnedAppsList() { 34 base::ListValue* CreateDefaultPinnedAppsList() {
27 std::unique_ptr<base::ListValue> apps(new base::ListValue); 35 std::unique_ptr<base::ListValue> apps(new base::ListValue);
28 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) 36 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i)
29 apps->Append(ash::CreateAppDict(kDefaultPinnedApps[i])); 37 apps->Append(CreateAppDict(kDefaultPinnedApps[i]));
30 38
31 return apps.release(); 39 return apps.release();
32 } 40 }
33 41
42 // Returns |profile|'s pref value for the display with the given |display_id|.
43 // The pref value is stored in |local_path| and |path|, but the pref service may
44 // have per-display preferences and the value can be specified by policy.
45 // Here is the priority:
46 // * A value managed by policy. This is a single value that applies to all
47 // displays.
48 // * A user-set value for the specified display.
49 // * A user-set value in |local_path| or |path|, if no per-display settings are
50 // ever specified (see http://crbug.com/173719 for why). |local_path| is
51 // preferred. See comment in |kShelfAlignment| as to why we consider two
52 // prefs and why |local_path| is preferred.
53 // * A value recommended by policy. This is a single value that applies to all
54 // root windows.
55 // * The default value for |local_path| if the value is not recommended by
56 // policy.
57 std::string GetPerDisplayPref(Profile* profile,
58 int64_t display_id,
59 const char* local_path,
60 const char* path) {
61 PrefService* pref_service = profile->GetPrefs();
62 const PrefService::Preference* local_pref =
63 pref_service->FindPreference(local_path);
64 const std::string value(pref_service->GetString(local_path));
65 if (local_pref->IsManaged())
66 return value;
67
68 std::string pref_key = base::Int64ToString(display_id);
69 bool has_per_display_prefs = false;
70 if (!pref_key.empty()) {
71 const base::DictionaryValue* shelf_prefs =
72 pref_service->GetDictionary(prefs::kShelfPreferences);
73 const base::DictionaryValue* display_pref = nullptr;
74 std::string per_display_value;
75 if (shelf_prefs->GetDictionary(pref_key, &display_pref) &&
76 display_pref->GetString(path, &per_display_value))
77 return per_display_value;
78
79 // If the pref for the specified display is not found, scan the whole prefs
80 // and check if the prefs for other display is already specified.
81 std::string unused_value;
82 for (base::DictionaryValue::Iterator iter(*shelf_prefs); !iter.IsAtEnd();
83 iter.Advance()) {
84 const base::DictionaryValue* display_pref = nullptr;
85 if (iter.value().GetAsDictionary(&display_pref) &&
86 display_pref->GetString(path, &unused_value)) {
87 has_per_display_prefs = true;
88 break;
89 }
90 }
91 }
92
93 if (local_pref->IsRecommended() || !has_per_display_prefs)
94 return value;
95
96 const base::Value* default_value =
97 pref_service->GetDefaultPrefValue(local_path);
98 std::string default_string;
99 default_value->GetAsString(&default_string);
100 return default_string;
101 }
102
103 // Sets |profile|'s pref value for the display with the given |display_id|.
104 void SetPerDisplayPref(Profile* profile,
105 int64_t display_id,
106 const char* pref_key,
107 const std::string& value) {
108 if (display_id < 0)
109 return;
110
111 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kShelfPreferences);
112 base::DictionaryValue* shelf_prefs = update.Get();
113 base::DictionaryValue* prefs = nullptr;
114 std::string key = base::Int64ToString(display_id);
115 if (!shelf_prefs->GetDictionary(key, &prefs)) {
116 prefs = new base::DictionaryValue();
117 shelf_prefs->Set(key, prefs);
118 }
119 prefs->SetStringWithoutPathExpansion(pref_key, value);
120 }
121
122 ShelfAlignment AlignmentFromPref(const std::string& value) {
123 if (value == kShelfAlignmentLeft)
124 return SHELF_ALIGNMENT_LEFT;
125 else if (value == kShelfAlignmentRight)
126 return SHELF_ALIGNMENT_RIGHT;
127 // Default to bottom.
128 return SHELF_ALIGNMENT_BOTTOM;
129 }
130
131 const char* AlignmentToPref(ShelfAlignment alignment) {
132 switch (alignment) {
133 case SHELF_ALIGNMENT_BOTTOM:
134 return kShelfAlignmentBottom;
135 case SHELF_ALIGNMENT_LEFT:
136 return kShelfAlignmentLeft;
137 case SHELF_ALIGNMENT_RIGHT:
138 return kShelfAlignmentRight;
139 }
140 NOTREACHED();
141 return nullptr;
142 }
143
144 ShelfAutoHideBehavior AutoHideBehaviorFromPref(const std::string& value) {
145 // Note: To maintain sync compatibility with old images of chrome/chromeos
146 // the set of values that may be encountered includes the now-extinct
147 // "Default" as well as "Never" and "Always", "Default" should now
148 // be treated as "Never" (http://crbug.com/146773).
149 if (value == kShelfAutoHideBehaviorAlways)
150 return SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS;
151 return SHELF_AUTO_HIDE_BEHAVIOR_NEVER;
152 }
153
154 const char* AutoHideBehaviorToPref(ShelfAutoHideBehavior behavior) {
155 switch (behavior) {
156 case SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS:
157 return kShelfAutoHideBehaviorAlways;
158 case SHELF_AUTO_HIDE_BEHAVIOR_NEVER:
159 return kShelfAutoHideBehaviorNever;
160 case SHELF_AUTO_HIDE_ALWAYS_HIDDEN:
161 // This one should not be a valid preference option for now. We only want
162 // to completely hide it when we run in app mode - or while we temporarily
163 // hide the shelf as part of an animation (e.g. the multi user change).
164 return nullptr;
165 }
166 NOTREACHED();
167 return nullptr;
168 }
169
34 } // namespace 170 } // namespace
35 171
36 namespace ash {
37
38 const char kPinnedAppsPrefAppIDPath[] = "id"; 172 const char kPinnedAppsPrefAppIDPath[] = "id";
39 const char kPinnedAppsPrefPinnedByPolicy[] = "pinned_by_policy"; 173 const char kPinnedAppsPrefPinnedByPolicy[] = "pinned_by_policy";
40 174
41 const char kShelfAutoHideBehaviorAlways[] = "Always"; 175 const char kShelfAutoHideBehaviorAlways[] = "Always";
42 const char kShelfAutoHideBehaviorNever[] = "Never"; 176 const char kShelfAutoHideBehaviorNever[] = "Never";
43 177
44 const char kShelfAlignmentBottom[] = "Bottom"; 178 const char kShelfAlignmentBottom[] = "Bottom";
45 const char kShelfAlignmentLeft[] = "Left"; 179 const char kShelfAlignmentLeft[] = "Left";
46 const char kShelfAlignmentRight[] = "Right"; 180 const char kShelfAlignmentRight[] = "Right";
47 181
(...skipping 22 matching lines...) Expand all
70 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); 204 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000);
71 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); 205 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false);
72 } 206 }
73 207
74 base::DictionaryValue* CreateAppDict(const std::string& app_id) { 208 base::DictionaryValue* CreateAppDict(const std::string& app_id) {
75 std::unique_ptr<base::DictionaryValue> app_value(new base::DictionaryValue); 209 std::unique_ptr<base::DictionaryValue> app_value(new base::DictionaryValue);
76 app_value->SetString(kPinnedAppsPrefAppIDPath, app_id); 210 app_value->SetString(kPinnedAppsPrefAppIDPath, app_id);
77 return app_value.release(); 211 return app_value.release();
78 } 212 }
79 213
80 ash::ShelfAlignment AlignmentFromPref(const std::string& value) { 214 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(Profile* profile,
81 if (value == ash::kShelfAlignmentLeft) 215 int64_t display_id) {
82 return ash::SHELF_ALIGNMENT_LEFT; 216 DCHECK(profile);
83 else if (value == ash::kShelfAlignmentRight) 217 DCHECK_GE(display_id, 0);
84 return ash::SHELF_ALIGNMENT_RIGHT; 218
85 // Default to bottom. 219 // Don't show the shelf in app mode.
86 return ash::SHELF_ALIGNMENT_BOTTOM; 220 if (chrome::IsRunningInAppMode())
221 return SHELF_AUTO_HIDE_ALWAYS_HIDDEN;
222
223 // See comment in |kShelfAlignment| as to why we consider two prefs.
224 return AutoHideBehaviorFromPref(
225 GetPerDisplayPref(profile, display_id, prefs::kShelfAutoHideBehaviorLocal,
226 prefs::kShelfAutoHideBehavior));
87 } 227 }
88 228
89 const char* AlignmentToPref(ash::ShelfAlignment alignment) { 229 void SetShelfAutoHideBehaviorPref(Profile* profile,
90 switch (alignment) { 230 int64_t display_id,
91 case ash::SHELF_ALIGNMENT_BOTTOM: 231 ShelfAutoHideBehavior behavior) {
92 return ash::kShelfAlignmentBottom; 232 DCHECK(profile);
93 case ash::SHELF_ALIGNMENT_LEFT: 233 DCHECK_GE(display_id, 0);
94 return ash::kShelfAlignmentLeft; 234
95 case ash::SHELF_ALIGNMENT_RIGHT: 235 const char* value = AutoHideBehaviorToPref(behavior);
96 return ash::kShelfAlignmentRight; 236 if (!value)
237 return;
238
239 SetPerDisplayPref(profile, display_id, prefs::kShelfAutoHideBehavior, value);
240 if (display_id == gfx::Screen::GetScreen()->GetPrimaryDisplay().id()) {
241 // See comment in |kShelfAlignment| about why we have two prefs here.
242 profile->GetPrefs()->SetString(prefs::kShelfAutoHideBehaviorLocal, value);
243 profile->GetPrefs()->SetString(prefs::kShelfAutoHideBehavior, value);
97 } 244 }
98 NOTREACHED();
99 return nullptr;
100 } 245 }
101 246
102 ash::ShelfAutoHideBehavior AutoHideBehaviorFromPref(const std::string& value) { 247 ShelfAlignment GetShelfAlignmentPref(Profile* profile, int64_t display_id) {
103 // Note: To maintain sync compatibility with old images of chrome/chromeos 248 DCHECK(profile);
104 // the set of values that may be encountered includes the now-extinct 249 DCHECK_GE(display_id, 0);
105 // "Default" as well as "Never" and "Always", "Default" should now 250
106 // be treated as "Never" (http://crbug.com/146773). 251 // See comment in |kShelfAlignment| as to why we consider two prefs.
107 if (value == ash::kShelfAutoHideBehaviorAlways) 252 return AlignmentFromPref(GetPerDisplayPref(profile, display_id,
108 return ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS; 253 prefs::kShelfAlignmentLocal,
109 return ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER; 254 prefs::kShelfAlignment));
110 } 255 }
111 256
112 const char* AutoHideBehaviorToPref(ash::ShelfAutoHideBehavior behavior) { 257 void SetShelfAlignmentPref(Profile* profile,
113 switch (behavior) { 258 int64_t display_id,
114 case ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS: 259 ShelfAlignment alignment) {
115 return ash::kShelfAutoHideBehaviorAlways; 260 DCHECK(profile);
116 case ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER: 261 DCHECK_GE(display_id, 0);
117 return ash::kShelfAutoHideBehaviorNever; 262
118 case ash::SHELF_AUTO_HIDE_ALWAYS_HIDDEN: 263 const char* value = AlignmentToPref(alignment);
119 // This one should not be a valid preference option for now. We only want 264 SetPerDisplayPref(profile, display_id, prefs::kShelfAlignment, value);
120 // to completely hide it when we run in app mode - or while we temporarily 265 if (display_id == gfx::Screen::GetScreen()->GetPrimaryDisplay().id()) {
121 // hide the shelf as part of an animation (e.g. the multi user change). 266 // See comment in |kShelfAlignment| as to why we consider two prefs.
122 return nullptr; 267 profile->GetPrefs()->SetString(prefs::kShelfAlignmentLocal, value);
268 profile->GetPrefs()->SetString(prefs::kShelfAlignment, value);
123 } 269 }
124 NOTREACHED();
125 return nullptr;
126 } 270 }
127 271
128 } // namespace ash 272 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698