| 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()]; |
| 83 | 73 |
| 84 while (ntp_ordinal_map_.size() < minimum_size) { | 74 while (ntp_ordinal_map_.size() < minimum_size) { |
| 85 syncer::StringOrdinal filler = | 75 syncer::StringOrdinal filler = |
| 86 ntp_ordinal_map_.rbegin()->first.CreateAfter(); | 76 ntp_ordinal_map_.rbegin()->first.CreateAfter(); |
| 87 AppLaunchOrdinalMap empty_ordinal_map; | 77 AppLaunchOrdinalMap empty_ordinal_map; |
| 88 ntp_ordinal_map_.insert(std::make_pair(filler, empty_ordinal_map)); | 78 ntp_ordinal_map_.insert(std::make_pair(filler, empty_ordinal_map)); |
| 89 } | 79 } |
| 90 } | 80 } |
| 91 | 81 |
| 92 void ChromeAppSorting::MigrateAppIndex( | 82 void ChromeAppSorting::MigrateAppIndex( |
| 93 const extensions::ExtensionIdList& extension_ids) { | 83 const extensions::ExtensionIdList& extension_ids) { |
| 94 if (extension_ids.empty()) | 84 if (extension_ids.empty()) |
| 95 return; | 85 return; |
| 96 | 86 |
| 87 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| 88 |
| 97 // Convert all the page index values to page ordinals. If there are any | 89 // 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 | 90 // app launch values that need to be migrated, inserted them into a sorted |
| 99 // set to be dealt with later. | 91 // set to be dealt with later. |
| 100 typedef std::map<syncer::StringOrdinal, std::map<int, const std::string*>, | 92 typedef std::map<syncer::StringOrdinal, std::map<int, const std::string*>, |
| 101 syncer::StringOrdinal::LessThanFn> AppPositionToIdMapping; | 93 syncer::StringOrdinal::LessThanFn> AppPositionToIdMapping; |
| 102 AppPositionToIdMapping app_launches_to_convert; | 94 AppPositionToIdMapping app_launches_to_convert; |
| 103 for (extensions::ExtensionIdList::const_iterator ext_id = | 95 for (extensions::ExtensionIdList::const_iterator ext_id = |
| 104 extension_ids.begin(); ext_id != extension_ids.end(); ++ext_id) { | 96 extension_ids.begin(); ext_id != extension_ids.end(); ++ext_id) { |
| 105 int old_page_index = 0; | 97 int old_page_index = 0; |
| 106 syncer::StringOrdinal page = GetPageOrdinal(*ext_id); | 98 syncer::StringOrdinal page = GetPageOrdinal(*ext_id); |
| 107 if (extension_scoped_prefs_->ReadPrefAsInteger( | 99 if (prefs->ReadPrefAsInteger(*ext_id, |
| 108 *ext_id, | 100 kPrefPageIndexDeprecated, |
| 109 kPrefPageIndexDeprecated, | 101 &old_page_index)) { |
| 110 &old_page_index)) { | |
| 111 // Some extensions have invalid page index, so we don't | 102 // Some extensions have invalid page index, so we don't |
| 112 // attempt to convert them. | 103 // attempt to convert them. |
| 113 if (old_page_index < 0) { | 104 if (old_page_index < 0) { |
| 114 DLOG(WARNING) << "Extension " << *ext_id | 105 DLOG(WARNING) << "Extension " << *ext_id |
| 115 << " has an invalid page index " << old_page_index | 106 << " has an invalid page index " << old_page_index |
| 116 << ". Aborting attempt to convert its index."; | 107 << ". Aborting attempt to convert its index."; |
| 117 break; | 108 break; |
| 118 } | 109 } |
| 119 | 110 |
| 120 CreateOrdinalsIfNecessary(static_cast<size_t>(old_page_index) + 1); | 111 CreateOrdinalsIfNecessary(static_cast<size_t>(old_page_index) + 1); |
| 121 | 112 |
| 122 page = PageIntegerAsStringOrdinal(old_page_index); | 113 page = PageIntegerAsStringOrdinal(old_page_index); |
| 123 SetPageOrdinal(*ext_id, page); | 114 SetPageOrdinal(*ext_id, page); |
| 124 extension_scoped_prefs_->UpdateExtensionPref( | 115 prefs->UpdateExtensionPref(*ext_id, kPrefPageIndexDeprecated, NULL); |
| 125 *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 (prefs->ReadPrefAsInteger(*ext_id, |
| 130 *ext_id, | 120 kPrefAppLaunchIndexDeprecated, |
| 131 kPrefAppLaunchIndexDeprecated, | 121 &old_app_launch_index)) { |
| 132 &old_app_launch_index)) { | |
| 133 // We can't update the app launch index value yet, because we use | 122 // We can't update the app launch index value yet, because we use |
| 134 // GetNextAppLaunchOrdinal to get the new ordinal value and it requires | 123 // GetNextAppLaunchOrdinal to get the new ordinal value and it requires |
| 135 // all the ordinals with lower values to have already been migrated. | 124 // all the ordinals with lower values to have already been migrated. |
| 136 // A valid page ordinal is also required because otherwise there is | 125 // A valid page ordinal is also required because otherwise there is |
| 137 // no page to add the app to. | 126 // no page to add the app to. |
| 138 if (page.IsValid()) | 127 if (page.IsValid()) |
| 139 app_launches_to_convert[page][old_app_launch_index] = &*ext_id; | 128 app_launches_to_convert[page][old_app_launch_index] = &*ext_id; |
| 140 | 129 |
| 141 extension_scoped_prefs_->UpdateExtensionPref( | 130 prefs->UpdateExtensionPref(*ext_id, kPrefAppLaunchIndexDeprecated, NULL); |
| 142 *ext_id, kPrefAppLaunchIndexDeprecated, NULL); | |
| 143 } | 131 } |
| 144 } | 132 } |
| 145 | 133 |
| 146 // Remove any empty pages that may have been added. This shouldn't occur, | 134 // 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 | 135 // but double check here to prevent future problems with conversions between |
| 148 // integers and StringOrdinals. | 136 // integers and StringOrdinals. |
| 149 for (PageOrdinalMap::iterator it = ntp_ordinal_map_.begin(); | 137 for (PageOrdinalMap::iterator it = ntp_ordinal_map_.begin(); |
| 150 it != ntp_ordinal_map_.end();) { | 138 it != ntp_ordinal_map_.end();) { |
| 151 if (it->second.empty()) { | 139 if (it->second.empty()) { |
| 152 PageOrdinalMap::iterator prev_it = it; | 140 PageOrdinalMap::iterator prev_it = it; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 content::Details<const std::string>(&moved_extension_id)); | 278 content::Details<const std::string>(&moved_extension_id)); |
| 291 } | 279 } |
| 292 | 280 |
| 293 | 281 |
| 294 syncer::StringOrdinal ChromeAppSorting::GetAppLaunchOrdinal( | 282 syncer::StringOrdinal ChromeAppSorting::GetAppLaunchOrdinal( |
| 295 const std::string& extension_id) const { | 283 const std::string& extension_id) const { |
| 296 std::string raw_value; | 284 std::string raw_value; |
| 297 // If the preference read fails then raw_value will still be unset and we | 285 // 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 | 286 // will return an invalid StringOrdinal to signal that no app launch ordinal |
| 299 // was found. | 287 // was found. |
| 300 extension_scoped_prefs_->ReadPrefAsString( | 288 ExtensionPrefs::Get(browser_context_)->ReadPrefAsString( |
| 301 extension_id, kPrefAppLaunchOrdinal, &raw_value); | 289 extension_id, kPrefAppLaunchOrdinal, &raw_value); |
| 302 return syncer::StringOrdinal(raw_value); | 290 return syncer::StringOrdinal(raw_value); |
| 303 } | 291 } |
| 304 | 292 |
| 305 void ChromeAppSorting::SetAppLaunchOrdinal( | 293 void ChromeAppSorting::SetAppLaunchOrdinal( |
| 306 const std::string& extension_id, | 294 const std::string& extension_id, |
| 307 const syncer::StringOrdinal& new_app_launch_ordinal) { | 295 const syncer::StringOrdinal& new_app_launch_ordinal) { |
| 308 // No work is required if the old and new values are the same. | 296 // No work is required if the old and new values are the same. |
| 309 if (new_app_launch_ordinal.EqualsOrBothInvalid( | 297 if (new_app_launch_ordinal.EqualsOrBothInvalid( |
| 310 GetAppLaunchOrdinal(extension_id))) { | 298 GetAppLaunchOrdinal(extension_id))) { |
| 311 return; | 299 return; |
| 312 } | 300 } |
| 313 | 301 |
| 314 syncer::StringOrdinal page_ordinal = GetPageOrdinal(extension_id); | 302 syncer::StringOrdinal page_ordinal = GetPageOrdinal(extension_id); |
| 315 RemoveOrdinalMapping( | 303 RemoveOrdinalMapping( |
| 316 extension_id, page_ordinal, GetAppLaunchOrdinal(extension_id)); | 304 extension_id, page_ordinal, GetAppLaunchOrdinal(extension_id)); |
| 317 AddOrdinalMapping(extension_id, page_ordinal, new_app_launch_ordinal); | 305 AddOrdinalMapping(extension_id, page_ordinal, new_app_launch_ordinal); |
| 318 | 306 |
| 319 base::Value* new_value = new_app_launch_ordinal.IsValid() ? | 307 base::Value* new_value = new_app_launch_ordinal.IsValid() ? |
| 320 new base::StringValue(new_app_launch_ordinal.ToInternalValue()) : | 308 new base::StringValue(new_app_launch_ordinal.ToInternalValue()) : |
| 321 NULL; | 309 NULL; |
| 322 | 310 |
| 323 extension_scoped_prefs_->UpdateExtensionPref( | 311 ExtensionPrefs::Get(browser_context_)->UpdateExtensionPref( |
| 324 extension_id, | 312 extension_id, |
| 325 kPrefAppLaunchOrdinal, | 313 kPrefAppLaunchOrdinal, |
| 326 new_value); | 314 new_value); |
| 327 SyncIfNeeded(extension_id); | 315 SyncIfNeeded(extension_id); |
| 328 } | 316 } |
| 329 | 317 |
| 330 syncer::StringOrdinal ChromeAppSorting::CreateFirstAppLaunchOrdinal( | 318 syncer::StringOrdinal ChromeAppSorting::CreateFirstAppLaunchOrdinal( |
| 331 const syncer::StringOrdinal& page_ordinal) const { | 319 const syncer::StringOrdinal& page_ordinal) const { |
| 332 const syncer::StringOrdinal& min_ordinal = | 320 const syncer::StringOrdinal& min_ordinal = |
| 333 GetMinOrMaxAppLaunchOrdinalsOnPage(page_ordinal, | 321 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. | 359 // Add a new page as all existing pages are full. |
| 372 syncer::StringOrdinal last_element = ntp_ordinal_map_.rbegin()->first; | 360 syncer::StringOrdinal last_element = ntp_ordinal_map_.rbegin()->first; |
| 373 return last_element.CreateAfter(); | 361 return last_element.CreateAfter(); |
| 374 } | 362 } |
| 375 | 363 |
| 376 syncer::StringOrdinal ChromeAppSorting::GetPageOrdinal( | 364 syncer::StringOrdinal ChromeAppSorting::GetPageOrdinal( |
| 377 const std::string& extension_id) const { | 365 const std::string& extension_id) const { |
| 378 std::string raw_data; | 366 std::string raw_data; |
| 379 // If the preference read fails then raw_data will still be unset and we will | 367 // 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. | 368 // return an invalid StringOrdinal to signal that no page ordinal was found. |
| 381 extension_scoped_prefs_->ReadPrefAsString( | 369 ExtensionPrefs::Get(browser_context_)->ReadPrefAsString( |
| 382 extension_id, kPrefPageOrdinal, &raw_data); | 370 extension_id, kPrefPageOrdinal, &raw_data); |
| 383 return syncer::StringOrdinal(raw_data); | 371 return syncer::StringOrdinal(raw_data); |
| 384 } | 372 } |
| 385 | 373 |
| 386 void ChromeAppSorting::SetPageOrdinal( | 374 void ChromeAppSorting::SetPageOrdinal( |
| 387 const std::string& extension_id, | 375 const std::string& extension_id, |
| 388 const syncer::StringOrdinal& new_page_ordinal) { | 376 const syncer::StringOrdinal& new_page_ordinal) { |
| 389 // No work is required if the old and new values are the same. | 377 // No work is required if the old and new values are the same. |
| 390 if (new_page_ordinal.EqualsOrBothInvalid(GetPageOrdinal(extension_id))) | 378 if (new_page_ordinal.EqualsOrBothInvalid(GetPageOrdinal(extension_id))) |
| 391 return; | 379 return; |
| 392 | 380 |
| 393 syncer::StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); | 381 syncer::StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(extension_id); |
| 394 RemoveOrdinalMapping( | 382 RemoveOrdinalMapping( |
| 395 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); | 383 extension_id, GetPageOrdinal(extension_id), app_launch_ordinal); |
| 396 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); | 384 AddOrdinalMapping(extension_id, new_page_ordinal, app_launch_ordinal); |
| 397 | 385 |
| 398 base::Value* new_value = new_page_ordinal.IsValid() ? | 386 base::Value* new_value = new_page_ordinal.IsValid() ? |
| 399 new base::StringValue(new_page_ordinal.ToInternalValue()) : | 387 new base::StringValue(new_page_ordinal.ToInternalValue()) : |
| 400 NULL; | 388 NULL; |
| 401 | 389 |
| 402 extension_scoped_prefs_->UpdateExtensionPref( | 390 ExtensionPrefs::Get(browser_context_)->UpdateExtensionPref( |
| 403 extension_id, | 391 extension_id, |
| 404 kPrefPageOrdinal, | 392 kPrefPageOrdinal, |
| 405 new_value); | 393 new_value); |
| 406 SyncIfNeeded(extension_id); | 394 SyncIfNeeded(extension_id); |
| 407 } | 395 } |
| 408 | 396 |
| 409 void ChromeAppSorting::ClearOrdinals(const std::string& extension_id) { | 397 void ChromeAppSorting::ClearOrdinals(const std::string& extension_id) { |
| 410 RemoveOrdinalMapping(extension_id, | 398 RemoveOrdinalMapping(extension_id, |
| 411 GetPageOrdinal(extension_id), | 399 GetPageOrdinal(extension_id), |
| 412 GetAppLaunchOrdinal(extension_id)); | 400 GetAppLaunchOrdinal(extension_id)); |
| 413 | 401 |
| 414 extension_scoped_prefs_->UpdateExtensionPref( | 402 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| 415 extension_id, kPrefPageOrdinal, NULL); | 403 prefs->UpdateExtensionPref(extension_id, kPrefPageOrdinal, NULL); |
| 416 extension_scoped_prefs_->UpdateExtensionPref( | 404 prefs->UpdateExtensionPref(extension_id, kPrefAppLaunchOrdinal, NULL); |
| 417 extension_id, kPrefAppLaunchOrdinal, NULL); | |
| 418 } | 405 } |
| 419 | 406 |
| 420 int ChromeAppSorting::PageStringOrdinalAsInteger( | 407 int ChromeAppSorting::PageStringOrdinalAsInteger( |
| 421 const syncer::StringOrdinal& page_ordinal) const { | 408 const syncer::StringOrdinal& page_ordinal) const { |
| 422 if (!page_ordinal.IsValid()) | 409 if (!page_ordinal.IsValid()) |
| 423 return -1; | 410 return -1; |
| 424 | 411 |
| 425 PageOrdinalMap::const_iterator it = ntp_ordinal_map_.find(page_ordinal); | 412 PageOrdinalMap::const_iterator it = ntp_ordinal_map_.find(page_ordinal); |
| 426 return it != ntp_ordinal_map_.end() ? | 413 return it != ntp_ordinal_map_.end() ? |
| 427 std::distance(ntp_ordinal_map_.begin(), it) : -1; | 414 std::distance(ntp_ordinal_map_.begin(), it) : -1; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 return_value = app_list.rbegin()->first; | 453 return_value = app_list.rbegin()->first; |
| 467 else if (return_type == ChromeAppSorting::MIN_ORDINAL) | 454 else if (return_type == ChromeAppSorting::MIN_ORDINAL) |
| 468 return_value = app_list.begin()->first; | 455 return_value = app_list.begin()->first; |
| 469 } | 456 } |
| 470 | 457 |
| 471 return return_value; | 458 return return_value; |
| 472 } | 459 } |
| 473 | 460 |
| 474 void ChromeAppSorting::InitializePageOrdinalMap( | 461 void ChromeAppSorting::InitializePageOrdinalMap( |
| 475 const extensions::ExtensionIdList& extension_ids) { | 462 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 = | 463 for (extensions::ExtensionIdList::const_iterator ext_it = |
| 480 extension_ids.begin(); ext_it != extension_ids.end(); ++ext_it) { | 464 extension_ids.begin(); ext_it != extension_ids.end(); ++ext_it) { |
| 481 AddOrdinalMapping(*ext_it, | 465 AddOrdinalMapping(*ext_it, |
| 482 GetPageOrdinal(*ext_it), | 466 GetPageOrdinal(*ext_it), |
| 483 GetAppLaunchOrdinal(*ext_it)); | 467 GetAppLaunchOrdinal(*ext_it)); |
| 484 | 468 |
| 485 // Ensure that the web store app still isn't found in this list, since | 469 // Ensure that the web store app still isn't found in this list, since |
| 486 // it is added after this loop. | 470 // it is added after this loop. |
| 487 DCHECK(*ext_it != extensions::kWebStoreAppId); | 471 DCHECK(*ext_it != extensions::kWebStoreAppId); |
| 488 DCHECK(*ext_it != extension_misc::kChromeAppId); | 472 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(); | 617 for (AppLaunchOrdinalMap::const_iterator it = m.begin(); it != m.end(); |
| 634 ++it) { | 618 ++it) { |
| 635 const std::string& id = it->second; | 619 const std::string& id = it->second; |
| 636 if (ntp_hidden_extensions_.count(id) == 0) | 620 if (ntp_hidden_extensions_.count(id) == 0) |
| 637 result++; | 621 result++; |
| 638 } | 622 } |
| 639 return result; | 623 return result; |
| 640 } | 624 } |
| 641 | 625 |
| 642 } // namespace extensions | 626 } // namespace extensions |
| OLD | NEW |