| 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/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 <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/app_mode/app_mode_utils.h" | 15 #include "chrome/browser/app_mode/app_mode_utils.h" |
| 16 #include "chrome/browser/chromeos/arc/arc_support_host.h" | 16 #include "chrome/browser/chromeos/arc/arc_support_host.h" |
| 17 #include "chrome/browser/prefs/pref_service_syncable_util.h" | 17 #include "chrome/browser/prefs/pref_service_syncable_util.h" |
| 18 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" | 18 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" |
| 19 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h" | 19 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h" |
| 20 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" | 20 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| 21 #include "chrome/browser/ui/ash/app_launcher_id.h" |
| 21 #include "chrome/browser/ui/ash/launcher/launcher_controller_helper.h" | 22 #include "chrome/browser/ui/ash/launcher/launcher_controller_helper.h" |
| 22 #include "chrome/common/extensions/extension_constants.h" | 23 #include "chrome/common/extensions/extension_constants.h" |
| 23 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 24 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
| 25 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
| 26 #include "components/prefs/scoped_user_pref_update.h" | 27 #include "components/prefs/scoped_user_pref_update.h" |
| 27 #include "components/sync/model/string_ordinal.h" | 28 #include "components/sync/model/string_ordinal.h" |
| 28 #include "components/sync_preferences/pref_service_syncable.h" | 29 #include "components/sync_preferences/pref_service_syncable.h" |
| 29 #include "ui/display/display.h" | 30 #include "ui/display/display.h" |
| 30 #include "ui/display/screen.h" | 31 #include "ui/display/screen.h" |
| 31 #include "ui/display/types/display_constants.h" | 32 #include "ui/display/types/display_constants.h" |
| 32 | 33 |
| 33 namespace ash { | 34 namespace ash { |
| 34 namespace launcher { | 35 namespace launcher { |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 39 std::unique_ptr<base::DictionaryValue> CreateAppDict( |
| 40 const std::string& app_id) { |
| 41 auto app_value = base::MakeUnique<base::DictionaryValue>(); |
| 42 app_value->SetString(kPinnedAppsPrefAppIDPath, app_id); |
| 43 return app_value; |
| 44 } |
| 45 |
| 38 // App ID of default pinned apps. | 46 // App ID of default pinned apps. |
| 39 const char* kDefaultPinnedApps[] = { | 47 const char* kDefaultPinnedApps[] = { |
| 40 extension_misc::kGmailAppId, extension_misc::kGoogleDocAppId, | 48 extension_misc::kGmailAppId, extension_misc::kGoogleDocAppId, |
| 41 extension_misc::kYoutubeAppId, ArcSupportHost::kHostAppId}; | 49 extension_misc::kYoutubeAppId, ArcSupportHost::kHostAppId}; |
| 42 | 50 |
| 43 base::ListValue* CreateDefaultPinnedAppsList() { | 51 base::ListValue* CreateDefaultPinnedAppsList() { |
| 44 std::unique_ptr<base::ListValue> apps(new base::ListValue); | 52 std::unique_ptr<base::ListValue> apps(new base::ListValue); |
| 45 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) | 53 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) |
| 46 apps->Append(CreateAppDict(AppLauncherId(kDefaultPinnedApps[i]))); | 54 apps->Append(CreateAppDict(kDefaultPinnedApps[i])); |
| 47 | 55 |
| 48 return apps.release(); | 56 return apps.release(); |
| 49 } | 57 } |
| 50 | 58 |
| 51 // Returns the preference value for the display with the given |display_id|. | 59 // Returns the preference value for the display with the given |display_id|. |
| 52 // The pref value is stored in |local_path| and |path|, but the pref service may | 60 // The pref value is stored in |local_path| and |path|, but the pref service may |
| 53 // have per-display preferences and the value can be specified by policy. | 61 // have per-display preferences and the value can be specified by policy. |
| 54 // Here is the priority: | 62 // Here is the priority: |
| 55 // * A value managed by policy. This is a single value that applies to all | 63 // * A value managed by policy. This is a single value that applies to all |
| 56 // displays. | 64 // displays. |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 // If no user-set value exists at |local_path|, the value from |synced_path| is | 207 // If no user-set value exists at |local_path|, the value from |synced_path| is |
| 200 // copied to |local_path|. | 208 // copied to |local_path|. |
| 201 void PropagatePrefToLocalIfNotSet( | 209 void PropagatePrefToLocalIfNotSet( |
| 202 sync_preferences::PrefServiceSyncable* pref_service, | 210 sync_preferences::PrefServiceSyncable* pref_service, |
| 203 const char* local_path, | 211 const char* local_path, |
| 204 const char* synced_path) { | 212 const char* synced_path) { |
| 205 if (!pref_service->FindPreference(local_path)->HasUserSetting()) | 213 if (!pref_service->FindPreference(local_path)->HasUserSetting()) |
| 206 pref_service->SetString(local_path, pref_service->GetString(synced_path)); | 214 pref_service->SetString(local_path, pref_service->GetString(synced_path)); |
| 207 } | 215 } |
| 208 | 216 |
| 217 std::vector<AppLauncherId> AppIdsToAppLauncherIds( |
| 218 const std::vector<std::string> app_ids) { |
| 219 std::vector<AppLauncherId> app_launcher_ids(app_ids.size(), AppLauncherId()); |
| 220 for (size_t i = 0; i < app_ids.size(); ++i) |
| 221 app_launcher_ids[i] = AppLauncherId(app_ids[i]); |
| 222 return app_launcher_ids; |
| 223 } |
| 224 |
| 209 struct PinInfo { | 225 struct PinInfo { |
| 210 PinInfo(const AppLauncherId& app_launcher_id, | 226 PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal) |
| 211 const syncer::StringOrdinal& item_ordinal) | 227 : app_id(app_id), item_ordinal(item_ordinal) {} |
| 212 : app_launcher_id(app_launcher_id), item_ordinal(item_ordinal) {} | |
| 213 | 228 |
| 214 AppLauncherId app_launcher_id; | 229 std::string app_id; |
| 215 syncer::StringOrdinal item_ordinal; | 230 syncer::StringOrdinal item_ordinal; |
| 216 }; | 231 }; |
| 217 | 232 |
| 218 struct ComparePinInfo { | 233 struct ComparePinInfo { |
| 219 bool operator()(const PinInfo& pin1, const PinInfo& pin2) { | 234 bool operator()(const PinInfo& pin1, const PinInfo& pin2) { |
| 220 return pin1.item_ordinal.LessThan(pin2.item_ordinal); | 235 return pin1.item_ordinal.LessThan(pin2.item_ordinal); |
| 221 } | 236 } |
| 222 }; | 237 }; |
| 223 | 238 |
| 224 // Helper class to keep apps in order of appearance and to provide fast way | 239 // Helper class to keep apps in order of appearance and to provide fast way |
| 225 // to check if app exists in the list. | 240 // to check if app exists in the list. |
| 226 class AppTracker { | 241 class AppTracker { |
| 227 public: | 242 public: |
| 228 bool HasApp(const AppLauncherId& app_launcher_id) const { | 243 bool HasApp(const std::string& app_id) const { |
| 229 return app_set_.find(app_launcher_id) != app_set_.end(); | 244 return app_set_.find(app_id) != app_set_.end(); |
| 230 } | 245 } |
| 231 | 246 |
| 232 void AddApp(const AppLauncherId& app_launcher_id) { | 247 void AddApp(const std::string& app_id) { |
| 233 if (HasApp(app_launcher_id)) | 248 if (HasApp(app_id)) |
| 234 return; | 249 return; |
| 235 app_list_.push_back(app_launcher_id); | 250 app_list_.push_back(app_id); |
| 236 app_set_.insert(app_launcher_id); | 251 app_set_.insert(app_id); |
| 237 } | 252 } |
| 238 | 253 |
| 239 void MaybeAddApp(const AppLauncherId& app_launcher_id, | 254 void MaybeAddApp(const std::string& app_id, |
| 240 const LauncherControllerHelper* helper, | 255 const LauncherControllerHelper* helper, |
| 241 bool check_for_valid_app) { | 256 bool check_for_valid_app) { |
| 242 DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.ToString()); | 257 DCHECK_NE(kPinnedAppsPlaceholder, app_id); |
| 243 if (check_for_valid_app && | 258 if (check_for_valid_app && !helper->IsValidIDForCurrentUser(app_id)) { |
| 244 !helper->IsValidIDForCurrentUser(app_launcher_id.ToString())) { | |
| 245 return; | 259 return; |
| 246 } | 260 } |
| 247 AddApp(app_launcher_id); | 261 AddApp(app_id); |
| 248 } | 262 } |
| 249 | 263 |
| 250 void MaybeAddAppFromPref(const base::DictionaryValue* app_pref, | 264 void MaybeAddAppFromPref(const base::DictionaryValue* app_pref, |
| 251 const LauncherControllerHelper* helper, | 265 const LauncherControllerHelper* helper, |
| 252 bool check_for_valid_app) { | 266 bool check_for_valid_app) { |
| 253 std::string app_id; | 267 std::string app_id; |
| 254 if (!app_pref->GetString(kPinnedAppsPrefAppIDPath, &app_id)) { | 268 if (!app_pref->GetString(kPinnedAppsPrefAppIDPath, &app_id)) { |
| 255 LOG(ERROR) << "Cannot get app id from app pref entry."; | 269 LOG(ERROR) << "Cannot get app id from app pref entry."; |
| 256 return; | 270 return; |
| 257 } | 271 } |
| 258 | 272 |
| 259 if (app_id == kPinnedAppsPlaceholder) | 273 if (app_id == kPinnedAppsPlaceholder) |
| 260 return; | 274 return; |
| 261 | 275 |
| 262 bool pinned_by_policy = false; | 276 bool pinned_by_policy = false; |
| 263 if (app_pref->GetBoolean(kPinnedAppsPrefPinnedByPolicy, | 277 if (app_pref->GetBoolean(kPinnedAppsPrefPinnedByPolicy, |
| 264 &pinned_by_policy) && | 278 &pinned_by_policy) && |
| 265 pinned_by_policy) { | 279 pinned_by_policy) { |
| 266 return; | 280 return; |
| 267 } | 281 } |
| 268 | 282 |
| 269 MaybeAddApp(AppLauncherId(app_id), helper, check_for_valid_app); | 283 MaybeAddApp(app_id, helper, check_for_valid_app); |
| 270 } | 284 } |
| 271 | 285 |
| 272 const std::vector<AppLauncherId>& app_list() const { return app_list_; } | 286 const std::vector<std::string>& app_list() const { return app_list_; } |
| 273 | 287 |
| 274 private: | 288 private: |
| 275 std::vector<AppLauncherId> app_list_; | 289 std::vector<std::string> app_list_; |
| 276 std::set<AppLauncherId> app_set_; | 290 std::set<std::string> app_set_; |
| 277 }; | 291 }; |
| 278 | 292 |
| 279 } // namespace | 293 } // namespace |
| 280 | 294 |
| 281 const char kPinnedAppsPrefAppIDPath[] = "id"; | 295 const char kPinnedAppsPrefAppIDPath[] = "id"; |
| 282 const char kPinnedAppsPrefPinnedByPolicy[] = "pinned_by_policy"; | 296 const char kPinnedAppsPrefPinnedByPolicy[] = "pinned_by_policy"; |
| 283 const char kPinnedAppsPlaceholder[] = "AppShelfIDPlaceholder--------"; | 297 const char kPinnedAppsPlaceholder[] = "AppShelfIDPlaceholder--------"; |
| 284 | 298 |
| 285 const char kShelfAutoHideBehaviorAlways[] = "Always"; | 299 const char kShelfAutoHideBehaviorAlways[] = "Always"; |
| 286 const char kShelfAutoHideBehaviorNever[] = "Never"; | 300 const char kShelfAutoHideBehaviorNever[] = "Never"; |
| 287 | 301 |
| 288 const char kShelfAlignmentBottom[] = "Bottom"; | 302 const char kShelfAlignmentBottom[] = "Bottom"; |
| 289 const char kShelfAlignmentLeft[] = "Left"; | 303 const char kShelfAlignmentLeft[] = "Left"; |
| 290 const char kShelfAlignmentRight[] = "Right"; | 304 const char kShelfAlignmentRight[] = "Right"; |
| 291 | 305 |
| 292 AppLauncherId::AppLauncherId(const std::string& app_id, | |
| 293 const std::string& launch_id) | |
| 294 : app_id_(app_id), launch_id_(launch_id) { | |
| 295 DCHECK(!app_id.empty()); | |
| 296 } | |
| 297 | |
| 298 AppLauncherId::AppLauncherId(const std::string& app_id) : app_id_(app_id) { | |
| 299 DCHECK(!app_id.empty()); | |
| 300 } | |
| 301 | |
| 302 AppLauncherId::AppLauncherId() {} | |
| 303 | |
| 304 AppLauncherId::~AppLauncherId() {} | |
| 305 | |
| 306 std::string AppLauncherId::ToString() const { | |
| 307 return app_id_ + launch_id_; | |
| 308 } | |
| 309 | |
| 310 bool AppLauncherId::operator<(const AppLauncherId& other) const { | |
| 311 return ToString() < other.ToString(); | |
| 312 } | |
| 313 | |
| 314 void RegisterChromeLauncherUserPrefs( | 306 void RegisterChromeLauncherUserPrefs( |
| 315 user_prefs::PrefRegistrySyncable* registry) { | 307 user_prefs::PrefRegistrySyncable* registry) { |
| 316 // TODO: If we want to support multiple profiles this will likely need to be | 308 // TODO: If we want to support multiple profiles this will likely need to be |
| 317 // pushed to local state and we'll need to track profile per item. | 309 // pushed to local state and we'll need to track profile per item. |
| 318 registry->RegisterIntegerPref( | 310 registry->RegisterIntegerPref( |
| 319 prefs::kShelfChromeIconIndex, | 311 prefs::kShelfChromeIconIndex, |
| 320 0, | 312 0, |
| 321 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 313 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 322 registry->RegisterListPref(prefs::kPinnedLauncherApps, | 314 registry->RegisterListPref(prefs::kPinnedLauncherApps, |
| 323 CreateDefaultPinnedAppsList(), | 315 CreateDefaultPinnedAppsList(), |
| 324 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 316 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 325 registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps); | 317 registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps); |
| 326 registry->RegisterStringPref(prefs::kShelfAutoHideBehavior, | 318 registry->RegisterStringPref(prefs::kShelfAutoHideBehavior, |
| 327 kShelfAutoHideBehaviorNever, | 319 kShelfAutoHideBehaviorNever, |
| 328 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 320 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 329 registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, | 321 registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, |
| 330 std::string()); | 322 std::string()); |
| 331 registry->RegisterStringPref(prefs::kShelfAlignment, | 323 registry->RegisterStringPref(prefs::kShelfAlignment, |
| 332 kShelfAlignmentBottom, | 324 kShelfAlignmentBottom, |
| 333 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 325 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 334 registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string()); | 326 registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string()); |
| 335 registry->RegisterDictionaryPref(prefs::kShelfPreferences); | 327 registry->RegisterDictionaryPref(prefs::kShelfPreferences); |
| 336 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); | 328 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); |
| 337 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); | 329 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); |
| 338 } | 330 } |
| 339 | 331 |
| 340 std::unique_ptr<base::DictionaryValue> CreateAppDict( | |
| 341 const AppLauncherId& app_launcher_id) { | |
| 342 auto app_value = base::MakeUnique<base::DictionaryValue>(); | |
| 343 app_value->SetString(kPinnedAppsPrefAppIDPath, app_launcher_id.ToString()); | |
| 344 return app_value; | |
| 345 } | |
| 346 | |
| 347 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs, | 332 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs, |
| 348 int64_t display_id) { | 333 int64_t display_id) { |
| 349 DCHECK_NE(display_id, display::kInvalidDisplayId); | 334 DCHECK_NE(display_id, display::kInvalidDisplayId); |
| 350 | 335 |
| 351 // Don't show the shelf in app mode. | 336 // Don't show the shelf in app mode. |
| 352 if (chrome::IsRunningInAppMode()) | 337 if (chrome::IsRunningInAppMode()) |
| 353 return SHELF_AUTO_HIDE_ALWAYS_HIDDEN; | 338 return SHELF_AUTO_HIDE_ALWAYS_HIDDEN; |
| 354 | 339 |
| 355 // See comment in |kShelfAlignment| as to why we consider two prefs. | 340 // See comment in |kShelfAlignment| as to why we consider two prefs. |
| 356 return AutoHideBehaviorFromPref( | 341 return AutoHideBehaviorFromPref( |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 if (!arc_app_list_pref) | 416 if (!arc_app_list_pref) |
| 432 continue; | 417 continue; |
| 433 | 418 |
| 434 // We are dealing with package name, not with 32 characters ID. | 419 // We are dealing with package name, not with 32 characters ID. |
| 435 const std::string& arc_package = app_id; | 420 const std::string& arc_package = app_id; |
| 436 const std::vector<std::string> activities = GetActivitiesForPackage( | 421 const std::vector<std::string> activities = GetActivitiesForPackage( |
| 437 arc_package, all_arc_app_ids, *arc_app_list_pref); | 422 arc_package, all_arc_app_ids, *arc_app_list_pref); |
| 438 for (const auto& activity : activities) { | 423 for (const auto& activity : activities) { |
| 439 const std::string arc_app_id = | 424 const std::string arc_app_id = |
| 440 ArcAppListPrefs::GetAppId(arc_package, activity); | 425 ArcAppListPrefs::GetAppId(arc_package, activity); |
| 441 apps->MaybeAddApp(AppLauncherId(arc_app_id), helper, | 426 apps->MaybeAddApp(arc_app_id, helper, check_for_valid_app); |
| 442 check_for_valid_app); | |
| 443 } | 427 } |
| 444 } else { | 428 } else { |
| 445 apps->MaybeAddApp(AppLauncherId(app_id), helper, check_for_valid_app); | 429 apps->MaybeAddApp(app_id, helper, check_for_valid_app); |
| 446 } | 430 } |
| 447 } | 431 } |
| 448 } | 432 } |
| 449 | 433 |
| 450 std::vector<AppLauncherId> GetPinnedAppsFromPrefsLegacy( | 434 std::vector<std::string> GetPinnedAppsFromPrefsLegacy( |
| 451 const PrefService* prefs, | 435 const PrefService* prefs, |
| 452 const LauncherControllerHelper* helper, | 436 const LauncherControllerHelper* helper, |
| 453 bool check_for_valid_app) { | 437 bool check_for_valid_app) { |
| 454 // Adding the app list item to the list of items requires that the ID is not | 438 // Adding the app list item to the list of items requires that the ID is not |
| 455 // a valid and known ID for the extension system. The ID was constructed that | 439 // a valid and known ID for the extension system. The ID was constructed that |
| 456 // way - but just to make sure... | 440 // way - but just to make sure... |
| 457 DCHECK(!helper->IsValidIDForCurrentUser(kPinnedAppsPlaceholder)); | 441 DCHECK(!helper->IsValidIDForCurrentUser(kPinnedAppsPlaceholder)); |
| 458 | 442 |
| 459 const auto* pinned_apps = prefs->GetList(prefs::kPinnedLauncherApps); | 443 const auto* pinned_apps = prefs->GetList(prefs::kPinnedLauncherApps); |
| 460 | 444 |
| 461 // Get the sanitized preference value for the index of the Chrome app icon. | 445 // Get the sanitized preference value for the index of the Chrome app icon. |
| 462 const size_t chrome_icon_index = std::max<size_t>( | 446 const size_t chrome_icon_index = std::max<size_t>( |
| 463 0, std::min<size_t>(pinned_apps->GetSize(), | 447 0, std::min<size_t>(pinned_apps->GetSize(), |
| 464 prefs->GetInteger(prefs::kShelfChromeIconIndex))); | 448 prefs->GetInteger(prefs::kShelfChromeIconIndex))); |
| 465 | 449 |
| 466 // Check if Chrome is in either of the the preferences lists. | 450 // Check if Chrome is in either of the the preferences lists. |
| 467 std::unique_ptr<base::Value> chrome_app( | 451 std::unique_ptr<base::Value> chrome_app( |
| 468 CreateAppDict(AppLauncherId(extension_misc::kChromeAppId))); | 452 CreateAppDict(extension_misc::kChromeAppId)); |
| 469 | 453 |
| 470 AppTracker apps; | 454 AppTracker apps; |
| 471 GetAppsPinnedByPolicy(prefs, helper, check_for_valid_app, &apps); | 455 GetAppsPinnedByPolicy(prefs, helper, check_for_valid_app, &apps); |
| 472 | 456 |
| 473 std::string app_id; | 457 std::string app_id; |
| 474 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) { | 458 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) { |
| 475 // We need to position the chrome icon relative to its place in the pinned | 459 // We need to position the chrome icon relative to its place in the pinned |
| 476 // preference list - even if an item of that list isn't shown yet. | 460 // preference list - even if an item of that list isn't shown yet. |
| 477 if (i == chrome_icon_index) | 461 if (i == chrome_icon_index) |
| 478 apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); | 462 apps.AddApp(extension_misc::kChromeAppId); |
| 479 const base::DictionaryValue* app_pref = nullptr; | 463 const base::DictionaryValue* app_pref = nullptr; |
| 480 if (!pinned_apps->GetDictionary(i, &app_pref)) { | 464 if (!pinned_apps->GetDictionary(i, &app_pref)) { |
| 481 LOG(ERROR) << "There is no dictionary for app entry."; | 465 LOG(ERROR) << "There is no dictionary for app entry."; |
| 482 continue; | 466 continue; |
| 483 } | 467 } |
| 484 apps.MaybeAddAppFromPref(app_pref, helper, check_for_valid_app); | 468 apps.MaybeAddAppFromPref(app_pref, helper, check_for_valid_app); |
| 485 } | 469 } |
| 486 | 470 |
| 487 // If not added yet, the chrome item will be the last item in the list. | 471 // If not added yet, the chrome item will be the last item in the list. |
| 488 apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); | 472 apps.AddApp(extension_misc::kChromeAppId); |
| 489 return apps.app_list(); | 473 return apps.app_list(); |
| 490 } | 474 } |
| 491 | 475 |
| 492 // static | 476 // static |
| 493 std::unique_ptr<ChromeLauncherPrefsObserver> | 477 std::unique_ptr<ChromeLauncherPrefsObserver> |
| 494 ChromeLauncherPrefsObserver::CreateIfNecessary(Profile* profile) { | 478 ChromeLauncherPrefsObserver::CreateIfNecessary(Profile* profile) { |
| 495 sync_preferences::PrefServiceSyncable* prefs = | 479 sync_preferences::PrefServiceSyncable* prefs = |
| 496 PrefServiceSyncableFromProfile(profile); | 480 PrefServiceSyncableFromProfile(profile); |
| 497 if (!prefs->FindPreference(prefs::kShelfAlignmentLocal)->HasUserSetting() || | 481 if (!prefs->FindPreference(prefs::kShelfAlignmentLocal)->HasUserSetting() || |
| 498 !prefs->FindPreference(prefs::kShelfAutoHideBehaviorLocal) | 482 !prefs->FindPreference(prefs::kShelfAutoHideBehaviorLocal) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 if (!position.IsValid() || | 541 if (!position.IsValid() || |
| 558 sync_peer.second->item_pin_ordinal.GreaterThan(position)) { | 542 sync_peer.second->item_pin_ordinal.GreaterThan(position)) { |
| 559 position = sync_peer.second->item_pin_ordinal; | 543 position = sync_peer.second->item_pin_ordinal; |
| 560 } | 544 } |
| 561 } | 545 } |
| 562 | 546 |
| 563 return position.IsValid() ? position.CreateAfter() | 547 return position.IsValid() ? position.CreateAfter() |
| 564 : syncer::StringOrdinal::CreateInitialOrdinal(); | 548 : syncer::StringOrdinal::CreateInitialOrdinal(); |
| 565 } | 549 } |
| 566 | 550 |
| 567 std::vector<AppLauncherId> ImportLegacyPinnedApps( | 551 std::vector<std::string> ImportLegacyPinnedApps( |
| 568 const PrefService* prefs, | 552 const PrefService* prefs, |
| 569 LauncherControllerHelper* helper) { | 553 LauncherControllerHelper* helper) { |
| 570 std::vector<AppLauncherId> legacy_pins_all = | 554 const std::vector<std::string> legacy_pins_all = |
| 571 GetPinnedAppsFromPrefsLegacy(prefs, helper, false); | 555 GetPinnedAppsFromPrefsLegacy(prefs, helper, false); |
| 572 DCHECK(!legacy_pins_all.empty()); | 556 DCHECK(!legacy_pins_all.empty()); |
| 573 | 557 |
| 574 app_list::AppListSyncableService* app_service = | 558 app_list::AppListSyncableService* app_service = |
| 575 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); | 559 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); |
| 576 | 560 |
| 577 std::vector<AppLauncherId> legacy_pins_valid; | 561 std::vector<std::string> legacy_pins_valid; |
| 578 syncer::StringOrdinal last_position = | 562 syncer::StringOrdinal last_position = |
| 579 syncer::StringOrdinal::CreateInitialOrdinal(); | 563 syncer::StringOrdinal::CreateInitialOrdinal(); |
| 580 // Convert to sync item record. | 564 // Convert to sync item record. |
| 581 for (const auto& app_launcher_id : legacy_pins_all) { | 565 for (const auto& app_id : legacy_pins_all) { |
| 582 DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.ToString()); | 566 DCHECK_NE(kPinnedAppsPlaceholder, app_id); |
| 583 app_service->SetPinPosition(app_launcher_id.ToString(), last_position); | 567 app_service->SetPinPosition(app_id, last_position); |
| 584 last_position = last_position.CreateAfter(); | 568 last_position = last_position.CreateAfter(); |
| 585 if (helper->IsValidIDForCurrentUser(app_launcher_id.ToString())) | 569 if (helper->IsValidIDForCurrentUser(app_id)) |
| 586 legacy_pins_valid.push_back(app_launcher_id); | 570 legacy_pins_valid.push_back(app_id); |
| 587 } | 571 } |
| 588 | 572 |
| 589 // Now process default apps. | 573 // Now process default apps. |
| 590 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) { | 574 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) { |
| 591 const std::string& app_id = kDefaultPinnedApps[i]; | 575 const std::string& app_id = kDefaultPinnedApps[i]; |
| 592 // Check if it is already imported. | 576 // Check if it is already imported. |
| 593 if (app_service->GetPinPosition(app_id).IsValid()) | 577 if (app_service->GetPinPosition(app_id).IsValid()) |
| 594 continue; | 578 continue; |
| 595 // Check if it is present but not in legacy pin. | 579 // Check if it is present but not in legacy pin. |
| 596 if (helper->IsValidIDForCurrentUser(app_id)) | 580 if (helper->IsValidIDForCurrentUser(app_id)) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 621 if (!sync_peer.second->item_pin_ordinal.IsValid()) | 605 if (!sync_peer.second->item_pin_ordinal.IsValid()) |
| 622 continue; | 606 continue; |
| 623 | 607 |
| 624 first_run = false; | 608 first_run = false; |
| 625 // Don't include apps that currently do not exist on device. | 609 // Don't include apps that currently do not exist on device. |
| 626 if (sync_peer.first != extension_misc::kChromeAppId && | 610 if (sync_peer.first != extension_misc::kChromeAppId && |
| 627 !helper->IsValidIDForCurrentUser(sync_peer.first)) { | 611 !helper->IsValidIDForCurrentUser(sync_peer.first)) { |
| 628 continue; | 612 continue; |
| 629 } | 613 } |
| 630 | 614 |
| 631 pin_infos.push_back(PinInfo(AppLauncherId(sync_peer.first), | 615 pin_infos.push_back( |
| 632 sync_peer.second->item_pin_ordinal)); | 616 PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal)); |
| 633 } | 617 } |
| 634 | 618 |
| 635 if (first_run) { | 619 if (first_run) { |
| 636 // Return default apps in case profile is not synced yet. | 620 // Return default apps in case profile is not synced yet. |
| 637 sync_preferences::PrefServiceSyncable* const pref_service_syncable = | 621 sync_preferences::PrefServiceSyncable* const pref_service_syncable = |
| 638 PrefServiceSyncableFromProfile(helper->profile()); | 622 PrefServiceSyncableFromProfile(helper->profile()); |
| 639 if (!pref_service_syncable->IsSyncing()) | 623 if (!pref_service_syncable->IsSyncing()) |
| 640 return GetPinnedAppsFromPrefsLegacy(prefs, helper, true); | 624 return AppIdsToAppLauncherIds( |
| 625 GetPinnedAppsFromPrefsLegacy(prefs, helper, true)); |
| 641 | 626 |
| 642 // We need to import legacy pins model and convert it to sync based | 627 // We need to import legacy pins model and convert it to sync based |
| 643 // model. | 628 // model. |
| 644 return ImportLegacyPinnedApps(prefs, helper); | 629 return AppIdsToAppLauncherIds(ImportLegacyPinnedApps(prefs, helper)); |
| 645 } | 630 } |
| 646 | 631 |
| 647 // Sort pins according their ordinals. | 632 // Sort pins according their ordinals. |
| 648 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); | 633 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); |
| 649 | 634 |
| 650 AppTracker policy_apps; | 635 AppTracker policy_apps; |
| 651 GetAppsPinnedByPolicy(prefs, helper, true, &policy_apps); | 636 GetAppsPinnedByPolicy(prefs, helper, true, &policy_apps); |
| 652 | 637 |
| 653 // Pinned by policy apps appear first, if they were not shown before. | 638 // Pinned by policy apps appear first, if they were not shown before. |
| 654 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); | 639 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); |
| 655 std::vector<AppLauncherId>::const_reverse_iterator it; | 640 std::vector<std::string>::const_reverse_iterator it; |
| 656 for (it = policy_apps.app_list().rbegin(); | 641 for (it = policy_apps.app_list().rbegin(); |
| 657 it != policy_apps.app_list().rend(); ++it) { | 642 it != policy_apps.app_list().rend(); ++it) { |
| 658 const std::string& app_launcher_id_str = (*it).ToString(); | 643 const std::string& app_id = *it; |
| 659 if (app_launcher_id_str == kPinnedAppsPlaceholder) | 644 if (app_id == kPinnedAppsPlaceholder) |
| 660 continue; | 645 continue; |
| 661 | 646 |
| 662 // Check if we already processed current app. | 647 // Check if we already processed current app. |
| 663 if (app_service->GetPinPosition(app_launcher_id_str).IsValid()) | 648 if (app_service->GetPinPosition(app_id).IsValid()) |
| 664 continue; | 649 continue; |
| 665 | 650 |
| 666 // Now move it to the front. | 651 // Now move it to the front. |
| 667 pin_infos.insert(pin_infos.begin(), | 652 pin_infos.insert(pin_infos.begin(), PinInfo(*it, front_position)); |
| 668 PinInfo(AppLauncherId((*it).app_id(), (*it).launch_id()), | 653 app_service->SetPinPosition(app_id, front_position); |
| 669 front_position)); | |
| 670 app_service->SetPinPosition(app_launcher_id_str, front_position); | |
| 671 front_position = front_position.CreateBefore(); | 654 front_position = front_position.CreateBefore(); |
| 672 } | 655 } |
| 673 | 656 |
| 674 // Now insert Chrome browser app if needed. | 657 // Now insert Chrome browser app if needed. |
| 675 if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) { | 658 if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) { |
| 676 pin_infos.insert( | 659 pin_infos.insert(pin_infos.begin(), |
| 677 pin_infos.begin(), | 660 PinInfo(extension_misc::kChromeAppId, front_position)); |
| 678 PinInfo(AppLauncherId(extension_misc::kChromeAppId), front_position)); | |
| 679 app_service->SetPinPosition(extension_misc::kChromeAppId, front_position); | 661 app_service->SetPinPosition(extension_misc::kChromeAppId, front_position); |
| 680 } | 662 } |
| 681 | 663 |
| 682 if (helper->IsValidIDForCurrentUser(ArcSupportHost::kHostAppId)) { | 664 if (helper->IsValidIDForCurrentUser(ArcSupportHost::kHostAppId)) { |
| 683 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { | 665 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { |
| 684 const syncer::StringOrdinal arc_host_position = | 666 const syncer::StringOrdinal arc_host_position = |
| 685 GetLastPinPosition(helper->profile()); | 667 GetLastPinPosition(helper->profile()); |
| 686 pin_infos.insert(pin_infos.begin(), | 668 pin_infos.insert(pin_infos.begin(), |
| 687 PinInfo(AppLauncherId(ArcSupportHost::kHostAppId), | 669 PinInfo(ArcSupportHost::kHostAppId, arc_host_position)); |
| 688 arc_host_position)); | |
| 689 app_service->SetPinPosition(ArcSupportHost::kHostAppId, | 670 app_service->SetPinPosition(ArcSupportHost::kHostAppId, |
| 690 arc_host_position); | 671 arc_host_position); |
| 691 } | 672 } |
| 692 } | 673 } |
| 693 | 674 |
| 694 // Convert to AppLauncherId array. | 675 // Convert to AppLauncherId array. |
| 695 std::vector<AppLauncherId> pins(pin_infos.size(), AppLauncherId()); | 676 std::vector<std::string> pins(pin_infos.size()); |
| 696 for (size_t i = 0; i < pin_infos.size(); ++i) | 677 for (size_t i = 0; i < pin_infos.size(); ++i) |
| 697 pins[i] = pin_infos[i].app_launcher_id; | 678 pins[i] = pin_infos[i].app_id; |
| 698 | 679 |
| 699 return pins; | 680 return AppIdsToAppLauncherIds(pins); |
| 700 } | 681 } |
| 701 | 682 |
| 702 void RemovePinPosition(Profile* profile, const AppLauncherId& app_launcher_id) { | 683 void RemovePinPosition(Profile* profile, const AppLauncherId& app_launcher_id) { |
| 703 DCHECK(profile); | 684 DCHECK(profile); |
| 685 |
| 686 const std::string& app_id = app_launcher_id.app_id(); |
| 687 if (!app_launcher_id.launch_id().empty()) { |
| 688 VLOG(2) << "Syncing remove pin for '" << app_id |
| 689 << "' with non-empty launch id '" << app_launcher_id.launch_id() |
| 690 << "' is not supported."; |
| 691 return; |
| 692 } |
| 693 DCHECK(!app_id.empty()); |
| 694 |
| 704 app_list::AppListSyncableService* app_service = | 695 app_list::AppListSyncableService* app_service = |
| 705 app_list::AppListSyncableServiceFactory::GetForProfile(profile); | 696 app_list::AppListSyncableServiceFactory::GetForProfile(profile); |
| 706 app_service->SetPinPosition(app_launcher_id.ToString(), | 697 app_service->SetPinPosition(app_id, syncer::StringOrdinal()); |
| 707 syncer::StringOrdinal()); | |
| 708 } | 698 } |
| 709 | 699 |
| 710 void SetPinPosition(Profile* profile, | 700 void SetPinPosition(Profile* profile, |
| 711 const AppLauncherId& app_launcher_id, | 701 const AppLauncherId& app_launcher_id, |
| 712 const AppLauncherId& app_launcher_id_before, | 702 const AppLauncherId& app_launcher_id_before, |
| 713 const std::vector<AppLauncherId>& app_launcher_ids_after) { | 703 const std::vector<AppLauncherId>& app_launcher_ids_after) { |
| 714 DCHECK(profile); | 704 DCHECK(profile); |
| 715 const std::string app_launcher_id_str = app_launcher_id.ToString(); | 705 |
| 716 const std::string app_launcher_id_before_str = | 706 const std::string& app_id = app_launcher_id.app_id(); |
| 717 app_launcher_id_before.ToString(); | 707 if (!app_launcher_id.launch_id().empty()) { |
| 718 DCHECK_NE(app_launcher_id_str, app_launcher_id_before_str); | 708 VLOG(2) << "Syncing set pin for '" << app_id |
| 709 << "' with non-empty launch id '" << app_launcher_id.launch_id() |
| 710 << "' is not supported."; |
| 711 return; |
| 712 } |
| 713 |
| 714 const std::string& app_id_before = app_launcher_id_before.app_id(); |
| 715 |
| 716 DCHECK(!app_id.empty()); |
| 717 DCHECK_NE(app_id, app_id_before); |
| 719 | 718 |
| 720 app_list::AppListSyncableService* app_service = | 719 app_list::AppListSyncableService* app_service = |
| 721 app_list::AppListSyncableServiceFactory::GetForProfile(profile); | 720 app_list::AppListSyncableServiceFactory::GetForProfile(profile); |
| 722 // Some unit tests may not have this service. | 721 // Some unit tests may not have this service. |
| 723 if (!app_service) | 722 if (!app_service) |
| 724 return; | 723 return; |
| 725 | 724 |
| 726 syncer::StringOrdinal position_before = | 725 syncer::StringOrdinal position_before = |
| 727 app_launcher_id_before_str.empty() | 726 app_id_before.empty() ? syncer::StringOrdinal() |
| 728 ? syncer::StringOrdinal() | 727 : app_service->GetPinPosition(app_id_before); |
| 729 : app_service->GetPinPosition(app_launcher_id_before_str); | |
| 730 syncer::StringOrdinal position_after; | 728 syncer::StringOrdinal position_after; |
| 731 for (const auto& app_launcher_id_after : app_launcher_ids_after) { | 729 for (const auto& app_launcher_id_after : app_launcher_ids_after) { |
| 732 const std::string app_launcher_id_after_str = | 730 const std::string& app_id_after = app_launcher_id_after.app_id(); |
| 733 app_launcher_id_after.ToString(); | 731 DCHECK_NE(app_id_after, app_id); |
| 734 DCHECK_NE(app_launcher_id_after_str, app_launcher_id_str); | 732 DCHECK_NE(app_id_after, app_id_before); |
| 735 DCHECK_NE(app_launcher_id_after_str, app_launcher_id_before_str); | 733 syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after); |
| 736 syncer::StringOrdinal position = | |
| 737 app_service->GetPinPosition(app_launcher_id_after_str); | |
| 738 DCHECK(position.IsValid()); | 734 DCHECK(position.IsValid()); |
| 739 if (!position.IsValid()) { | 735 if (!position.IsValid()) { |
| 740 LOG(ERROR) << "Sync pin position was not found for " | 736 LOG(ERROR) << "Sync pin position was not found for " << app_id_after; |
| 741 << app_launcher_id_after_str; | |
| 742 continue; | 737 continue; |
| 743 } | 738 } |
| 744 if (!position_before.IsValid() || !position.Equals(position_before)) { | 739 if (!position_before.IsValid() || !position.Equals(position_before)) { |
| 745 position_after = position; | 740 position_after = position; |
| 746 break; | 741 break; |
| 747 } | 742 } |
| 748 } | 743 } |
| 749 | 744 |
| 750 syncer::StringOrdinal pin_position; | 745 syncer::StringOrdinal pin_position; |
| 751 if (position_before.IsValid() && position_after.IsValid()) | 746 if (position_before.IsValid() && position_after.IsValid()) |
| 752 pin_position = position_before.CreateBetween(position_after); | 747 pin_position = position_before.CreateBetween(position_after); |
| 753 else if (position_before.IsValid()) | 748 else if (position_before.IsValid()) |
| 754 pin_position = position_before.CreateAfter(); | 749 pin_position = position_before.CreateAfter(); |
| 755 else if (position_after.IsValid()) | 750 else if (position_after.IsValid()) |
| 756 pin_position = position_after.CreateBefore(); | 751 pin_position = position_after.CreateBefore(); |
| 757 else | 752 else |
| 758 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); | 753 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); |
| 759 app_service->SetPinPosition(app_launcher_id_str, pin_position); | 754 app_service->SetPinPosition(app_id, pin_position); |
| 760 } | 755 } |
| 761 | 756 |
| 762 } // namespace launcher | 757 } // namespace launcher |
| 763 } // namespace ash | 758 } // namespace ash |
| OLD | NEW |