OLD | NEW |
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/launcher/chrome_launcher_controller.h" | 5 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "ash/launcher/launcher_model.h" | 10 #include "ash/launcher/launcher_model.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 117 |
118 // Stores the optional refocus url pattern for this item. | 118 // Stores the optional refocus url pattern for this item. |
119 const GURL& refocus_url() const { return refocus_url_; } | 119 const GURL& refocus_url() const { return refocus_url_; } |
120 void set_refocus_url(const GURL& refocus_url) { refocus_url_ = refocus_url; } | 120 void set_refocus_url(const GURL& refocus_url) { refocus_url_ = refocus_url; } |
121 | 121 |
122 private: | 122 private: |
123 GURL refocus_url_; | 123 GURL refocus_url_; |
124 DISALLOW_COPY_AND_ASSIGN(AppShortcutLauncherItemController); | 124 DISALLOW_COPY_AND_ASSIGN(AppShortcutLauncherItemController); |
125 }; | 125 }; |
126 | 126 |
| 127 // If the value of the pref at |local_path is not empty, it is returned |
| 128 // otherwise the value of the pref at |synced_path| is returned. |
| 129 std::string GetLocalOrRemotePref(PrefService* pref_service, |
| 130 const char* local_path, |
| 131 const char* synced_path) { |
| 132 const std::string value(pref_service->GetString(local_path)); |
| 133 return value.empty() ? pref_service->GetString(synced_path) : value; |
| 134 } |
| 135 |
| 136 // If prefs have synced and the pref value at |local_path| is empty the value |
| 137 // from |synced_path| is copied to |local_path|. |
| 138 void MaybePropagatePrefToLocal(PrefService* pref_service, |
| 139 const char* local_path, |
| 140 const char* synced_path) { |
| 141 if (pref_service->GetString(local_path).empty() && |
| 142 pref_service->HasSynced()) { |
| 143 // First time the user is using this machine, propagate from remote to |
| 144 // local. |
| 145 pref_service->SetString(local_path, pref_service->GetString(synced_path)); |
| 146 } |
| 147 } |
| 148 |
127 } // namespace | 149 } // namespace |
128 | 150 |
129 // ChromeLauncherController ---------------------------------------------------- | 151 // ChromeLauncherController ---------------------------------------------------- |
130 | 152 |
131 // statics | 153 // statics |
132 ChromeLauncherController* ChromeLauncherController::instance_ = NULL; | 154 ChromeLauncherController* ChromeLauncherController::instance_ = NULL; |
133 | 155 |
134 ChromeLauncherController::ChromeLauncherController(Profile* profile, | 156 ChromeLauncherController::ChromeLauncherController(Profile* profile, |
135 ash::LauncherModel* model) | 157 ash::LauncherModel* model) |
136 : model_(model), | 158 : model_(model), |
(...skipping 24 matching lines...) Expand all Loading... |
161 app_icon_loader_.reset(new LauncherAppIconLoader(profile_, this)); | 183 app_icon_loader_.reset(new LauncherAppIconLoader(profile_, this)); |
162 | 184 |
163 notification_registrar_.Add(this, | 185 notification_registrar_.Add(this, |
164 chrome::NOTIFICATION_EXTENSION_LOADED, | 186 chrome::NOTIFICATION_EXTENSION_LOADED, |
165 content::Source<Profile>(profile_)); | 187 content::Source<Profile>(profile_)); |
166 notification_registrar_.Add(this, | 188 notification_registrar_.Add(this, |
167 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 189 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
168 content::Source<Profile>(profile_)); | 190 content::Source<Profile>(profile_)); |
169 pref_change_registrar_.Init(profile_->GetPrefs()); | 191 pref_change_registrar_.Init(profile_->GetPrefs()); |
170 pref_change_registrar_.Add(prefs::kPinnedLauncherApps, this); | 192 pref_change_registrar_.Add(prefs::kPinnedLauncherApps, this); |
171 pref_change_registrar_.Add(prefs::kShelfAlignment, this); | |
172 pref_change_registrar_.Add(prefs::kShelfAutoHideBehavior, this); | |
173 } | 193 } |
174 | 194 |
175 ChromeLauncherController::~ChromeLauncherController() { | 195 ChromeLauncherController::~ChromeLauncherController() { |
176 // Reset the shell window controller here since it has a weak pointer to this. | 196 // Reset the shell window controller here since it has a weak pointer to this. |
177 shell_window_controller_.reset(); | 197 shell_window_controller_.reset(); |
178 | 198 |
179 model_->RemoveObserver(this); | 199 model_->RemoveObserver(this); |
180 for (IDToItemControllerMap::iterator i = id_to_item_controller_map_.begin(); | 200 for (IDToItemControllerMap::iterator i = id_to_item_controller_map_.begin(); |
181 i != id_to_item_controller_map_.end(); ++i) { | 201 i != id_to_item_controller_map_.end(); ++i) { |
182 i->second->OnRemoved(); | 202 i->second->OnRemoved(); |
183 model_->RemoveItemAt(model_->ItemIndexByID(i->first)); | 203 model_->RemoveItemAt(model_->ItemIndexByID(i->first)); |
184 } | 204 } |
185 if (instance_ == this) | 205 if (instance_ == this) |
186 instance_ = NULL; | 206 instance_ = NULL; |
187 | 207 |
188 if (ash::Shell::HasInstance()) | 208 if (ash::Shell::HasInstance()) |
189 ash::Shell::GetInstance()->RemoveShellObserver(this); | 209 ash::Shell::GetInstance()->RemoveShellObserver(this); |
190 | 210 |
191 if (observed_sync_service_) | 211 if (observed_sync_service_) |
192 observed_sync_service_->RemoveObserver(this); | 212 observed_sync_service_->RemoveObserver(this); |
| 213 |
| 214 profile_->GetPrefs()->RemoveObserver(this); |
193 } | 215 } |
194 | 216 |
195 void ChromeLauncherController::Init() { | 217 void ChromeLauncherController::Init() { |
196 // TODO(xiyuan): Remove migration code and kUseDefaultPinnedApp after M20. | 218 // TODO(xiyuan): Remove migration code and kUseDefaultPinnedApp after M20. |
197 // Migration cases: | 219 // Migration cases: |
198 // - Users that unpin all apps: | 220 // - Users that unpin all apps: |
199 // - have default pinned apps | 221 // - have default pinned apps |
200 // - kUseDefaultPinnedApps set to false | 222 // - kUseDefaultPinnedApps set to false |
201 // Migrate them by setting an empty list for kPinnedLauncherApps. | 223 // Migrate them by setting an empty list for kPinnedLauncherApps. |
202 // | 224 // |
(...skipping 12 matching lines...) Expand all Loading... |
215 ListPrefUpdate updater(profile_->GetPrefs(), prefs::kPinnedLauncherApps); | 237 ListPrefUpdate updater(profile_->GetPrefs(), prefs::kPinnedLauncherApps); |
216 updater.Get()->Clear(); | 238 updater.Get()->Clear(); |
217 } | 239 } |
218 | 240 |
219 UpdateAppLaunchersFromPref(); | 241 UpdateAppLaunchersFromPref(); |
220 | 242 |
221 // TODO(sky): update unit test so that this test isn't necessary. | 243 // TODO(sky): update unit test so that this test isn't necessary. |
222 if (ash::Shell::HasInstance()) { | 244 if (ash::Shell::HasInstance()) { |
223 SetShelfAutoHideBehaviorFromPrefs(); | 245 SetShelfAutoHideBehaviorFromPrefs(); |
224 SetShelfAlignmentFromPrefs(); | 246 SetShelfAlignmentFromPrefs(); |
| 247 PrefService* prefs = profile_->GetPrefs(); |
| 248 if (prefs->GetString(prefs::kShelfAlignmentLocal).empty() || |
| 249 prefs->GetString(prefs::kShelfAutoHideBehaviorLocal).empty()) { |
| 250 prefs->AddObserver(this); |
| 251 } |
225 ash::Shell::GetInstance()->AddShellObserver(this); | 252 ash::Shell::GetInstance()->AddShellObserver(this); |
226 } | 253 } |
227 } | 254 } |
228 | 255 |
229 ash::LauncherID ChromeLauncherController::CreateTabbedLauncherItem( | 256 ash::LauncherID ChromeLauncherController::CreateTabbedLauncherItem( |
230 LauncherItemController* controller, | 257 LauncherItemController* controller, |
231 IncognitoState is_incognito, | 258 IncognitoState is_incognito, |
232 ash::LauncherItemStatus status) { | 259 ash::LauncherItemStatus status) { |
233 ash::LauncherID id = model_->next_id(); | 260 ash::LauncherID id = model_->next_id(); |
234 DCHECK(!HasItemController(id)); | 261 DCHECK(!HasItemController(id)); |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 | 581 |
555 const char* value = NULL; | 582 const char* value = NULL; |
556 switch (behavior) { | 583 switch (behavior) { |
557 case ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS: | 584 case ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS: |
558 value = ash::kShelfAutoHideBehaviorAlways; | 585 value = ash::kShelfAutoHideBehaviorAlways; |
559 break; | 586 break; |
560 case ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER: | 587 case ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER: |
561 value = ash::kShelfAutoHideBehaviorNever; | 588 value = ash::kShelfAutoHideBehaviorNever; |
562 break; | 589 break; |
563 } | 590 } |
| 591 // See comment in |kShelfAlignment| about why we have two prefs here. |
| 592 profile_->GetPrefs()->SetString(prefs::kShelfAutoHideBehaviorLocal, value); |
564 profile_->GetPrefs()->SetString(prefs::kShelfAutoHideBehavior, value); | 593 profile_->GetPrefs()->SetString(prefs::kShelfAutoHideBehavior, value); |
565 } | 594 } |
566 | 595 |
567 void ChromeLauncherController::RemoveTabFromRunningApp( | 596 void ChromeLauncherController::RemoveTabFromRunningApp( |
568 TabContents* tab, | 597 TabContents* tab, |
569 const std::string& app_id) { | 598 const std::string& app_id) { |
570 tab_contents_to_app_id_.erase(tab); | 599 tab_contents_to_app_id_.erase(tab); |
571 AppIDToTabContentsListMap::iterator i_app_id = | 600 AppIDToTabContentsListMap::iterator i_app_id = |
572 app_id_to_tab_contents_list_.find(app_id); | 601 app_id_to_tab_contents_list_.find(app_id); |
573 if (i_app_id != app_id_to_tab_contents_list_.end()) { | 602 if (i_app_id != app_id_to_tab_contents_list_.end()) { |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 details); | 780 details); |
752 const Extension* extension = unload_info->extension; | 781 const Extension* extension = unload_info->extension; |
753 if (IsAppPinned(extension->id())) | 782 if (IsAppPinned(extension->id())) |
754 DoUnpinAppsWithID(extension->id()); | 783 DoUnpinAppsWithID(extension->id()); |
755 app_icon_loader_->ClearImage(extension->id()); | 784 app_icon_loader_->ClearImage(extension->id()); |
756 break; | 785 break; |
757 } | 786 } |
758 case chrome::NOTIFICATION_PREF_CHANGED: { | 787 case chrome::NOTIFICATION_PREF_CHANGED: { |
759 const std::string& pref_name( | 788 const std::string& pref_name( |
760 *content::Details<std::string>(details).ptr()); | 789 *content::Details<std::string>(details).ptr()); |
761 if (pref_name == prefs::kPinnedLauncherApps) | 790 if (pref_name == prefs::kPinnedLauncherApps) { |
762 UpdateAppLaunchersFromPref(); | 791 UpdateAppLaunchersFromPref(); |
763 else if (pref_name == prefs::kShelfAlignment) | 792 } else if (pref_name == prefs::kShelfAlignmentLocal) { |
764 SetShelfAlignmentFromPrefs(); | 793 SetShelfAlignmentFromPrefs(); |
765 else if (pref_name == prefs::kShelfAutoHideBehavior) | 794 } else if (pref_name == prefs::kShelfAutoHideBehaviorLocal) { |
766 SetShelfAutoHideBehaviorFromPrefs(); | 795 SetShelfAutoHideBehaviorFromPrefs(); |
767 else | 796 } else { |
768 NOTREACHED() << "Unexpected pref change for " << pref_name; | 797 NOTREACHED() << "Unexpected pref change for " << pref_name; |
| 798 } |
769 break; | 799 break; |
770 } | 800 } |
771 default: | 801 default: |
772 NOTREACHED() << "Unexpected notification type=" << type; | 802 NOTREACHED() << "Unexpected notification type=" << type; |
773 } | 803 } |
774 } | 804 } |
775 | 805 |
776 void ChromeLauncherController::OnShelfAlignmentChanged() { | 806 void ChromeLauncherController::OnShelfAlignmentChanged() { |
777 const char* pref_value = NULL; | 807 const char* pref_value = NULL; |
778 // TODO(oshima): Support multiple displays. | 808 // TODO(oshima): Support multiple displays. |
779 switch (ash::Shell::GetInstance()->GetShelfAlignment( | 809 switch (ash::Shell::GetInstance()->GetShelfAlignment( |
780 ash::Shell::GetPrimaryRootWindow())) { | 810 ash::Shell::GetPrimaryRootWindow())) { |
781 case ash::SHELF_ALIGNMENT_BOTTOM: | 811 case ash::SHELF_ALIGNMENT_BOTTOM: |
782 pref_value = ash::kShelfAlignmentBottom; | 812 pref_value = ash::kShelfAlignmentBottom; |
783 break; | 813 break; |
784 case ash::SHELF_ALIGNMENT_LEFT: | 814 case ash::SHELF_ALIGNMENT_LEFT: |
785 pref_value = ash::kShelfAlignmentLeft; | 815 pref_value = ash::kShelfAlignmentLeft; |
786 break; | 816 break; |
787 case ash::SHELF_ALIGNMENT_RIGHT: | 817 case ash::SHELF_ALIGNMENT_RIGHT: |
788 pref_value = ash::kShelfAlignmentRight; | 818 pref_value = ash::kShelfAlignmentRight; |
789 break; | 819 break; |
790 } | 820 } |
| 821 // See comment in |kShelfAlignment| about why we have two prefs here. |
| 822 profile_->GetPrefs()->SetString(prefs::kShelfAlignmentLocal, pref_value); |
791 profile_->GetPrefs()->SetString(prefs::kShelfAlignment, pref_value); | 823 profile_->GetPrefs()->SetString(prefs::kShelfAlignment, pref_value); |
792 } | 824 } |
793 | 825 |
794 void ChromeLauncherController::OnStateChanged() { | 826 void ChromeLauncherController::OnStateChanged() { |
795 DCHECK(observed_sync_service_); | 827 DCHECK(observed_sync_service_); |
796 CheckAppSync(); | 828 CheckAppSync(); |
797 } | 829 } |
798 | 830 |
| 831 void ChromeLauncherController::OnHasSyncedChanged() { |
| 832 MaybePropagatePrefToLocal(profile_->GetPrefs(), |
| 833 prefs::kShelfAlignmentLocal, |
| 834 prefs::kShelfAlignment); |
| 835 MaybePropagatePrefToLocal(profile_->GetPrefs(), |
| 836 prefs::kShelfAutoHideBehaviorLocal, |
| 837 prefs::kShelfAutoHideBehavior); |
| 838 } |
| 839 |
799 void ChromeLauncherController::PersistPinnedState() { | 840 void ChromeLauncherController::PersistPinnedState() { |
800 // It is a coding error to call PersistPinnedState() if the pinned apps are | 841 // It is a coding error to call PersistPinnedState() if the pinned apps are |
801 // not user-editable. The code should check earlier and not perform any | 842 // not user-editable. The code should check earlier and not perform any |
802 // modification actions that trigger persisting the state. | 843 // modification actions that trigger persisting the state. |
803 if (!CanPin()) { | 844 if (!CanPin()) { |
804 NOTREACHED() << "Can't pin but pinned state being updated"; | 845 NOTREACHED() << "Can't pin but pinned state being updated"; |
805 return; | 846 return; |
806 } | 847 } |
807 | 848 |
808 // Set kUseDefaultPinnedApps to false and use pinned apps list from prefs | 849 // Set kUseDefaultPinnedApps to false and use pinned apps list from prefs |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 else | 983 else |
943 ++index; | 984 ++index; |
944 } | 985 } |
945 | 986 |
946 // Append unprocessed items from the pref to the end of the model. | 987 // Append unprocessed items from the pref to the end of the model. |
947 for (; pref_app_id != pinned_apps.end(); ++pref_app_id) | 988 for (; pref_app_id != pinned_apps.end(); ++pref_app_id) |
948 DoPinAppWithID(*pref_app_id); | 989 DoPinAppWithID(*pref_app_id); |
949 } | 990 } |
950 | 991 |
951 void ChromeLauncherController::SetShelfAutoHideBehaviorFromPrefs() { | 992 void ChromeLauncherController::SetShelfAutoHideBehaviorFromPrefs() { |
| 993 // See comment in |kShelfAlignment| as to why we consider two prefs. |
| 994 const std::string behavior_value( |
| 995 GetLocalOrRemotePref(profile_->GetPrefs(), |
| 996 prefs::kShelfAutoHideBehaviorLocal, |
| 997 prefs::kShelfAutoHideBehavior)); |
| 998 |
952 // Note: To maintain sync compatibility with old images of chrome/chromeos | 999 // Note: To maintain sync compatibility with old images of chrome/chromeos |
953 // the set of values that may be encountered includes the now-extinct | 1000 // the set of values that may be encountered includes the now-extinct |
954 // "Default" as well as "Never" and "Always", "Default" should now | 1001 // "Default" as well as "Never" and "Always", "Default" should now |
955 // be treated as "Never". | 1002 // be treated as "Never". |
956 // (http://code.google.com/p/chromium/issues/detail?id=146773) | 1003 // (http://code.google.com/p/chromium/issues/detail?id=146773) |
957 const std::string behavior_value( | |
958 profile_->GetPrefs()->GetString(prefs::kShelfAutoHideBehavior)); | |
959 ash::ShelfAutoHideBehavior behavior = | 1004 ash::ShelfAutoHideBehavior behavior = |
960 ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER; | 1005 ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER; |
961 if (behavior_value == ash::kShelfAutoHideBehaviorAlways) | 1006 if (behavior_value == ash::kShelfAutoHideBehaviorAlways) |
962 behavior = ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS; | 1007 behavior = ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS; |
963 // TODO(oshima): Support multiple displays. | 1008 // TODO(oshima): Support multiple displays. |
964 ash::Shell::GetInstance()->SetShelfAutoHideBehavior( | 1009 ash::Shell::GetInstance()->SetShelfAutoHideBehavior( |
965 behavior, ash::Shell::GetPrimaryRootWindow()); | 1010 behavior, ash::Shell::GetPrimaryRootWindow()); |
966 } | 1011 } |
967 | 1012 |
968 void ChromeLauncherController::SetShelfAlignmentFromPrefs() { | 1013 void ChromeLauncherController::SetShelfAlignmentFromPrefs() { |
969 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 1014 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
970 switches::kShowLauncherAlignmentMenu)) | 1015 switches::kShowLauncherAlignmentMenu)) |
971 return; | 1016 return; |
972 | 1017 |
| 1018 // See comment in |kShelfAlignment| as to why we consider two prefs. |
973 const std::string alignment_value( | 1019 const std::string alignment_value( |
974 profile_->GetPrefs()->GetString(prefs::kShelfAlignment)); | 1020 GetLocalOrRemotePref(profile_->GetPrefs(), |
| 1021 prefs::kShelfAlignmentLocal, |
| 1022 prefs::kShelfAlignment)); |
975 ash::ShelfAlignment alignment = ash::SHELF_ALIGNMENT_BOTTOM; | 1023 ash::ShelfAlignment alignment = ash::SHELF_ALIGNMENT_BOTTOM; |
976 if (alignment_value == ash::kShelfAlignmentLeft) | 1024 if (alignment_value == ash::kShelfAlignmentLeft) |
977 alignment = ash::SHELF_ALIGNMENT_LEFT; | 1025 alignment = ash::SHELF_ALIGNMENT_LEFT; |
978 else if (alignment_value == ash::kShelfAlignmentRight) | 1026 else if (alignment_value == ash::kShelfAlignmentRight) |
979 alignment = ash::SHELF_ALIGNMENT_RIGHT; | 1027 alignment = ash::SHELF_ALIGNMENT_RIGHT; |
980 // TODO(oshima): Support multiple displays. | 1028 // TODO(oshima): Support multiple displays. |
981 ash::Shell::GetInstance()->SetShelfAlignment( | 1029 ash::Shell::GetInstance()->SetShelfAlignment( |
982 alignment, ash::Shell::GetPrimaryRootWindow()); | 1030 alignment, ash::Shell::GetPrimaryRootWindow()); |
983 } | 1031 } |
984 | 1032 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1072 } | 1120 } |
1073 | 1121 |
1074 void ChromeLauncherController::StopLoadingAnimation() { | 1122 void ChromeLauncherController::StopLoadingAnimation() { |
1075 DCHECK(observed_sync_service_); | 1123 DCHECK(observed_sync_service_); |
1076 | 1124 |
1077 model_->SetStatus(ash::LauncherModel::STATUS_NORMAL); | 1125 model_->SetStatus(ash::LauncherModel::STATUS_NORMAL); |
1078 loading_timer_.Stop(); | 1126 loading_timer_.Stop(); |
1079 observed_sync_service_->RemoveObserver(this); | 1127 observed_sync_service_->RemoveObserver(this); |
1080 observed_sync_service_ = NULL; | 1128 observed_sync_service_ = NULL; |
1081 } | 1129 } |
OLD | NEW |