| 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/extensions/extension_sorting.h" | 5 #include "chrome/browser/extensions/extension_sorting.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_scoped_prefs.h" | 7 #include "chrome/browser/extensions/extension_scoped_prefs.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 // A preference determining the page on which an app appears in the NTP. | 22 // A preference determining the page on which an app appears in the NTP. |
| 23 const char kPrefPageIndexDeprecated[] = "page_index"; | 23 const char kPrefPageIndexDeprecated[] = "page_index"; |
| 24 const char kPrefPageOrdinal[] = "page_ordinal"; | 24 const char kPrefPageOrdinal[] = "page_ordinal"; |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 ExtensionSorting::ExtensionSorting(ExtensionScopedPrefs* extension_scoped_prefs, | 28 ExtensionSorting::ExtensionSorting(ExtensionScopedPrefs* extension_scoped_prefs, |
| 29 PrefService* pref_service) | 29 PrefService* pref_service) |
| 30 : extension_scoped_prefs_(extension_scoped_prefs), | 30 : extension_scoped_prefs_(extension_scoped_prefs), |
| 31 pref_service_(pref_service) { | 31 pref_service_(pref_service), |
| 32 extension_service_(NULL) { |
| 32 } | 33 } |
| 33 | 34 |
| 34 ExtensionSorting::~ExtensionSorting() { | 35 ExtensionSorting::~ExtensionSorting() { |
| 35 } | 36 } |
| 36 | 37 |
| 38 void ExtensionSorting::SetExtensionService( |
| 39 ExtensionServiceInterface* extension_service) { |
| 40 extension_service_ = extension_service; |
| 41 } |
| 42 |
| 37 void ExtensionSorting::Initialize( | 43 void ExtensionSorting::Initialize( |
| 38 const ExtensionPrefs::ExtensionIdSet& extension_ids) { | 44 const ExtensionPrefs::ExtensionIdSet& extension_ids) { |
| 39 InitializePageOrdinalMap(extension_ids); | 45 InitializePageOrdinalMap(extension_ids); |
| 40 | 46 |
| 41 MigrateAppIndex(extension_ids); | 47 MigrateAppIndex(extension_ids); |
| 42 } | 48 } |
| 43 | 49 |
| 44 void ExtensionSorting::MigrateAppIndex( | 50 void ExtensionSorting::MigrateAppIndex( |
| 45 const ExtensionPrefs::ExtensionIdSet& extension_ids) { | 51 const ExtensionPrefs::ExtensionIdSet& extension_ids) { |
| 46 if (extension_ids.empty()) | 52 if (extension_ids.empty()) |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 AddOrdinalMapping(extension_id, page_ordinal, new_app_launch_ordinal); | 276 AddOrdinalMapping(extension_id, page_ordinal, new_app_launch_ordinal); |
| 271 | 277 |
| 272 Value* new_value = new_app_launch_ordinal.IsValid() ? | 278 Value* new_value = new_app_launch_ordinal.IsValid() ? |
| 273 Value::CreateStringValue(new_app_launch_ordinal.ToString()) : | 279 Value::CreateStringValue(new_app_launch_ordinal.ToString()) : |
| 274 NULL; | 280 NULL; |
| 275 | 281 |
| 276 extension_scoped_prefs_->UpdateExtensionPref( | 282 extension_scoped_prefs_->UpdateExtensionPref( |
| 277 extension_id, | 283 extension_id, |
| 278 kPrefAppLaunchOrdinal, | 284 kPrefAppLaunchOrdinal, |
| 279 new_value); | 285 new_value); |
| 286 SyncIfNeeded(extension_id); |
| 280 } | 287 } |
| 281 | 288 |
| 282 StringOrdinal ExtensionSorting::CreateFirstAppLaunchOrdinal( | 289 StringOrdinal ExtensionSorting::CreateFirstAppLaunchOrdinal( |
| 283 const StringOrdinal& page_ordinal) const { | 290 const StringOrdinal& page_ordinal) const { |
| 284 const StringOrdinal& min_ordinal = | 291 const StringOrdinal& min_ordinal = |
| 285 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, | 292 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, |
| 286 ExtensionSorting::MIN_ORDINAL); | 293 ExtensionSorting::MIN_ORDINAL); |
| 287 | 294 |
| 288 if (min_ordinal.IsValid()) | 295 if (min_ordinal.IsValid()) |
| 289 return min_ordinal.CreateBefore(); | 296 return min_ordinal.CreateBefore(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 const { | 344 const { |
| 338 std::string raw_data; | 345 std::string raw_data; |
| 339 // If the preference read fails then raw_data will still be unset and we will | 346 // If the preference read fails then raw_data will still be unset and we will |
| 340 // return an invalid StringOrdinal to signal that no page ordinal was found. | 347 // return an invalid StringOrdinal to signal that no page ordinal was found. |
| 341 extension_scoped_prefs_->ReadExtensionPrefString( | 348 extension_scoped_prefs_->ReadExtensionPrefString( |
| 342 extension_id, kPrefPageOrdinal, &raw_data); | 349 extension_id, kPrefPageOrdinal, &raw_data); |
| 343 return StringOrdinal(raw_data); | 350 return StringOrdinal(raw_data); |
| 344 } | 351 } |
| 345 | 352 |
| 346 void ExtensionSorting::SetPageOrdinal(const std::string& extension_id, | 353 void ExtensionSorting::SetPageOrdinal(const std::string& extension_id, |
| 347 const StringOrdinal& new_page_ordinal) { | 354 const StringOrdinal& new_page_ordinal) { |
| 348 // No work is required if the old and new values are the same. | 355 // No work is required if the old and new values are the same. |
| 349 if (new_page_ordinal.EqualOrBothInvalid(GetPageOrdinal(extension_id))) | 356 if (new_page_ordinal.EqualOrBothInvalid(GetPageOrdinal(extension_id))) |
| 350 return; | 357 return; |
| 351 | 358 |
| 352 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); | 359 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); |
| 353 RemoveOrdinalMapping( | 360 RemoveOrdinalMapping( |
| 354 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); | 361 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); |
| 355 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); | 362 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); |
| 356 | 363 |
| 357 Value* new_value = new_page_ordinal.IsValid() ? | 364 Value* new_value = new_page_ordinal.IsValid() ? |
| 358 Value::CreateStringValue(new_page_ordinal.ToString()) : | 365 Value::CreateStringValue(new_page_ordinal.ToString()) : |
| 359 NULL; | 366 NULL; |
| 360 | 367 |
| 361 extension_scoped_prefs_->UpdateExtensionPref( | 368 extension_scoped_prefs_->UpdateExtensionPref( |
| 362 extension_id, | 369 extension_id, |
| 363 kPrefPageOrdinal, | 370 kPrefPageOrdinal, |
| 364 new_value); | 371 new_value); |
| 372 SyncIfNeeded(extension_id); |
| 365 } | 373 } |
| 366 | 374 |
| 367 void ExtensionSorting::ClearOrdinals(const std::string& extension_id) { | 375 void ExtensionSorting::ClearOrdinals(const std::string& extension_id) { |
| 368 RemoveOrdinalMapping(extension_id, | 376 RemoveOrdinalMapping(extension_id, |
| 369 GetPageOrdinal(extension_id), | 377 GetPageOrdinal(extension_id), |
| 370 GetAppLaunchOrdinal(extension_id)); | 378 GetAppLaunchOrdinal(extension_id)); |
| 371 | 379 |
| 372 extension_scoped_prefs_->UpdateExtensionPref( | 380 extension_scoped_prefs_->UpdateExtensionPref( |
| 373 extension_id, kPrefPageOrdinal, NULL); | 381 extension_id, kPrefPageOrdinal, NULL); |
| 374 extension_scoped_prefs_->UpdateExtensionPref( | 382 extension_scoped_prefs_->UpdateExtensionPref( |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 | 491 |
| 484 for (AppLaunchOrdinalMap::iterator it = | 492 for (AppLaunchOrdinalMap::iterator it = |
| 485 page_map->second.find(app_launch_ordinal); | 493 page_map->second.find(app_launch_ordinal); |
| 486 it != page_map->second.end(); ++it) { | 494 it != page_map->second.end(); ++it) { |
| 487 if (it->second == extension_id) { | 495 if (it->second == extension_id) { |
| 488 page_map->second.erase(it); | 496 page_map->second.erase(it); |
| 489 break; | 497 break; |
| 490 } | 498 } |
| 491 } | 499 } |
| 492 } | 500 } |
| 501 |
| 502 void ExtensionSorting::SyncIfNeeded(const std::string& extension_id) { |
| 503 if (extension_service_) { |
| 504 const Extension* ext = |
| 505 extension_service_->GetInstalledExtension(extension_id); |
| 506 |
| 507 if (ext) { |
| 508 CHECK(ext->is_app()); |
| 509 extension_service_->SyncExtensionChangeIfNeeded(*ext); |
| 510 } |
| 511 } |
| 512 } |
| OLD | NEW |