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