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

Side by Side Diff: chrome/browser/ui/ash/launcher/chrome_launcher_controller_per_browser.cc

Issue 11570017: Enables storing shelf's auto_hide_behavior and alignment per display. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
bartfab (slow) 2012/12/14 09:46:10 See chrome_launcher_controller_per_app.cc for comm
Jun Mukai 2012/12/14 18:47:16 I think I've done manually...
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/launcher/chrome_launcher_controller_per_browser. h" 5 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_per_browser. h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "ash/launcher/launcher_model.h" 9 #include "ash/launcher/launcher_model.h"
10 #include "ash/shell.h" 10 #include "ash/shell.h"
11 #include "ash/wm/window_util.h" 11 #include "ash/wm/window_util.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/string_number_conversions.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 #include "chrome/browser/defaults.h" 15 #include "chrome/browser/defaults.h"
15 #include "chrome/browser/extensions/extension_service.h" 16 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/prefs/incognito_mode_prefs.h" 17 #include "chrome/browser/prefs/incognito_mode_prefs.h"
17 #include "chrome/browser/prefs/pref_service.h" 18 #include "chrome/browser/prefs/pref_service.h"
18 #include "chrome/browser/prefs/scoped_user_pref_update.h" 19 #include "chrome/browser/prefs/scoped_user_pref_update.h"
19 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/browser/ui/ash/app_sync_ui_state.h" 22 #include "chrome/browser/ui/ash/app_sync_ui_state.h"
22 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h" 23 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
(...skipping 14 matching lines...) Expand all
37 #include "chrome/common/chrome_switches.h" 38 #include "chrome/common/chrome_switches.h"
38 #include "chrome/common/extensions/extension.h" 39 #include "chrome/common/extensions/extension.h"
39 #include "chrome/common/extensions/extension_resource.h" 40 #include "chrome/common/extensions/extension_resource.h"
40 #include "chrome/common/pref_names.h" 41 #include "chrome/common/pref_names.h"
41 #include "chrome/common/url_constants.h" 42 #include "chrome/common/url_constants.h"
42 #include "content/public/browser/navigation_entry.h" 43 #include "content/public/browser/navigation_entry.h"
43 #include "content/public/browser/notification_service.h" 44 #include "content/public/browser/notification_service.h"
44 #include "content/public/browser/web_contents.h" 45 #include "content/public/browser/web_contents.h"
45 #include "extensions/common/url_pattern.h" 46 #include "extensions/common/url_pattern.h"
46 #include "grit/theme_resources.h" 47 #include "grit/theme_resources.h"
48 #include "ui/aura/root_window.h"
47 #include "ui/aura/window.h" 49 #include "ui/aura/window.h"
48 50
49 using content::WebContents; 51 using content::WebContents;
50 using extensions::Extension; 52 using extensions::Extension;
51 53
52 namespace { 54 namespace {
53 55
54 // Item controller for an app shortcut. Shortcuts track app and launcher ids, 56 // Item controller for an app shortcut. Shortcuts track app and launcher ids,
55 // but do not have any associated windows (opening a shortcut will replace the 57 // but do not have any associated windows (opening a shortcut will replace the
56 // item with the appropriate LauncherItemController type). 58 // item with the appropriate LauncherItemController type).
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 114
113 // Stores the optional refocus url pattern for this item. 115 // Stores the optional refocus url pattern for this item.
114 const GURL& refocus_url() const { return refocus_url_; } 116 const GURL& refocus_url() const { return refocus_url_; }
115 void set_refocus_url(const GURL& refocus_url) { refocus_url_ = refocus_url; } 117 void set_refocus_url(const GURL& refocus_url) { refocus_url_ = refocus_url; }
116 118
117 private: 119 private:
118 GURL refocus_url_; 120 GURL refocus_url_;
119 DISALLOW_COPY_AND_ASSIGN(AppShortcutLauncherItemController); 121 DISALLOW_COPY_AND_ASSIGN(AppShortcutLauncherItemController);
120 }; 122 };
121 123
124 std::string GetPrefKeyForRootWindow(aura::RootWindow* root_window) {
125 if (!root_window)
126 return std::string();
127
128 gfx::Display display = gfx::Screen::GetScreenFor(
129 root_window)->GetDisplayNearestWindow(root_window);
130 if (!display.is_valid())
131 return std::string();
132
133 return base::Int64ToString(display.id());
134 }
135
136 void UpdatePerDisplayPref(PrefService* pref_service,
137 aura::RootWindow* root_window,
138 const char* pref_key,
139 const std::string& value) {
140 std::string key = GetPrefKeyForRootWindow(root_window);
141 if (key.empty())
142 return;
143
144 DictionaryPrefUpdate update(pref_service, prefs::kShelfPreferences);
145 base::DictionaryValue* shelf_prefs = update.Get();
146 base::DictionaryValue* prefs = NULL;
147 if (!shelf_prefs->GetDictionary(key, &prefs)) {
148 prefs = new base::DictionaryValue();
149 shelf_prefs->Set(key, prefs);
150 }
151 prefs->SetStringWithoutPathExpansion(pref_key, value);
152 }
153
122 // If the value of the pref at |local_path is not empty, it is returned 154 // If the value of the pref at |local_path is not empty, it is returned
123 // otherwise the value of the pref at |synced_path| is returned. 155 // otherwise the value of the pref at |synced_path| is returned.
124 std::string GetLocalOrRemotePref(PrefService* pref_service, 156 std::string GetLocalOrRemotePref(PrefService* pref_service,
157 aura::RootWindow* root_window,
125 const char* local_path, 158 const char* local_path,
126 const char* synced_path) { 159 const char* synced_path) {
127 const std::string value(pref_service->GetString(local_path)); 160 const std::string value(pref_service->GetString(local_path));
161 if (pref_service->FindPreference(local_path)->IsRecommended())
162 return value;
163
164 std::string pref_key = GetPrefKeyForRootWindow(root_window);
165 if (!pref_key.empty()) {
166 const base::DictionaryValue* shelf_prefs = pref_service->GetDictionary(
167 prefs::kShelfPreferences);
168 const base::DictionaryValue* display_pref = NULL;
169 std::string per_display_value;
170 if (shelf_prefs->GetDictionary(pref_key, &display_pref) &&
171 display_pref->GetString(synced_path, &per_display_value)) {
172 return per_display_value;
173 }
174 }
175
128 return value.empty() ? pref_service->GetString(synced_path) : value; 176 return value.empty() ? pref_service->GetString(synced_path) : value;
129 } 177 }
130 178
131 // If prefs have synced and no user-set value exists at |local_path|, the value 179 // If prefs have synced and no user-set value exists at |local_path|, the value
132 // from |synced_path| is copied to |local_path|. 180 // from |synced_path| is copied to |local_path|.
133 void MaybePropagatePrefToLocal(PrefService* pref_service, 181 void MaybePropagatePrefToLocal(PrefService* pref_service,
134 const char* local_path, 182 const char* local_path,
135 const char* synced_path) { 183 const char* synced_path) {
136 if (!pref_service->FindPreference(local_path)->HasUserSetting() && 184 if (!pref_service->FindPreference(local_path)->HasUserSetting() &&
137 pref_service->IsSyncing()) { 185 pref_service->IsSyncing()) {
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 611
564 bool ChromeLauncherControllerPerBrowser::CanPin() const { 612 bool ChromeLauncherControllerPerBrowser::CanPin() const {
565 const PrefService::Preference* pref = 613 const PrefService::Preference* pref =
566 profile_->GetPrefs()->FindPreference(prefs::kPinnedLauncherApps); 614 profile_->GetPrefs()->FindPreference(prefs::kPinnedLauncherApps);
567 return pref && pref->IsUserModifiable(); 615 return pref && pref->IsUserModifiable();
568 } 616 }
569 617
570 ash::ShelfAutoHideBehavior 618 ash::ShelfAutoHideBehavior
571 ChromeLauncherControllerPerBrowser::GetShelfAutoHideBehavior( 619 ChromeLauncherControllerPerBrowser::GetShelfAutoHideBehavior(
572 aura::RootWindow* root_window) const { 620 aura::RootWindow* root_window) const {
573 // TODO(oshima): Support multiple launchers.
574
575 // See comment in |kShelfAlignment| as to why we consider two prefs. 621 // See comment in |kShelfAlignment| as to why we consider two prefs.
576 const std::string behavior_value( 622 const std::string behavior_value(
577 GetLocalOrRemotePref(profile_->GetPrefs(), 623 GetLocalOrRemotePref(profile_->GetPrefs(),
624 root_window,
578 prefs::kShelfAutoHideBehaviorLocal, 625 prefs::kShelfAutoHideBehaviorLocal,
579 prefs::kShelfAutoHideBehavior)); 626 prefs::kShelfAutoHideBehavior));
580 627
581 // Note: To maintain sync compatibility with old images of chrome/chromeos 628 // Note: To maintain sync compatibility with old images of chrome/chromeos
582 // the set of values that may be encountered includes the now-extinct 629 // the set of values that may be encountered includes the now-extinct
583 // "Default" as well as "Never" and "Always", "Default" should now 630 // "Default" as well as "Never" and "Always", "Default" should now
584 // be treated as "Never" (http://crbug.com/146773). 631 // be treated as "Never" (http://crbug.com/146773).
585 if (behavior_value == ash::kShelfAutoHideBehaviorAlways) 632 if (behavior_value == ash::kShelfAutoHideBehaviorAlways)
586 return ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS; 633 return ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS;
587 return ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER; 634 return ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER;
588 } 635 }
589 636
590 bool ChromeLauncherControllerPerBrowser::CanUserModifyShelfAutoHideBehavior( 637 bool ChromeLauncherControllerPerBrowser::CanUserModifyShelfAutoHideBehavior(
591 aura::RootWindow* root_window) const { 638 aura::RootWindow* root_window) const {
592 // TODO(oshima): Support multiple launchers.
593 return profile_->GetPrefs()-> 639 return profile_->GetPrefs()->
594 FindPreference(prefs::kShelfAutoHideBehaviorLocal)->IsUserModifiable(); 640 FindPreference(prefs::kShelfAutoHideBehaviorLocal)->IsUserModifiable();
595 } 641 }
596 642
597 void ChromeLauncherControllerPerBrowser::ToggleShelfAutoHideBehavior( 643 void ChromeLauncherControllerPerBrowser::ToggleShelfAutoHideBehavior(
598 aura::RootWindow* root_window) { 644 aura::RootWindow* root_window) {
599 ash::ShelfAutoHideBehavior behavior = GetShelfAutoHideBehavior(root_window) == 645 ash::ShelfAutoHideBehavior behavior = GetShelfAutoHideBehavior(root_window) ==
600 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS ? 646 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS ?
601 ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER : 647 ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER :
602 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS; 648 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS;
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 if (IsAppPinned(extension->id())) 844 if (IsAppPinned(extension->id()))
799 DoUnpinAppsWithID(extension->id()); 845 DoUnpinAppsWithID(extension->id());
800 app_icon_loader_->ClearImage(extension->id()); 846 app_icon_loader_->ClearImage(extension->id());
801 break; 847 break;
802 } 848 }
803 default: 849 default:
804 NOTREACHED() << "Unexpected notification type=" << type; 850 NOTREACHED() << "Unexpected notification type=" << type;
805 } 851 }
806 } 852 }
807 853
808 void ChromeLauncherControllerPerBrowser::OnShelfAlignmentChanged() { 854 void ChromeLauncherControllerPerBrowser::OnShelfAlignmentChanged(
855 aura::RootWindow* root_window) {
809 const char* pref_value = NULL; 856 const char* pref_value = NULL;
810 // TODO(oshima): Support multiple displays. 857 switch (ash::Shell::GetInstance()->GetShelfAlignment(root_window)) {
811 switch (ash::Shell::GetInstance()->GetShelfAlignment(
812 ash::Shell::GetPrimaryRootWindow())) {
813 case ash::SHELF_ALIGNMENT_BOTTOM: 858 case ash::SHELF_ALIGNMENT_BOTTOM:
814 pref_value = ash::kShelfAlignmentBottom; 859 pref_value = ash::kShelfAlignmentBottom;
815 break; 860 break;
816 case ash::SHELF_ALIGNMENT_LEFT: 861 case ash::SHELF_ALIGNMENT_LEFT:
817 pref_value = ash::kShelfAlignmentLeft; 862 pref_value = ash::kShelfAlignmentLeft;
818 break; 863 break;
819 case ash::SHELF_ALIGNMENT_RIGHT: 864 case ash::SHELF_ALIGNMENT_RIGHT:
820 pref_value = ash::kShelfAlignmentRight; 865 pref_value = ash::kShelfAlignmentRight;
821 break; 866 break;
822 } 867 }
823 // See comment in |kShelfAlignment| about why we have two prefs here. 868
824 profile_->GetPrefs()->SetString(prefs::kShelfAlignmentLocal, pref_value); 869 UpdatePerDisplayPref(
825 profile_->GetPrefs()->SetString(prefs::kShelfAlignment, pref_value); 870 profile_->GetPrefs(), root_window, prefs::kShelfAlignment, pref_value);
871
872 if (root_window == ash::Shell::GetPrimaryRootWindow()) {
873 // See comment in |kShelfAlignment| about why we have two prefs here.
874 profile_->GetPrefs()->SetString(prefs::kShelfAlignmentLocal, pref_value);
875 profile_->GetPrefs()->SetString(prefs::kShelfAlignment, pref_value);
876 }
826 } 877 }
827 878
828 void ChromeLauncherControllerPerBrowser::OnIsSyncingChanged() { 879 void ChromeLauncherControllerPerBrowser::OnIsSyncingChanged() {
829 MaybePropagatePrefToLocal(profile_->GetPrefs(), 880 MaybePropagatePrefToLocal(profile_->GetPrefs(),
830 prefs::kShelfAlignmentLocal, 881 prefs::kShelfAlignmentLocal,
831 prefs::kShelfAlignment); 882 prefs::kShelfAlignment);
832 MaybePropagatePrefToLocal(profile_->GetPrefs(), 883 MaybePropagatePrefToLocal(profile_->GetPrefs(),
833 prefs::kShelfAutoHideBehaviorLocal, 884 prefs::kShelfAutoHideBehaviorLocal,
834 prefs::kShelfAutoHideBehavior); 885 prefs::kShelfAutoHideBehavior);
835 } 886 }
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 } 1043 }
993 1044
994 // Append unprocessed items from the pref to the end of the model. 1045 // Append unprocessed items from the pref to the end of the model.
995 for (; pref_app_id != pinned_apps.end(); ++pref_app_id) 1046 for (; pref_app_id != pinned_apps.end(); ++pref_app_id)
996 DoPinAppWithID(*pref_app_id); 1047 DoPinAppWithID(*pref_app_id);
997 } 1048 }
998 1049
999 void ChromeLauncherControllerPerBrowser::SetShelfAutoHideBehaviorPrefs( 1050 void ChromeLauncherControllerPerBrowser::SetShelfAutoHideBehaviorPrefs(
1000 ash::ShelfAutoHideBehavior behavior, 1051 ash::ShelfAutoHideBehavior behavior,
1001 aura::RootWindow* root_window) { 1052 aura::RootWindow* root_window) {
1002 // TODO(oshima): Support multiple launchers.
1003 if (root_window != ash::Shell::GetPrimaryRootWindow())
1004 return;
1005
1006 const char* value = NULL; 1053 const char* value = NULL;
1007 switch (behavior) { 1054 switch (behavior) {
1008 case ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS: 1055 case ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS:
1009 value = ash::kShelfAutoHideBehaviorAlways; 1056 value = ash::kShelfAutoHideBehaviorAlways;
1010 break; 1057 break;
1011 case ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER: 1058 case ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER:
1012 value = ash::kShelfAutoHideBehaviorNever; 1059 value = ash::kShelfAutoHideBehaviorNever;
1013 break; 1060 break;
1014 } 1061 }
1015 // See comment in |kShelfAlignment| about why we have two prefs here. 1062
1016 profile_->GetPrefs()->SetString(prefs::kShelfAutoHideBehaviorLocal, value); 1063 UpdatePerDisplayPref(
1017 profile_->GetPrefs()->SetString(prefs::kShelfAutoHideBehavior, value); 1064 profile_->GetPrefs(), root_window, prefs::kShelfAutoHideBehavior, value);
1065
1066 if (root_window != ash::Shell::GetPrimaryRootWindow()) {
1067 // See comment in |kShelfAlignment| about why we have two prefs here.
1068 profile_->GetPrefs()->SetString(prefs::kShelfAutoHideBehaviorLocal, value);
1069 profile_->GetPrefs()->SetString(prefs::kShelfAutoHideBehavior, value);
1070 }
1018 } 1071 }
1019 1072
1020 void ChromeLauncherControllerPerBrowser::SetShelfAutoHideBehaviorFromPrefs() { 1073 void ChromeLauncherControllerPerBrowser::SetShelfAutoHideBehaviorFromPrefs() {
1021 // TODO(oshima): Support multiple displays. 1074 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows();
1022 aura::RootWindow* root_window = ash::Shell::GetPrimaryRootWindow(); 1075 for (ash::Shell::RootWindowList::const_iterator iter = root_windows.begin();
1023 ash::Shell::GetInstance()->SetShelfAutoHideBehavior( 1076 iter != root_windows.end(); ++iter) {
1024 GetShelfAutoHideBehavior(root_window), root_window); 1077 ash::Shell::GetInstance()->SetShelfAutoHideBehavior(
1078 GetShelfAutoHideBehavior(*iter), *iter);
1079 }
1025 } 1080 }
1026 1081
1027 void ChromeLauncherControllerPerBrowser::SetShelfAlignmentFromPrefs() { 1082 void ChromeLauncherControllerPerBrowser::SetShelfAlignmentFromPrefs() {
1028 if (!CommandLine::ForCurrentProcess()->HasSwitch( 1083 if (!CommandLine::ForCurrentProcess()->HasSwitch(
1029 switches::kShowLauncherAlignmentMenu)) 1084 switches::kShowLauncherAlignmentMenu))
1030 return; 1085 return;
1031 1086
1032 // See comment in |kShelfAlignment| as to why we consider two prefs. 1087 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows();
1033 const std::string alignment_value( 1088 for (ash::Shell::RootWindowList::const_iterator iter = root_windows.begin();
1034 GetLocalOrRemotePref(profile_->GetPrefs(), 1089 iter != root_windows.end(); ++iter) {
1035 prefs::kShelfAlignmentLocal, 1090 // See comment in |kShelfAlignment| as to why we consider two prefs.
1036 prefs::kShelfAlignment)); 1091 const std::string alignment_value(
1037 ash::ShelfAlignment alignment = ash::SHELF_ALIGNMENT_BOTTOM; 1092 GetLocalOrRemotePref(profile_->GetPrefs(),
1038 if (alignment_value == ash::kShelfAlignmentLeft) 1093 *iter,
1039 alignment = ash::SHELF_ALIGNMENT_LEFT; 1094 prefs::kShelfAlignmentLocal,
1040 else if (alignment_value == ash::kShelfAlignmentRight) 1095 prefs::kShelfAlignment));
1041 alignment = ash::SHELF_ALIGNMENT_RIGHT; 1096 ash::ShelfAlignment alignment = ash::SHELF_ALIGNMENT_BOTTOM;
1042 // TODO(oshima): Support multiple displays. 1097 if (alignment_value == ash::kShelfAlignmentLeft)
1043 ash::Shell::GetInstance()->SetShelfAlignment( 1098 alignment = ash::SHELF_ALIGNMENT_LEFT;
1044 alignment, ash::Shell::GetPrimaryRootWindow()); 1099 else if (alignment_value == ash::kShelfAlignmentRight)
1100 alignment = ash::SHELF_ALIGNMENT_RIGHT;
1101 ash::Shell::GetInstance()->SetShelfAlignment(alignment, *iter);
1102 }
1045 } 1103 }
1046 1104
1047 WebContents* ChromeLauncherControllerPerBrowser::GetLastActiveWebContents( 1105 WebContents* ChromeLauncherControllerPerBrowser::GetLastActiveWebContents(
1048 const std::string& app_id) { 1106 const std::string& app_id) {
1049 AppIDToWebContentsListMap::const_iterator i = 1107 AppIDToWebContentsListMap::const_iterator i =
1050 app_id_to_web_contents_list_.find(app_id); 1108 app_id_to_web_contents_list_.find(app_id);
1051 if (i == app_id_to_web_contents_list_.end()) 1109 if (i == app_id_to_web_contents_list_.end())
1052 return NULL; 1110 return NULL;
1053 DCHECK_GT(i->second.size(), 0u); 1111 DCHECK_GT(i->second.size(), 0u);
1054 return *i->second.begin(); 1112 return *i->second.begin();
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1114 void ChromeLauncherControllerPerBrowser::SetAppIconLoaderForTest( 1172 void ChromeLauncherControllerPerBrowser::SetAppIconLoaderForTest(
1115 AppIconLoader* loader) { 1173 AppIconLoader* loader) {
1116 app_icon_loader_.reset(loader); 1174 app_icon_loader_.reset(loader);
1117 } 1175 }
1118 1176
1119 const std::string& 1177 const std::string&
1120 ChromeLauncherControllerPerBrowser::GetAppIdFromLauncherIdForTest( 1178 ChromeLauncherControllerPerBrowser::GetAppIdFromLauncherIdForTest(
1121 ash::LauncherID id) { 1179 ash::LauncherID id) {
1122 return id_to_item_controller_map_[id]->app_id(); 1180 return id_to_item_controller_map_[id]->app_id();
1123 } 1181 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698