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 ExtensionService* 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 | |
287 if (extension_service_) { | |
288 const Extension* ext = | |
289 extension_service_->GetInstalledExtension(extension_id); | |
290 DCHECK(!ext || ext->is_app()); | |
Aaron Boodman
2012/03/14 18:54:28
The extension system prefers CHECK to DCHECK.
csharp
2012/03/15 17:21:19
Done.
| |
291 | |
292 if (ext) | |
Aaron Boodman
2012/03/14 18:54:28
Move the DCHECK inside this branch and make it jus
csharp
2012/03/15 17:21:19
Done.
| |
293 extension_service_->SyncExtensionChangeIfNeeded(*ext); | |
294 } | |
280 } | 295 } |
281 | 296 |
282 StringOrdinal ExtensionSorting::CreateFirstAppLaunchOrdinal( | 297 StringOrdinal ExtensionSorting::CreateFirstAppLaunchOrdinal( |
283 const StringOrdinal& page_ordinal) const { | 298 const StringOrdinal& page_ordinal) const { |
284 const StringOrdinal& min_ordinal = | 299 const StringOrdinal& min_ordinal = |
285 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, | 300 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, |
286 ExtensionSorting::MIN_ORDINAL); | 301 ExtensionSorting::MIN_ORDINAL); |
287 | 302 |
288 if (min_ordinal.IsValid()) | 303 if (min_ordinal.IsValid()) |
289 return min_ordinal.CreateBefore(); | 304 return min_ordinal.CreateBefore(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
337 const { | 352 const { |
338 std::string raw_data; | 353 std::string raw_data; |
339 // If the preference read fails then raw_data will still be unset and we will | 354 // 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. | 355 // return an invalid StringOrdinal to signal that no page ordinal was found. |
341 extension_scoped_prefs_->ReadExtensionPrefString( | 356 extension_scoped_prefs_->ReadExtensionPrefString( |
342 extension_id, kPrefPageOrdinal, &raw_data); | 357 extension_id, kPrefPageOrdinal, &raw_data); |
343 return StringOrdinal(raw_data); | 358 return StringOrdinal(raw_data); |
344 } | 359 } |
345 | 360 |
346 void ExtensionSorting::SetPageOrdinal(const std::string& extension_id, | 361 void ExtensionSorting::SetPageOrdinal(const std::string& extension_id, |
347 const StringOrdinal& new_page_ordinal) { | 362 const StringOrdinal& new_page_ordinal) { |
348 // No work is required if the old and new values are the same. | 363 // No work is required if the old and new values are the same. |
349 if (new_page_ordinal.EqualOrBothInvalid(GetPageOrdinal(extension_id))) | 364 if (new_page_ordinal.EqualOrBothInvalid(GetPageOrdinal(extension_id))) |
350 return; | 365 return; |
351 | 366 |
352 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); | 367 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); |
353 RemoveOrdinalMapping( | 368 RemoveOrdinalMapping( |
354 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); | 369 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); |
355 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); | 370 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); |
356 | 371 |
357 Value* new_value = new_page_ordinal.IsValid() ? | 372 Value* new_value = new_page_ordinal.IsValid() ? |
358 Value::CreateStringValue(new_page_ordinal.ToString()) : | 373 Value::CreateStringValue(new_page_ordinal.ToString()) : |
359 NULL; | 374 NULL; |
360 | 375 |
361 extension_scoped_prefs_->UpdateExtensionPref( | 376 extension_scoped_prefs_->UpdateExtensionPref( |
362 extension_id, | 377 extension_id, |
363 kPrefPageOrdinal, | 378 kPrefPageOrdinal, |
364 new_value); | 379 new_value); |
380 | |
381 if (extension_service_) { | |
382 const Extension* ext = | |
383 extension_service_->GetInstalledExtension(extension_id); | |
384 DCHECK(!ext || ext->is_app()); | |
385 | |
386 if (ext) | |
387 extension_service_->SyncExtensionChangeIfNeeded(*ext); | |
388 } | |
365 } | 389 } |
366 | 390 |
367 void ExtensionSorting::ClearOrdinals(const std::string& extension_id) { | 391 void ExtensionSorting::ClearOrdinals(const std::string& extension_id) { |
368 RemoveOrdinalMapping(extension_id, | 392 RemoveOrdinalMapping(extension_id, |
369 GetPageOrdinal(extension_id), | 393 GetPageOrdinal(extension_id), |
370 GetAppLaunchOrdinal(extension_id)); | 394 GetAppLaunchOrdinal(extension_id)); |
371 | 395 |
372 extension_scoped_prefs_->UpdateExtensionPref( | 396 extension_scoped_prefs_->UpdateExtensionPref( |
373 extension_id, kPrefPageOrdinal, NULL); | 397 extension_id, kPrefPageOrdinal, NULL); |
374 extension_scoped_prefs_->UpdateExtensionPref( | 398 extension_scoped_prefs_->UpdateExtensionPref( |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 | 507 |
484 for (AppLaunchOrdinalMap::iterator it = | 508 for (AppLaunchOrdinalMap::iterator it = |
485 page_map->second.find(app_launch_ordinal); | 509 page_map->second.find(app_launch_ordinal); |
486 it != page_map->second.end(); ++it) { | 510 it != page_map->second.end(); ++it) { |
487 if (it->second == extension_id) { | 511 if (it->second == extension_id) { |
488 page_map->second.erase(it); | 512 page_map->second.erase(it); |
489 break; | 513 break; |
490 } | 514 } |
491 } | 515 } |
492 } | 516 } |
OLD | NEW |