Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chrome_app_sorting.h" | 5 #include "chrome/browser/extensions/chrome_app_sorting.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 // ChromeAppSorting::AppOrdinals | 44 // ChromeAppSorting::AppOrdinals |
| 45 | 45 |
| 46 ChromeAppSorting::AppOrdinals::AppOrdinals() {} | 46 ChromeAppSorting::AppOrdinals::AppOrdinals() {} |
| 47 | 47 |
| 48 ChromeAppSorting::AppOrdinals::~AppOrdinals() {} | 48 ChromeAppSorting::AppOrdinals::~AppOrdinals() {} |
| 49 | 49 |
| 50 //////////////////////////////////////////////////////////////////////////////// | 50 //////////////////////////////////////////////////////////////////////////////// |
| 51 // ChromeAppSorting | 51 // ChromeAppSorting |
| 52 | 52 |
| 53 ChromeAppSorting::ChromeAppSorting(content::BrowserContext* browser_context) | 53 ChromeAppSorting::ChromeAppSorting(content::BrowserContext* browser_context) |
| 54 : extension_scoped_prefs_(NULL), | 54 : browser_context_(browser_context), |
| 55 browser_context_(browser_context), | |
| 56 default_ordinals_created_(false) { | 55 default_ordinals_created_(false) { |
| 57 } | 56 } |
| 58 | 57 |
| 59 ChromeAppSorting::~ChromeAppSorting() { | 58 ChromeAppSorting::~ChromeAppSorting() { |
| 60 } | 59 } |
| 61 | 60 |
| 62 void ChromeAppSorting::SetExtensionScopedPrefs(ExtensionScopedPrefs* prefs) { | |
| 63 extension_scoped_prefs_ = prefs; | |
| 64 } | |
| 65 | |
| 66 void ChromeAppSorting::CheckExtensionScopedPrefs() const { | |
| 67 CHECK(extension_scoped_prefs_); | |
| 68 } | |
| 69 | |
| 70 void ChromeAppSorting::Initialize( | 61 void ChromeAppSorting::Initialize( |
| 71 const extensions::ExtensionIdList& extension_ids) { | 62 const extensions::ExtensionIdList& extension_ids) { |
| 72 CHECK(extension_scoped_prefs_); | |
| 73 InitializePageOrdinalMap(extension_ids); | 63 InitializePageOrdinalMap(extension_ids); |
| 74 | 64 |
| 75 MigrateAppIndex(extension_ids); | 65 MigrateAppIndex(extension_ids); |
| 76 } | 66 } |
| 77 | 67 |
| 78 void ChromeAppSorting::CreateOrdinalsIfNecessary(size_t minimum_size) { | 68 void ChromeAppSorting::CreateOrdinalsIfNecessary(size_t minimum_size) { |
| 79 // Create StringOrdinal values as required to ensure |ntp_ordinal_map_| has at | 69 // Create StringOrdinal values as required to ensure |ntp_ordinal_map_| has at |
| 80 // least |minimum_size| entries. | 70 // least |minimum_size| entries. |
| 81 if (ntp_ordinal_map_.empty() && minimum_size > 0) | 71 if (ntp_ordinal_map_.empty() && minimum_size > 0) |
| 82 ntp_ordinal_map_[syncer::StringOrdinal::CreateInitialOrdinal()]; | 72 ntp_ordinal_map_[syncer::StringOrdinal::CreateInitialOrdinal()]; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 97 // Convert all the page index values to page ordinals. If there are any | 87 // Convert all the page index values to page ordinals. If there are any |
| 98 // app launch values that need to be migrated, inserted them into a sorted | 88 // app launch values that need to be migrated, inserted them into a sorted |
| 99 // set to be dealt with later. | 89 // set to be dealt with later. |
| 100 typedef std::map<syncer::StringOrdinal, std::map<int, const std::string*>, | 90 typedef std::map<syncer::StringOrdinal, std::map<int, const std::string*>, |
| 101 syncer::StringOrdinal::LessThanFn> AppPositionToIdMapping; | 91 syncer::StringOrdinal::LessThanFn> AppPositionToIdMapping; |
| 102 AppPositionToIdMapping app_launches_to_convert; | 92 AppPositionToIdMapping app_launches_to_convert; |
| 103 for (extensions::ExtensionIdList::const_iterator ext_id = | 93 for (extensions::ExtensionIdList::const_iterator ext_id = |
| 104 extension_ids.begin(); ext_id != extension_ids.end(); ++ext_id) { | 94 extension_ids.begin(); ext_id != extension_ids.end(); ++ext_id) { |
| 105 int old_page_index = 0; | 95 int old_page_index = 0; |
| 106 syncer::StringOrdinal page = GetPageOrdinal(*ext_id); | 96 syncer::StringOrdinal page = GetPageOrdinal(*ext_id); |
| 107 if (extension_scoped_prefs_->ReadPrefAsInteger( | 97 if (ExtensionPrefs::Get(browser_context_)->ReadPrefAsInteger( |
|
not at google - send to devlin
2015/07/29 18:22:03
I would hold onto a reference to this at the start
Marc Treib
2015/07/30 14:04:12
Done.
| |
| 108 *ext_id, | 98 *ext_id, |
| 109 kPrefPageIndexDeprecated, | 99 kPrefPageIndexDeprecated, |
| 110 &old_page_index)) { | 100 &old_page_index)) { |
| 111 // Some extensions have invalid page index, so we don't | 101 // Some extensions have invalid page index, so we don't |
| 112 // attempt to convert them. | 102 // attempt to convert them. |
| 113 if (old_page_index < 0) { | 103 if (old_page_index < 0) { |
| 114 DLOG(WARNING) << "Extension " << *ext_id | 104 DLOG(WARNING) << "Extension " << *ext_id |
| 115 << " has an invalid page index " << old_page_index | 105 << " has an invalid page index " << old_page_index |
| 116 << ". Aborting attempt to convert its index."; | 106 << ". Aborting attempt to convert its index."; |
| 117 break; | 107 break; |
| 118 } | 108 } |
| 119 | 109 |
| 120 CreateOrdinalsIfNecessary(static_cast<size_t>(old_page_index) + 1); | 110 CreateOrdinalsIfNecessary(static_cast<size_t>(old_page_index) + 1); |
| 121 | 111 |
| 122 page = PageIntegerAsStringOrdinal(old_page_index); | 112 page = PageIntegerAsStringOrdinal(old_page_index); |
| 123 SetPageOrdinal(*ext_id, page); | 113 SetPageOrdinal(*ext_id, page); |
| 124 extension_scoped_prefs_->UpdateExtensionPref( | 114 ExtensionPrefs::Get(browser_context_)->UpdateExtensionPref( |
| 125 *ext_id, kPrefPageIndexDeprecated, NULL); | 115 *ext_id, kPrefPageIndexDeprecated, NULL); |
| 126 } | 116 } |
| 127 | 117 |
| 128 int old_app_launch_index = 0; | 118 int old_app_launch_index = 0; |
| 129 if (extension_scoped_prefs_->ReadPrefAsInteger( | 119 if (ExtensionPrefs::Get(browser_context_)->ReadPrefAsInteger( |
| 130 *ext_id, | 120 *ext_id, |
| 131 kPrefAppLaunchIndexDeprecated, | 121 kPrefAppLaunchIndexDeprecated, |
| 132 &old_app_launch_index)) { | 122 &old_app_launch_index)) { |
| 133 // We can't update the app launch index value yet, because we use | 123 // We can't update the app launch index value yet, because we use |
| 134 // GetNextAppLaunchOrdinal to get the new ordinal value and it requires | 124 // GetNextAppLaunchOrdinal to get the new ordinal value and it requires |
| 135 // all the ordinals with lower values to have already been migrated. | 125 // all the ordinals with lower values to have already been migrated. |
| 136 // A valid page ordinal is also required because otherwise there is | 126 // A valid page ordinal is also required because otherwise there is |
| 137 // no page to add the app to. | 127 // no page to add the app to. |
| 138 if (page.IsValid()) | 128 if (page.IsValid()) |
| 139 app_launches_to_convert[page][old_app_launch_index] = &*ext_id; | 129 app_launches_to_convert[page][old_app_launch_index] = &*ext_id; |
| 140 | 130 |
| 141 extension_scoped_prefs_->UpdateExtensionPref( | 131 ExtensionPrefs::Get(browser_context_)->UpdateExtensionPref( |
| 142 *ext_id, kPrefAppLaunchIndexDeprecated, NULL); | 132 *ext_id, kPrefAppLaunchIndexDeprecated, NULL); |
| 143 } | 133 } |
| 144 } | 134 } |
| 145 | 135 |
| 146 // Remove any empty pages that may have been added. This shouldn't occur, | 136 // Remove any empty pages that may have been added. This shouldn't occur, |
| 147 // but double check here to prevent future problems with conversions between | 137 // but double check here to prevent future problems with conversions between |
| 148 // integers and StringOrdinals. | 138 // integers and StringOrdinals. |
| 149 for (PageOrdinalMap::iterator it = ntp_ordinal_map_.begin(); | 139 for (PageOrdinalMap::iterator it = ntp_ordinal_map_.begin(); |
| 150 it != ntp_ordinal_map_.end();) { | 140 it != ntp_ordinal_map_.end();) { |
| 151 if (it->second.empty()) { | 141 if (it->second.empty()) { |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 content::Details<const std::string>(&moved_extension_id)); | 280 content::Details<const std::string>(&moved_extension_id)); |
| 291 } | 281 } |
| 292 | 282 |
| 293 | 283 |
| 294 syncer::StringOrdinal ChromeAppSorting::GetAppLaunchOrdinal( | 284 syncer::StringOrdinal ChromeAppSorting::GetAppLaunchOrdinal( |
| 295 const std::string& extension_id) const { | 285 const std::string& extension_id) const { |
| 296 std::string raw_value; | 286 std::string raw_value; |
| 297 // If the preference read fails then raw_value will still be unset and we | 287 // If the preference read fails then raw_value will still be unset and we |
| 298 // will return an invalid StringOrdinal to signal that no app launch ordinal | 288 // will return an invalid StringOrdinal to signal that no app launch ordinal |
| 299 // was found. | 289 // was found. |
| 300 extension_scoped_prefs_->ReadPrefAsString( | 290 ExtensionPrefs::Get(browser_context_)->ReadPrefAsString( |
| 301 extension_id, kPrefAppLaunchOrdinal, &raw_value); | 291 extension_id, kPrefAppLaunchOrdinal, &raw_value); |
| 302 return syncer::StringOrdinal(raw_value); | 292 return syncer::StringOrdinal(raw_value); |
| 303 } | 293 } |
| 304 | 294 |
| 305 void ChromeAppSorting::SetAppLaunchOrdinal( | 295 void ChromeAppSorting::SetAppLaunchOrdinal( |
| 306 const std::string& extension_id, | 296 const std::string& extension_id, |
| 307 const syncer::StringOrdinal& new_app_launch_ordinal) { | 297 const syncer::StringOrdinal& new_app_launch_ordinal) { |
| 308 // No work is required if the old and new values are the same. | 298 // No work is required if the old and new values are the same. |
| 309 if (new_app_launch_ordinal.EqualsOrBothInvalid( | 299 if (new_app_launch_ordinal.EqualsOrBothInvalid( |
| 310 GetAppLaunchOrdinal(extension_id))) { | 300 GetAppLaunchOrdinal(extension_id))) { |
| 311 return; | 301 return; |
| 312 } | 302 } |
| 313 | 303 |
| 314 syncer::StringOrdinal page_ordinal = GetPageOrdinal(extension_id); | 304 syncer::StringOrdinal page_ordinal = GetPageOrdinal(extension_id); |
| 315 RemoveOrdinalMapping( | 305 RemoveOrdinalMapping( |
| 316 extension_id, page_ordinal, GetAppLaunchOrdinal(extension_id)); | 306 extension_id, page_ordinal, GetAppLaunchOrdinal(extension_id)); |
| 317 AddOrdinalMapping(extension_id, page_ordinal, new_app_launch_ordinal); | 307 AddOrdinalMapping(extension_id, page_ordinal, new_app_launch_ordinal); |
| 318 | 308 |
| 319 base::Value* new_value = new_app_launch_ordinal.IsValid() ? | 309 base::Value* new_value = new_app_launch_ordinal.IsValid() ? |
| 320 new base::StringValue(new_app_launch_ordinal.ToInternalValue()) : | 310 new base::StringValue(new_app_launch_ordinal.ToInternalValue()) : |
| 321 NULL; | 311 NULL; |
| 322 | 312 |
| 323 extension_scoped_prefs_->UpdateExtensionPref( | 313 ExtensionPrefs::Get(browser_context_)->UpdateExtensionPref( |
| 324 extension_id, | 314 extension_id, |
| 325 kPrefAppLaunchOrdinal, | 315 kPrefAppLaunchOrdinal, |
| 326 new_value); | 316 new_value); |
| 327 SyncIfNeeded(extension_id); | 317 SyncIfNeeded(extension_id); |
| 328 } | 318 } |
| 329 | 319 |
| 330 syncer::StringOrdinal ChromeAppSorting::CreateFirstAppLaunchOrdinal( | 320 syncer::StringOrdinal ChromeAppSorting::CreateFirstAppLaunchOrdinal( |
| 331 const syncer::StringOrdinal& page_ordinal) const { | 321 const syncer::StringOrdinal& page_ordinal) const { |
| 332 const syncer::StringOrdinal& min_ordinal = | 322 const syncer::StringOrdinal& min_ordinal = |
| 333 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, | 323 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 // Add a new page as all existing pages are full. | 361 // Add a new page as all existing pages are full. |
| 372 syncer::StringOrdinal last_element = ntp_ordinal_map_.rbegin()->first; | 362 syncer::StringOrdinal last_element = ntp_ordinal_map_.rbegin()->first; |
| 373 return last_element.CreateAfter(); | 363 return last_element.CreateAfter(); |
| 374 } | 364 } |
| 375 | 365 |
| 376 syncer::StringOrdinal ChromeAppSorting::GetPageOrdinal( | 366 syncer::StringOrdinal ChromeAppSorting::GetPageOrdinal( |
| 377 const std::string& extension_id) const { | 367 const std::string& extension_id) const { |
| 378 std::string raw_data; | 368 std::string raw_data; |
| 379 // If the preference read fails then raw_data will still be unset and we will | 369 // If the preference read fails then raw_data will still be unset and we will |
| 380 // return an invalid StringOrdinal to signal that no page ordinal was found. | 370 // return an invalid StringOrdinal to signal that no page ordinal was found. |
| 381 extension_scoped_prefs_->ReadPrefAsString( | 371 ExtensionPrefs::Get(browser_context_)->ReadPrefAsString( |
| 382 extension_id, kPrefPageOrdinal, &raw_data); | 372 extension_id, kPrefPageOrdinal, &raw_data); |
| 383 return syncer::StringOrdinal(raw_data); | 373 return syncer::StringOrdinal(raw_data); |
| 384 } | 374 } |
| 385 | 375 |
| 386 void ChromeAppSorting::SetPageOrdinal( | 376 void ChromeAppSorting::SetPageOrdinal( |
| 387 const std::string& extension_id, | 377 const std::string& extension_id, |
| 388 const syncer::StringOrdinal& new_page_ordinal) { | 378 const syncer::StringOrdinal& new_page_ordinal) { |
| 389 // No work is required if the old and new values are the same. | 379 // No work is required if the old and new values are the same. |
| 390 if (new_page_ordinal.EqualsOrBothInvalid(GetPageOrdinal(extension_id))) | 380 if (new_page_ordinal.EqualsOrBothInvalid(GetPageOrdinal(extension_id))) |
| 391 return; | 381 return; |
| 392 | 382 |
| 393 syncer::StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); | 383 syncer::StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); |
| 394 RemoveOrdinalMapping( | 384 RemoveOrdinalMapping( |
| 395 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); | 385 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); |
| 396 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); | 386 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); |
| 397 | 387 |
| 398 base::Value* new_value = new_page_ordinal.IsValid() ? | 388 base::Value* new_value = new_page_ordinal.IsValid() ? |
| 399 new base::StringValue(new_page_ordinal.ToInternalValue()) : | 389 new base::StringValue(new_page_ordinal.ToInternalValue()) : |
| 400 NULL; | 390 NULL; |
| 401 | 391 |
| 402 extension_scoped_prefs_->UpdateExtensionPref( | 392 ExtensionPrefs::Get(browser_context_)->UpdateExtensionPref( |
| 403 extension_id, | 393 extension_id, |
| 404 kPrefPageOrdinal, | 394 kPrefPageOrdinal, |
| 405 new_value); | 395 new_value); |
| 406 SyncIfNeeded(extension_id); | 396 SyncIfNeeded(extension_id); |
| 407 } | 397 } |
| 408 | 398 |
| 409 void ChromeAppSorting::ClearOrdinals(const std::string& extension_id) { | 399 void ChromeAppSorting::ClearOrdinals(const std::string& extension_id) { |
| 410 RemoveOrdinalMapping(extension_id, | 400 RemoveOrdinalMapping(extension_id, |
| 411 GetPageOrdinal(extension_id), | 401 GetPageOrdinal(extension_id), |
| 412 GetAppLaunchOrdinal(extension_id)); | 402 GetAppLaunchOrdinal(extension_id)); |
| 413 | 403 |
| 414 extension_scoped_prefs_->UpdateExtensionPref( | 404 ExtensionPrefs::Get(browser_context_)->UpdateExtensionPref( |
|
not at google - send to devlin
2015/07/29 18:22:03
Here as well.
Marc Treib
2015/07/30 14:04:12
Done.
| |
| 415 extension_id, kPrefPageOrdinal, NULL); | 405 extension_id, kPrefPageOrdinal, NULL); |
| 416 extension_scoped_prefs_->UpdateExtensionPref( | 406 ExtensionPrefs::Get(browser_context_)->UpdateExtensionPref( |
| 417 extension_id, kPrefAppLaunchOrdinal, NULL); | 407 extension_id, kPrefAppLaunchOrdinal, NULL); |
| 418 } | 408 } |
| 419 | 409 |
| 420 int ChromeAppSorting::PageStringOrdinalAsInteger( | 410 int ChromeAppSorting::PageStringOrdinalAsInteger( |
| 421 const syncer::StringOrdinal& page_ordinal) const { | 411 const syncer::StringOrdinal& page_ordinal) const { |
| 422 if (!page_ordinal.IsValid()) | 412 if (!page_ordinal.IsValid()) |
| 423 return -1; | 413 return -1; |
| 424 | 414 |
| 425 PageOrdinalMap::const_iterator it = ntp_ordinal_map_.find(page_ordinal); | 415 PageOrdinalMap::const_iterator it = ntp_ordinal_map_.find(page_ordinal); |
| 426 return it != ntp_ordinal_map_.end() ? | 416 return it != ntp_ordinal_map_.end() ? |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 466 return_value = app_list.rbegin()->first; | 456 return_value = app_list.rbegin()->first; |
| 467 else if (return_type == ChromeAppSorting::MIN_ORDINAL) | 457 else if (return_type == ChromeAppSorting::MIN_ORDINAL) |
| 468 return_value = app_list.begin()->first; | 458 return_value = app_list.begin()->first; |
| 469 } | 459 } |
| 470 | 460 |
| 471 return return_value; | 461 return return_value; |
| 472 } | 462 } |
| 473 | 463 |
| 474 void ChromeAppSorting::InitializePageOrdinalMap( | 464 void ChromeAppSorting::InitializePageOrdinalMap( |
| 475 const extensions::ExtensionIdList& extension_ids) { | 465 const extensions::ExtensionIdList& extension_ids) { |
| 476 // TODO(mgiuca): Added this CHECK to try and diagnose http://crbug.com/476648. | |
| 477 // Remove it after the investigation is concluded. | |
| 478 CHECK(extension_scoped_prefs_); | |
| 479 for (extensions::ExtensionIdList::const_iterator ext_it = | 466 for (extensions::ExtensionIdList::const_iterator ext_it = |
| 480 extension_ids.begin(); ext_it != extension_ids.end(); ++ext_it) { | 467 extension_ids.begin(); ext_it != extension_ids.end(); ++ext_it) { |
| 481 AddOrdinalMapping(*ext_it, | 468 AddOrdinalMapping(*ext_it, |
| 482 GetPageOrdinal(*ext_it), | 469 GetPageOrdinal(*ext_it), |
| 483 GetAppLaunchOrdinal(*ext_it)); | 470 GetAppLaunchOrdinal(*ext_it)); |
| 484 | 471 |
| 485 // Ensure that the web store app still isn't found in this list, since | 472 // Ensure that the web store app still isn't found in this list, since |
| 486 // it is added after this loop. | 473 // it is added after this loop. |
| 487 DCHECK(*ext_it != extensions::kWebStoreAppId); | 474 DCHECK(*ext_it != extensions::kWebStoreAppId); |
| 488 DCHECK(*ext_it != extension_misc::kChromeAppId); | 475 DCHECK(*ext_it != extension_misc::kChromeAppId); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 633 for (AppLaunchOrdinalMap::const_iterator it = m.begin(); it != m.end(); | 620 for (AppLaunchOrdinalMap::const_iterator it = m.begin(); it != m.end(); |
| 634 ++it) { | 621 ++it) { |
| 635 const std::string& id = it->second; | 622 const std::string& id = it->second; |
| 636 if (ntp_hidden_extensions_.count(id) == 0) | 623 if (ntp_hidden_extensions_.count(id) == 0) |
| 637 result++; | 624 result++; |
| 638 } | 625 } |
| 639 return result; | 626 return result; |
| 640 } | 627 } |
| 641 | 628 |
| 642 } // namespace extensions | 629 } // namespace extensions |
| OLD | NEW |