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

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

Issue 2684853002: Discard pinning an app with non-empty launcher id. (Closed)
Patch Set: few nits Created 3 years, 10 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 <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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 // If no user-set value exists at |local_path|, the value from |synced_path| is 219 // If no user-set value exists at |local_path|, the value from |synced_path| is
212 // copied to |local_path|. 220 // copied to |local_path|.
213 void PropagatePrefToLocalIfNotSet( 221 void PropagatePrefToLocalIfNotSet(
214 sync_preferences::PrefServiceSyncable* pref_service, 222 sync_preferences::PrefServiceSyncable* pref_service,
215 const char* local_path, 223 const char* local_path,
216 const char* synced_path) { 224 const char* synced_path) {
217 if (!pref_service->FindPreference(local_path)->HasUserSetting()) 225 if (!pref_service->FindPreference(local_path)->HasUserSetting())
218 pref_service->SetString(local_path, pref_service->GetString(synced_path)); 226 pref_service->SetString(local_path, pref_service->GetString(synced_path));
219 } 227 }
220 228
229 std::vector<AppLauncherId> AppIdsToAppLauncherIds(
230 const std::vector<std::string> app_ids) {
231 std::vector<AppLauncherId> app_launcher_ids(app_ids.size(), AppLauncherId());
232 for (size_t i = 0; i < app_ids.size(); ++i)
233 app_launcher_ids[i] = AppLauncherId(app_ids[i]);
234 return app_launcher_ids;
235 }
236
221 struct PinInfo { 237 struct PinInfo {
222 PinInfo(const AppLauncherId& app_launcher_id, 238 PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal)
223 const syncer::StringOrdinal& item_ordinal) 239 : app_id(app_id), item_ordinal(item_ordinal) {}
224 : app_launcher_id(app_launcher_id), item_ordinal(item_ordinal) {}
225 240
226 AppLauncherId app_launcher_id; 241 std::string app_id;
227 syncer::StringOrdinal item_ordinal; 242 syncer::StringOrdinal item_ordinal;
228 }; 243 };
229 244
230 struct ComparePinInfo { 245 struct ComparePinInfo {
231 bool operator()(const PinInfo& pin1, const PinInfo& pin2) { 246 bool operator()(const PinInfo& pin1, const PinInfo& pin2) {
232 return pin1.item_ordinal.LessThan(pin2.item_ordinal); 247 return pin1.item_ordinal.LessThan(pin2.item_ordinal);
233 } 248 }
234 }; 249 };
235 250
236 // Helper class to keep apps in order of appearance and to provide fast way 251 // Helper class to keep apps in order of appearance and to provide fast way
237 // to check if app exists in the list. 252 // to check if app exists in the list.
238 class AppTracker { 253 class AppTracker {
239 public: 254 public:
240 bool HasApp(const AppLauncherId& app_launcher_id) const { 255 bool HasApp(const std::string& app_id) const {
241 return app_set_.find(app_launcher_id) != app_set_.end(); 256 return app_set_.find(app_id) != app_set_.end();
242 } 257 }
243 258
244 void AddApp(const AppLauncherId& app_launcher_id) { 259 void AddApp(const std::string& app_id) {
245 if (HasApp(app_launcher_id)) 260 if (HasApp(app_id))
246 return; 261 return;
247 app_list_.push_back(app_launcher_id); 262 app_list_.push_back(app_id);
248 app_set_.insert(app_launcher_id); 263 app_set_.insert(app_id);
249 } 264 }
250 265
251 void MaybeAddApp(const AppLauncherId& app_launcher_id, 266 void MaybeAddApp(const std::string& app_id,
252 const LauncherControllerHelper* helper, 267 const LauncherControllerHelper* helper,
253 bool check_for_valid_app) { 268 bool check_for_valid_app) {
254 DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.ToString()); 269 DCHECK_NE(kPinnedAppsPlaceholder, app_id);
255 if (check_for_valid_app && 270 if (check_for_valid_app && !helper->IsValidIDForCurrentUser(app_id)) {
256 !helper->IsValidIDForCurrentUser(app_launcher_id.ToString())) {
257 return; 271 return;
258 } 272 }
259 AddApp(app_launcher_id); 273 AddApp(app_id);
260 } 274 }
261 275
262 void MaybeAddAppFromPref(const base::DictionaryValue* app_pref, 276 void MaybeAddAppFromPref(const base::DictionaryValue* app_pref,
263 const LauncherControllerHelper* helper, 277 const LauncherControllerHelper* helper,
264 bool check_for_valid_app) { 278 bool check_for_valid_app) {
265 std::string app_id; 279 std::string app_id;
266 if (!app_pref->GetString(kPinnedAppsPrefAppIDPath, &app_id)) { 280 if (!app_pref->GetString(kPinnedAppsPrefAppIDPath, &app_id)) {
267 LOG(ERROR) << "Cannot get app id from app pref entry."; 281 LOG(ERROR) << "Cannot get app id from app pref entry.";
268 return; 282 return;
269 } 283 }
270 284
271 if (app_id == kPinnedAppsPlaceholder) 285 if (app_id == kPinnedAppsPlaceholder)
272 return; 286 return;
273 287
274 bool pinned_by_policy = false; 288 bool pinned_by_policy = false;
275 if (app_pref->GetBoolean(kPinnedAppsPrefPinnedByPolicy, 289 if (app_pref->GetBoolean(kPinnedAppsPrefPinnedByPolicy,
276 &pinned_by_policy) && 290 &pinned_by_policy) &&
277 pinned_by_policy) { 291 pinned_by_policy) {
278 return; 292 return;
279 } 293 }
280 294
281 MaybeAddApp(AppLauncherId(app_id), helper, check_for_valid_app); 295 MaybeAddApp(app_id, helper, check_for_valid_app);
282 } 296 }
283 297
284 const std::vector<AppLauncherId>& app_list() const { return app_list_; } 298 const std::vector<std::string>& app_list() const { return app_list_; }
285 299
286 private: 300 private:
287 std::vector<AppLauncherId> app_list_; 301 std::vector<std::string> app_list_;
288 std::set<AppLauncherId> app_set_; 302 std::set<std::string> app_set_;
289 }; 303 };
290 304
291 } // namespace 305 } // namespace
292 306
293 const char kPinnedAppsPrefAppIDPath[] = "id"; 307 const char kPinnedAppsPrefAppIDPath[] = "id";
294 const char kPinnedAppsPrefPinnedByPolicy[] = "pinned_by_policy"; 308 const char kPinnedAppsPrefPinnedByPolicy[] = "pinned_by_policy";
295 const char kPinnedAppsPlaceholder[] = "AppShelfIDPlaceholder--------"; 309 const char kPinnedAppsPlaceholder[] = "AppShelfIDPlaceholder--------";
296 310
297 const char kShelfAutoHideBehaviorAlways[] = "Always"; 311 const char kShelfAutoHideBehaviorAlways[] = "Always";
298 const char kShelfAutoHideBehaviorNever[] = "Never"; 312 const char kShelfAutoHideBehaviorNever[] = "Never";
299 313
300 const char kShelfAlignmentBottom[] = "Bottom"; 314 const char kShelfAlignmentBottom[] = "Bottom";
301 const char kShelfAlignmentLeft[] = "Left"; 315 const char kShelfAlignmentLeft[] = "Left";
302 const char kShelfAlignmentRight[] = "Right"; 316 const char kShelfAlignmentRight[] = "Right";
303 317
304 AppLauncherId::AppLauncherId(const std::string& app_id,
305 const std::string& launch_id)
306 : app_id_(app_id), launch_id_(launch_id) {
307 DCHECK(!app_id.empty());
308 }
309
310 AppLauncherId::AppLauncherId(const std::string& app_id) : app_id_(app_id) {
311 DCHECK(!app_id.empty());
312 }
313
314 AppLauncherId::AppLauncherId() {}
315
316 AppLauncherId::~AppLauncherId() {}
317
318 std::string AppLauncherId::ToString() const {
319 return app_id_ + launch_id_;
320 }
321
322 bool AppLauncherId::operator<(const AppLauncherId& other) const {
323 return ToString() < other.ToString();
324 }
325
326 void RegisterChromeLauncherUserPrefs( 318 void RegisterChromeLauncherUserPrefs(
327 user_prefs::PrefRegistrySyncable* registry) { 319 user_prefs::PrefRegistrySyncable* registry) {
328 // TODO: If we want to support multiple profiles this will likely need to be 320 // TODO: If we want to support multiple profiles this will likely need to be
329 // pushed to local state and we'll need to track profile per item. 321 // pushed to local state and we'll need to track profile per item.
330 registry->RegisterIntegerPref( 322 registry->RegisterIntegerPref(
331 prefs::kShelfChromeIconIndex, 323 prefs::kShelfChromeIconIndex,
332 0, 324 0,
333 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 325 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
334 registry->RegisterListPref(prefs::kPinnedLauncherApps, 326 registry->RegisterListPref(prefs::kPinnedLauncherApps,
335 CreateDefaultPinnedAppsList(), 327 CreateDefaultPinnedAppsList(),
336 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 328 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
337 registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps); 329 registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps);
338 registry->RegisterStringPref(prefs::kShelfAutoHideBehavior, 330 registry->RegisterStringPref(prefs::kShelfAutoHideBehavior,
339 kShelfAutoHideBehaviorNever, 331 kShelfAutoHideBehaviorNever,
340 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 332 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
341 registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, 333 registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal,
342 std::string()); 334 std::string());
343 registry->RegisterStringPref(prefs::kShelfAlignment, 335 registry->RegisterStringPref(prefs::kShelfAlignment,
344 kShelfAlignmentBottom, 336 kShelfAlignmentBottom,
345 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 337 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
346 registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string()); 338 registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string());
347 registry->RegisterDictionaryPref(prefs::kShelfPreferences); 339 registry->RegisterDictionaryPref(prefs::kShelfPreferences);
348 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); 340 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000);
349 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); 341 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false);
350 } 342 }
351 343
352 std::unique_ptr<base::DictionaryValue> CreateAppDict(
353 const AppLauncherId& app_launcher_id) {
354 auto app_value = base::MakeUnique<base::DictionaryValue>();
355 app_value->SetString(kPinnedAppsPrefAppIDPath, app_launcher_id.ToString());
356 return app_value;
357 }
358
359 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs, 344 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs,
360 int64_t display_id) { 345 int64_t display_id) {
361 DCHECK_NE(display_id, display::kInvalidDisplayId); 346 DCHECK_NE(display_id, display::kInvalidDisplayId);
362 347
363 // Don't show the shelf in app mode. 348 // Don't show the shelf in app mode.
364 if (chrome::IsRunningInAppMode()) 349 if (chrome::IsRunningInAppMode())
365 return SHELF_AUTO_HIDE_ALWAYS_HIDDEN; 350 return SHELF_AUTO_HIDE_ALWAYS_HIDDEN;
366 351
367 // See comment in |kShelfAlignment| as to why we consider two prefs. 352 // See comment in |kShelfAlignment| as to why we consider two prefs.
368 return AutoHideBehaviorFromPref( 353 return AutoHideBehaviorFromPref(
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 if (!arc_app_list_pref) 428 if (!arc_app_list_pref)
444 continue; 429 continue;
445 430
446 // We are dealing with package name, not with 32 characters ID. 431 // We are dealing with package name, not with 32 characters ID.
447 const std::string& arc_package = app_id; 432 const std::string& arc_package = app_id;
448 const std::vector<std::string> activities = GetActivitiesForPackage( 433 const std::vector<std::string> activities = GetActivitiesForPackage(
449 arc_package, all_arc_app_ids, *arc_app_list_pref); 434 arc_package, all_arc_app_ids, *arc_app_list_pref);
450 for (const auto& activity : activities) { 435 for (const auto& activity : activities) {
451 const std::string arc_app_id = 436 const std::string arc_app_id =
452 ArcAppListPrefs::GetAppId(arc_package, activity); 437 ArcAppListPrefs::GetAppId(arc_package, activity);
453 apps->MaybeAddApp(AppLauncherId(arc_app_id), helper, 438 apps->MaybeAddApp(arc_app_id, helper, check_for_valid_app);
454 check_for_valid_app);
455 } 439 }
456 } else { 440 } else {
457 apps->MaybeAddApp(AppLauncherId(app_id), helper, check_for_valid_app); 441 apps->MaybeAddApp(app_id, helper, check_for_valid_app);
458 } 442 }
459 } 443 }
460 } 444 }
461 445
462 std::vector<AppLauncherId> GetPinnedAppsFromPrefsLegacy( 446 std::vector<std::string> GetPinnedAppsFromPrefsLegacy(
463 const PrefService* prefs, 447 const PrefService* prefs,
464 const LauncherControllerHelper* helper, 448 const LauncherControllerHelper* helper,
465 bool check_for_valid_app) { 449 bool check_for_valid_app) {
466 // Adding the app list item to the list of items requires that the ID is not 450 // Adding the app list item to the list of items requires that the ID is not
467 // a valid and known ID for the extension system. The ID was constructed that 451 // a valid and known ID for the extension system. The ID was constructed that
468 // way - but just to make sure... 452 // way - but just to make sure...
469 DCHECK(!helper->IsValidIDForCurrentUser(kPinnedAppsPlaceholder)); 453 DCHECK(!helper->IsValidIDForCurrentUser(kPinnedAppsPlaceholder));
470 454
471 const auto* pinned_apps = prefs->GetList(prefs::kPinnedLauncherApps); 455 const auto* pinned_apps = prefs->GetList(prefs::kPinnedLauncherApps);
472 456
473 // Get the sanitized preference value for the index of the Chrome app icon. 457 // Get the sanitized preference value for the index of the Chrome app icon.
474 const size_t chrome_icon_index = std::max<size_t>( 458 const size_t chrome_icon_index = std::max<size_t>(
475 0, std::min<size_t>(pinned_apps->GetSize(), 459 0, std::min<size_t>(pinned_apps->GetSize(),
476 prefs->GetInteger(prefs::kShelfChromeIconIndex))); 460 prefs->GetInteger(prefs::kShelfChromeIconIndex)));
477 461
478 // Check if Chrome is in either of the the preferences lists. 462 // Check if Chrome is in either of the the preferences lists.
479 std::unique_ptr<base::Value> chrome_app( 463 std::unique_ptr<base::Value> chrome_app(
480 CreateAppDict(AppLauncherId(extension_misc::kChromeAppId))); 464 CreateAppDict(extension_misc::kChromeAppId));
481 465
482 AppTracker apps; 466 AppTracker apps;
483 GetAppsPinnedByPolicy(prefs, helper, check_for_valid_app, &apps); 467 GetAppsPinnedByPolicy(prefs, helper, check_for_valid_app, &apps);
484 468
485 std::string app_id; 469 std::string app_id;
486 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) { 470 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) {
487 // We need to position the chrome icon relative to its place in the pinned 471 // We need to position the chrome icon relative to its place in the pinned
488 // preference list - even if an item of that list isn't shown yet. 472 // preference list - even if an item of that list isn't shown yet.
489 if (i == chrome_icon_index) 473 if (i == chrome_icon_index)
490 apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); 474 apps.AddApp(extension_misc::kChromeAppId);
491 const base::DictionaryValue* app_pref = nullptr; 475 const base::DictionaryValue* app_pref = nullptr;
492 if (!pinned_apps->GetDictionary(i, &app_pref)) { 476 if (!pinned_apps->GetDictionary(i, &app_pref)) {
493 LOG(ERROR) << "There is no dictionary for app entry."; 477 LOG(ERROR) << "There is no dictionary for app entry.";
494 continue; 478 continue;
495 } 479 }
496 apps.MaybeAddAppFromPref(app_pref, helper, check_for_valid_app); 480 apps.MaybeAddAppFromPref(app_pref, helper, check_for_valid_app);
497 } 481 }
498 482
499 // If not added yet, the chrome item will be the last item in the list. 483 // If not added yet, the chrome item will be the last item in the list.
500 apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); 484 apps.AddApp(extension_misc::kChromeAppId);
501 return apps.app_list(); 485 return apps.app_list();
502 } 486 }
503 487
504 // static 488 // static
505 std::unique_ptr<ChromeLauncherPrefsObserver> 489 std::unique_ptr<ChromeLauncherPrefsObserver>
506 ChromeLauncherPrefsObserver::CreateIfNecessary(Profile* profile) { 490 ChromeLauncherPrefsObserver::CreateIfNecessary(Profile* profile) {
507 sync_preferences::PrefServiceSyncable* prefs = 491 sync_preferences::PrefServiceSyncable* prefs =
508 PrefServiceSyncableFromProfile(profile); 492 PrefServiceSyncableFromProfile(profile);
509 if (!prefs->FindPreference(prefs::kShelfAlignmentLocal)->HasUserSetting() || 493 if (!prefs->FindPreference(prefs::kShelfAlignmentLocal)->HasUserSetting() ||
510 !prefs->FindPreference(prefs::kShelfAutoHideBehaviorLocal) 494 !prefs->FindPreference(prefs::kShelfAutoHideBehaviorLocal)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 if (!position.IsValid() || 553 if (!position.IsValid() ||
570 sync_peer.second->item_pin_ordinal.GreaterThan(position)) { 554 sync_peer.second->item_pin_ordinal.GreaterThan(position)) {
571 position = sync_peer.second->item_pin_ordinal; 555 position = sync_peer.second->item_pin_ordinal;
572 } 556 }
573 } 557 }
574 558
575 return position.IsValid() ? position.CreateAfter() 559 return position.IsValid() ? position.CreateAfter()
576 : syncer::StringOrdinal::CreateInitialOrdinal(); 560 : syncer::StringOrdinal::CreateInitialOrdinal();
577 } 561 }
578 562
579 std::vector<AppLauncherId> ImportLegacyPinnedApps( 563 std::vector<std::string> ImportLegacyPinnedApps(
580 const PrefService* prefs, 564 const PrefService* prefs,
581 LauncherControllerHelper* helper) { 565 LauncherControllerHelper* helper) {
582 std::vector<AppLauncherId> legacy_pins_all = 566 const std::vector<std::string> legacy_pins_all =
583 GetPinnedAppsFromPrefsLegacy(prefs, helper, false); 567 GetPinnedAppsFromPrefsLegacy(prefs, helper, false);
584 DCHECK(!legacy_pins_all.empty()); 568 DCHECK(!legacy_pins_all.empty());
585 569
586 app_list::AppListSyncableService* app_service = 570 app_list::AppListSyncableService* app_service =
587 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); 571 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile());
588 572
589 std::vector<AppLauncherId> legacy_pins_valid; 573 std::vector<std::string> legacy_pins_valid;
590 syncer::StringOrdinal last_position = 574 syncer::StringOrdinal last_position =
591 syncer::StringOrdinal::CreateInitialOrdinal(); 575 syncer::StringOrdinal::CreateInitialOrdinal();
592 // Convert to sync item record. 576 // Convert to sync item record.
593 for (const auto& app_launcher_id : legacy_pins_all) { 577 for (const auto& app_id : legacy_pins_all) {
594 DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.ToString()); 578 DCHECK_NE(kPinnedAppsPlaceholder, app_id);
595 app_service->SetPinPosition(app_launcher_id.ToString(), last_position); 579 app_service->SetPinPosition(app_id, last_position);
596 last_position = last_position.CreateAfter(); 580 last_position = last_position.CreateAfter();
597 if (helper->IsValidIDForCurrentUser(app_launcher_id.ToString())) 581 if (helper->IsValidIDForCurrentUser(app_id))
598 legacy_pins_valid.push_back(app_launcher_id); 582 legacy_pins_valid.push_back(app_id);
599 } 583 }
600 584
601 // Now process default apps. 585 // Now process default apps.
602 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) { 586 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) {
603 const std::string& app_id = kDefaultPinnedApps[i]; 587 const std::string& app_id = kDefaultPinnedApps[i];
604 // Check if it is already imported. 588 // Check if it is already imported.
605 if (app_service->GetPinPosition(app_id).IsValid()) 589 if (app_service->GetPinPosition(app_id).IsValid())
606 continue; 590 continue;
607 // Check if it is present but not in legacy pin. 591 // Check if it is present but not in legacy pin.
608 if (helper->IsValidIDForCurrentUser(app_id)) 592 if (helper->IsValidIDForCurrentUser(app_id))
(...skipping 24 matching lines...) Expand all
633 if (!sync_peer.second->item_pin_ordinal.IsValid()) 617 if (!sync_peer.second->item_pin_ordinal.IsValid())
634 continue; 618 continue;
635 619
636 first_run = false; 620 first_run = false;
637 // Don't include apps that currently do not exist on device. 621 // Don't include apps that currently do not exist on device.
638 if (sync_peer.first != extension_misc::kChromeAppId && 622 if (sync_peer.first != extension_misc::kChromeAppId &&
639 !helper->IsValidIDForCurrentUser(sync_peer.first)) { 623 !helper->IsValidIDForCurrentUser(sync_peer.first)) {
640 continue; 624 continue;
641 } 625 }
642 626
643 pin_infos.push_back(PinInfo(AppLauncherId(sync_peer.first), 627 pin_infos.push_back(
644 sync_peer.second->item_pin_ordinal)); 628 PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal));
645 } 629 }
646 630
647 if (first_run) { 631 if (first_run) {
648 // Return default apps in case profile is not synced yet. 632 // Return default apps in case profile is not synced yet.
649 sync_preferences::PrefServiceSyncable* const pref_service_syncable = 633 sync_preferences::PrefServiceSyncable* const pref_service_syncable =
650 PrefServiceSyncableFromProfile(helper->profile()); 634 PrefServiceSyncableFromProfile(helper->profile());
651 if (!pref_service_syncable->IsSyncing()) 635 if (!pref_service_syncable->IsSyncing())
652 return GetPinnedAppsFromPrefsLegacy(prefs, helper, true); 636 return AppIdsToAppLauncherIds(
637 GetPinnedAppsFromPrefsLegacy(prefs, helper, true));
653 638
654 // We need to import legacy pins model and convert it to sync based 639 // We need to import legacy pins model and convert it to sync based
655 // model. 640 // model.
656 return ImportLegacyPinnedApps(prefs, helper); 641 return AppIdsToAppLauncherIds(ImportLegacyPinnedApps(prefs, helper));
657 } 642 }
658 643
659 // Sort pins according their ordinals. 644 // Sort pins according their ordinals.
660 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); 645 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo());
661 646
662 AppTracker policy_apps; 647 AppTracker policy_apps;
663 GetAppsPinnedByPolicy(prefs, helper, true, &policy_apps); 648 GetAppsPinnedByPolicy(prefs, helper, true, &policy_apps);
664 649
665 // Pinned by policy apps appear first, if they were not shown before. 650 // Pinned by policy apps appear first, if they were not shown before.
666 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); 651 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile());
667 std::vector<AppLauncherId>::const_reverse_iterator it; 652 std::vector<std::string>::const_reverse_iterator it;
668 for (it = policy_apps.app_list().rbegin(); 653 for (it = policy_apps.app_list().rbegin();
669 it != policy_apps.app_list().rend(); ++it) { 654 it != policy_apps.app_list().rend(); ++it) {
670 const std::string& app_launcher_id_str = (*it).ToString(); 655 const std::string& app_id = *it;
671 if (app_launcher_id_str == kPinnedAppsPlaceholder) 656 if (app_id == kPinnedAppsPlaceholder)
672 continue; 657 continue;
673 658
674 // Check if we already processed current app. 659 // Check if we already processed current app.
675 if (app_service->GetPinPosition(app_launcher_id_str).IsValid()) 660 if (app_service->GetPinPosition(app_id).IsValid())
676 continue; 661 continue;
677 662
678 // Now move it to the front. 663 // Now move it to the front.
679 pin_infos.insert(pin_infos.begin(), 664 pin_infos.insert(pin_infos.begin(), PinInfo(*it, front_position));
680 PinInfo(AppLauncherId((*it).app_id(), (*it).launch_id()), 665 app_service->SetPinPosition(app_id, front_position);
681 front_position));
682 app_service->SetPinPosition(app_launcher_id_str, front_position);
683 front_position = front_position.CreateBefore(); 666 front_position = front_position.CreateBefore();
684 } 667 }
685 668
686 // Now insert Chrome browser app if needed. 669 // Now insert Chrome browser app if needed.
687 if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) { 670 if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) {
688 pin_infos.insert( 671 pin_infos.insert(pin_infos.begin(),
689 pin_infos.begin(), 672 PinInfo(extension_misc::kChromeAppId, front_position));
690 PinInfo(AppLauncherId(extension_misc::kChromeAppId), front_position));
691 app_service->SetPinPosition(extension_misc::kChromeAppId, front_position); 673 app_service->SetPinPosition(extension_misc::kChromeAppId, front_position);
692 } 674 }
693 675
694 if (helper->IsValidIDForCurrentUser(ArcSupportHost::kHostAppId)) { 676 if (helper->IsValidIDForCurrentUser(ArcSupportHost::kHostAppId)) {
695 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { 677 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) {
696 const syncer::StringOrdinal arc_host_position = 678 const syncer::StringOrdinal arc_host_position =
697 GetLastPinPosition(helper->profile()); 679 GetLastPinPosition(helper->profile());
698 pin_infos.insert(pin_infos.begin(), 680 pin_infos.insert(pin_infos.begin(),
699 PinInfo(AppLauncherId(ArcSupportHost::kHostAppId), 681 PinInfo(ArcSupportHost::kHostAppId, arc_host_position));
700 arc_host_position));
701 app_service->SetPinPosition(ArcSupportHost::kHostAppId, 682 app_service->SetPinPosition(ArcSupportHost::kHostAppId,
702 arc_host_position); 683 arc_host_position);
703 } 684 }
704 } 685 }
705 686
706 // Convert to AppLauncherId array. 687 // Convert to AppLauncherId array.
707 std::vector<AppLauncherId> pins(pin_infos.size(), AppLauncherId()); 688 std::vector<std::string> pins(pin_infos.size());
708 for (size_t i = 0; i < pin_infos.size(); ++i) 689 for (size_t i = 0; i < pin_infos.size(); ++i)
709 pins[i] = pin_infos[i].app_launcher_id; 690 pins[i] = pin_infos[i].app_id;
710 691
711 return pins; 692 return AppIdsToAppLauncherIds(pins);
712 } 693 }
713 694
714 void RemovePinPosition(Profile* profile, const AppLauncherId& app_launcher_id) { 695 void RemovePinPosition(Profile* profile, const AppLauncherId& app_launcher_id) {
715 DCHECK(profile); 696 DCHECK(profile);
697
698 const std::string& app_id = app_launcher_id.app_id();
699 if (!app_launcher_id.launch_id().empty()) {
700 VLOG(2) << "Syncing remove pin for '" << app_id
701 << "' with non-empty launch id '" << app_launcher_id.launch_id()
702 << "' is not supported.";
703 return;
704 }
705 DCHECK(!app_id.empty());
706
716 app_list::AppListSyncableService* app_service = 707 app_list::AppListSyncableService* app_service =
717 app_list::AppListSyncableServiceFactory::GetForProfile(profile); 708 app_list::AppListSyncableServiceFactory::GetForProfile(profile);
718 app_service->SetPinPosition(app_launcher_id.ToString(), 709 app_service->SetPinPosition(app_id, syncer::StringOrdinal());
719 syncer::StringOrdinal());
720 } 710 }
721 711
722 void SetPinPosition(Profile* profile, 712 void SetPinPosition(Profile* profile,
723 const AppLauncherId& app_launcher_id, 713 const AppLauncherId& app_launcher_id,
724 const AppLauncherId& app_launcher_id_before, 714 const AppLauncherId& app_launcher_id_before,
725 const std::vector<AppLauncherId>& app_launcher_ids_after) { 715 const std::vector<AppLauncherId>& app_launcher_ids_after) {
726 DCHECK(profile); 716 DCHECK(profile);
727 const std::string app_launcher_id_str = app_launcher_id.ToString(); 717
728 const std::string app_launcher_id_before_str = 718 const std::string& app_id = app_launcher_id.app_id();
729 app_launcher_id_before.ToString(); 719 if (!app_launcher_id.launch_id().empty()) {
730 DCHECK_NE(app_launcher_id_str, app_launcher_id_before_str); 720 VLOG(2) << "Syncing set pin for '" << app_id
721 << "' with non-empty launch id '" << app_launcher_id.launch_id()
722 << "' is not supported.";
723 return;
724 }
725
726 const std::string& app_id_before = app_launcher_id_before.app_id();
727
728 DCHECK(!app_id.empty());
729 DCHECK_NE(app_id, app_id_before);
731 730
732 app_list::AppListSyncableService* app_service = 731 app_list::AppListSyncableService* app_service =
733 app_list::AppListSyncableServiceFactory::GetForProfile(profile); 732 app_list::AppListSyncableServiceFactory::GetForProfile(profile);
734 // Some unit tests may not have this service. 733 // Some unit tests may not have this service.
735 if (!app_service) 734 if (!app_service)
736 return; 735 return;
737 736
738 syncer::StringOrdinal position_before = 737 syncer::StringOrdinal position_before =
739 app_launcher_id_before_str.empty() 738 app_id_before.empty() ? syncer::StringOrdinal()
740 ? syncer::StringOrdinal() 739 : app_service->GetPinPosition(app_id_before);
741 : app_service->GetPinPosition(app_launcher_id_before_str);
742 syncer::StringOrdinal position_after; 740 syncer::StringOrdinal position_after;
743 for (const auto& app_launcher_id_after : app_launcher_ids_after) { 741 for (const auto& app_launcher_id_after : app_launcher_ids_after) {
744 const std::string app_launcher_id_after_str = 742 const std::string& app_id_after = app_launcher_id_after.app_id();
745 app_launcher_id_after.ToString(); 743 DCHECK_NE(app_id_after, app_id);
746 DCHECK_NE(app_launcher_id_after_str, app_launcher_id_str); 744 DCHECK_NE(app_id_after, app_id_before);
747 DCHECK_NE(app_launcher_id_after_str, app_launcher_id_before_str); 745 syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after);
748 syncer::StringOrdinal position =
749 app_service->GetPinPosition(app_launcher_id_after_str);
750 DCHECK(position.IsValid()); 746 DCHECK(position.IsValid());
751 if (!position.IsValid()) { 747 if (!position.IsValid()) {
752 LOG(ERROR) << "Sync pin position was not found for " 748 LOG(ERROR) << "Sync pin position was not found for " << app_id_after;
753 << app_launcher_id_after_str;
754 continue; 749 continue;
755 } 750 }
756 if (!position_before.IsValid() || !position.Equals(position_before)) { 751 if (!position_before.IsValid() || !position.Equals(position_before)) {
757 position_after = position; 752 position_after = position;
758 break; 753 break;
759 } 754 }
760 } 755 }
761 756
762 syncer::StringOrdinal pin_position; 757 syncer::StringOrdinal pin_position;
763 if (position_before.IsValid() && position_after.IsValid()) 758 if (position_before.IsValid() && position_after.IsValid())
764 pin_position = position_before.CreateBetween(position_after); 759 pin_position = position_before.CreateBetween(position_after);
765 else if (position_before.IsValid()) 760 else if (position_before.IsValid())
766 pin_position = position_before.CreateAfter(); 761 pin_position = position_before.CreateAfter();
767 else if (position_after.IsValid()) 762 else if (position_after.IsValid())
768 pin_position = position_after.CreateBefore(); 763 pin_position = position_after.CreateBefore();
769 else 764 else
770 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); 765 pin_position = syncer::StringOrdinal::CreateInitialOrdinal();
771 app_service->SetPinPosition(app_launcher_id_str, pin_position); 766 app_service->SetPinPosition(app_id, pin_position);
772 } 767 }
773 768
774 } // namespace launcher 769 } // namespace launcher
775 } // namespace ash 770 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698