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

Side by Side Diff: ash/common/shelf/shelf_controller.cc

Issue 2474653003: PreferencesManager (Closed)
Patch Set: Rebase Created 4 years, 1 month 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "ash/common/shelf/shelf_controller.h" 5 #include "ash/common/shelf/shelf_controller.h"
6 6
7 #include "ash/common/shelf/shelf_item_delegate.h" 7 #include "ash/common/shelf/shelf_item_delegate.h"
8 #include "ash/common/shelf/shelf_menu_model.h" 8 #include "ash/common/shelf/shelf_menu_model.h"
9 #include "ash/common/shelf/wm_shelf.h" 9 #include "ash/common/shelf/wm_shelf.h"
10 #include "ash/common/shell_delegate.h"
10 #include "ash/common/wm_lookup.h" 11 #include "ash/common/wm_lookup.h"
11 #include "ash/common/wm_root_window_controller.h" 12 #include "ash/common/wm_root_window_controller.h"
12 #include "ash/common/wm_shell.h" 13 #include "ash/common/wm_shell.h"
13 #include "ash/common/wm_window.h" 14 #include "ash/common/wm_window.h"
14 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "services/preferences/public/cpp/pref_observer_store.h"
17 #include "services/preferences/public/interfaces/preferences.mojom.h"
18 #include "services/service_manager/public/cpp/connector.h"
15 #include "ui/base/resource/resource_bundle.h" 19 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/display/display.h" 20 #include "ui/display/display.h"
17 #include "ui/display/screen.h" 21 #include "ui/display/screen.h"
18 #include "ui/gfx/image/image_skia.h" 22 #include "ui/gfx/image/image_skia.h"
19 #include "ui/resources/grit/ui_resources.h" 23 #include "ui/resources/grit/ui_resources.h"
20 24
21 namespace ash { 25 namespace ash {
22 26
23 namespace { 27 namespace {
24 28
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 // The controller may be null for invalid ids or for displays being removed. 161 // The controller may be null for invalid ids or for displays being removed.
158 WmRootWindowController* root_window_controller = 162 WmRootWindowController* root_window_controller =
159 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); 163 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id);
160 return root_window_controller ? root_window_controller->GetShelf() : nullptr; 164 return root_window_controller ? root_window_controller->GetShelf() : nullptr;
161 } 165 }
162 166
163 } // namespace 167 } // namespace
164 168
165 ShelfController::ShelfController() {} 169 ShelfController::ShelfController() {}
166 170
167 ShelfController::~ShelfController() {} 171 ShelfController::~ShelfController() {
172 if (store_)
173 store_->RemoveObserver(this);
174 }
168 175
169 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { 176 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) {
170 bindings_.AddBinding(this, std::move(request)); 177 bindings_.AddBinding(this, std::move(request));
178
179 // Sample of connecting to the PreferencesManager
msw 2016/11/18 00:32:57 aside: reducing some boilerplate might be nice.
jonross 2016/11/18 00:47:58 Moving this construction to WmShell would centrali
180 WmShell* shell = WmShell::Get();
181 service_manager::Connector* connector =
182 shell->delegate()->GetShellConnector();
183 if (!connector)
184 return;
185 prefs::mojom::PreferencesManagerPtr pref_manager_ptr;
186 connector->ConnectToInterface("service:content_browser", &pref_manager_ptr);
187 store_ = new PrefObserverStore(std::move(pref_manager_ptr));
188 std::set<std::string> keys;
189 // chrome::common::pref_names::kShelfAutoHideBehavior
msw 2016/11/18 00:32:57 aside: I guess we'll move ash-relevant pref names
jonross 2016/11/18 00:47:58 An issue with this is that the consts need to be i
James Cook 2016/11/18 00:50:34 +1. Most components have a pref_names.cc file, we
msw 2016/11/18 00:58:13 Yeah, ash/public/cpp/pref_names.h could declare th
jonross 2016/11/18 01:01:09 That SGTM
190 const std::string key("auto_hide_behavior");
191 keys.insert(key);
192 store_->AddObserver(this);
193 store_->Init(keys);
171 } 194 }
172 195
173 void ShelfController::NotifyShelfCreated(WmShelf* shelf) { 196 void ShelfController::NotifyShelfCreated(WmShelf* shelf) {
174 // Notify observers, Chrome will set alignment and auto-hide from prefs. 197 // Notify observers, Chrome will set alignment and auto-hide from prefs.
175 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); 198 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
176 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { 199 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) {
177 observer->OnShelfCreated(display_id); 200 observer->OnShelfCreated(display_id);
178 }); 201 });
179 } 202 }
180 203
181 void ShelfController::NotifyShelfAlignmentChanged(WmShelf* shelf) { 204 void ShelfController::NotifyShelfAlignmentChanged(WmShelf* shelf) {
182 ShelfAlignment alignment = shelf->alignment(); 205 ShelfAlignment alignment = shelf->alignment();
183 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); 206 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
184 observers_.ForAllPtrs( 207 observers_.ForAllPtrs(
185 [alignment, display_id](mojom::ShelfObserver* observer) { 208 [alignment, display_id](mojom::ShelfObserver* observer) {
186 observer->OnAlignmentChanged(alignment, display_id); 209 observer->OnAlignmentChanged(alignment, display_id);
187 }); 210 });
188 } 211 }
189 212
190 void ShelfController::NotifyShelfAutoHideBehaviorChanged(WmShelf* shelf) { 213 void ShelfController::NotifyShelfAutoHideBehaviorChanged(WmShelf* shelf) {
191 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior(); 214 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior();
192 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); 215 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
193 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { 216 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) {
194 observer->OnAutoHideBehaviorChanged(behavior, display_id); 217 observer->OnAutoHideBehaviorChanged(behavior, display_id);
195 }); 218 });
219
220 // Using Preferences to set the value. the chrome ShelfObserver can seprately
221 // listen to preference changes to make any according UI state changes.
222 std::string behaviour_pref;
223 switch (behavior) {
msw 2016/11/18 00:32:57 aside: would it make sense to store the behavior e
jonross 2016/11/18 00:47:58 Yeah in most cases it would make sense to just hav
224 case SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS:
225 behaviour_pref = "Always";
226 break;
227 case SHELF_AUTO_HIDE_BEHAVIOR_NEVER:
228 behaviour_pref = "Never";
229 break;
230 default:
231 behaviour_pref = "ooops";
232 }
233 std::unique_ptr<base::Value> value(new base::StringValue(behaviour_pref));
234 if (store_)
235 store_->SetValue("auto_hide_behavior", std::move(value), 0);
196 } 236 }
197 237
198 void ShelfController::AddObserver( 238 void ShelfController::AddObserver(
199 mojom::ShelfObserverAssociatedPtrInfo observer) { 239 mojom::ShelfObserverAssociatedPtrInfo observer) {
200 mojom::ShelfObserverAssociatedPtr observer_ptr; 240 mojom::ShelfObserverAssociatedPtr observer_ptr;
201 observer_ptr.Bind(std::move(observer)); 241 observer_ptr.Bind(std::move(observer));
202 observers_.AddPtr(std::move(observer_ptr)); 242 observers_.AddPtr(std::move(observer_ptr));
203 } 243 }
204 244
205 void ShelfController::SetAlignment(ShelfAlignment alignment, 245 void ShelfController::SetAlignment(ShelfAlignment alignment,
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 if (!app_id_to_shelf_id_.count(app_id)) 314 if (!app_id_to_shelf_id_.count(app_id))
275 return; 315 return;
276 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; 316 ShelfID shelf_id = app_id_to_shelf_id_[app_id];
277 int index = model_.ItemIndexByID(shelf_id); 317 int index = model_.ItemIndexByID(shelf_id);
278 DCHECK_GE(index, 0); 318 DCHECK_GE(index, 0);
279 ShelfItem item = *model_.ItemByID(shelf_id); 319 ShelfItem item = *model_.ItemByID(shelf_id);
280 item.image = GetShelfIconFromBitmap(image); 320 item.image = GetShelfIconFromBitmap(image);
281 model_.Set(index, item); 321 model_.Set(index, item);
282 } 322 }
283 323
324 void ShelfController::OnPrefValueChanged(const std::string& key) {
325 const base::Value* value = nullptr;
326 store_->GetValue("auto_hide_behavior", &value);
327 if (!value)
328 return;
329 std::string actual_value;
330 value->GetAsString(&actual_value);
331 LOG(ERROR) << "JR OnPrefValueChanged " << key << " : " << actual_value
332 << "\n";
333
334 // Call a method that needs the pref here
335 }
336
337 void ShelfController::OnInitializationCompleted(bool succeeded) {
msw 2016/11/18 00:32:57 aside: I wonder if it'd make sense to just call On
jonross 2016/11/18 00:47:58 This init/changed paradigm is just brought over fr
jonross 2016/11/19 00:28:20 I've updated this based on your suggestion. Furthe
338 LOG(ERROR) << "JR OnInitializationCompleted\n";
339 if (!succeeded)
340 return;
341 const base::Value* value = nullptr;
342 store_->GetValue("auto_hide_behavior", &value);
343 if (!value)
344 return;
345 std::string actual_value;
346 value->GetAsString(&actual_value);
347 LOG(ERROR) << "\tJR auto_hide_behavior: " << actual_value << "\n";
348
349 // Call a method that needs the pref here
350 }
351
284 } // namespace ash 352 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698