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