| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" | 5 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 |
| 8 #include <memory> | 9 #include <memory> |
| 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/hash.h" | 12 #include "base/hash.h" |
| 11 #include "base/i18n/string_compare.h" | 13 #include "base/i18n/string_compare.h" |
| 12 #include "base/mac/bind_objc_block.h" | 14 #include "base/mac/bind_objc_block.h" |
| 13 #include "base/mac/scoped_nsautorelease_pool.h" | 15 #include "base/mac/scoped_nsautorelease_pool.h" |
| 14 #include "base/mac/scoped_nsobject.h" | 16 #include "base/mac/scoped_nsobject.h" |
| 17 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/user_metrics_action.h" | 18 #include "base/metrics/user_metrics_action.h" |
| 16 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 18 #include "components/bookmarks/browser/bookmark_model.h" | 21 #include "components/bookmarks/browser/bookmark_model.h" |
| 19 #include "components/query_parser/query_parser.h" | 22 #include "components/query_parser/query_parser.h" |
| 20 #include "components/strings/grit/components_strings.h" | 23 #include "components/strings/grit/components_strings.h" |
| 21 #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" | 24 #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" |
| 22 #include "ios/chrome/browser/experimental_flags.h" | 25 #include "ios/chrome/browser/experimental_flags.h" |
| 23 #import "ios/chrome/browser/ui/bookmarks/bookmark_collection_cells.h" | 26 #import "ios/chrome/browser/ui/bookmarks/bookmark_collection_cells.h" |
| 24 #import "ios/chrome/browser/ui/bookmarks/bookmark_menu_item.h" | 27 #import "ios/chrome/browser/ui/bookmarks/bookmark_menu_item.h" |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 390 |
| 388 return firstParent; | 391 return firstParent; |
| 389 } | 392 } |
| 390 | 393 |
| 391 #pragma mark - Segregation of nodes by time. | 394 #pragma mark - Segregation of nodes by time. |
| 392 | 395 |
| 393 NodesSection::NodesSection() {} | 396 NodesSection::NodesSection() {} |
| 394 | 397 |
| 395 NodesSection::~NodesSection() {} | 398 NodesSection::~NodesSection() {} |
| 396 | 399 |
| 397 // Sorts NodesSection by their time. | 400 void segregateNodes( |
| 398 class NodesSectionComparator : public std::binary_function<const NodesSection*, | 401 const NodeVector& vector, |
| 399 const NodesSection*, | 402 std::vector<std::unique_ptr<NodesSection>>& nodesSectionVector) { |
| 400 bool> { | |
| 401 public: | |
| 402 // Returns true if |n1| preceeds |n2|. | |
| 403 bool operator()(const NodesSection* n1, const NodesSection* n2) { | |
| 404 return n1->time > n2->time; | |
| 405 } | |
| 406 }; | |
| 407 | |
| 408 // Sorts bookmark nodes by their creation time. | |
| 409 class NodeCreationComparator : public std::binary_function<const BookmarkNode*, | |
| 410 const BookmarkNode*, | |
| 411 bool> { | |
| 412 public: | |
| 413 // Returns true if |n1| preceeds |n2|. | |
| 414 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) { | |
| 415 return n1->date_added() > n2->date_added(); | |
| 416 } | |
| 417 }; | |
| 418 | |
| 419 void segregateNodes(const NodeVector& vector, | |
| 420 ScopedVector<NodesSection>& nodesSectionVector) { | |
| 421 nodesSectionVector.clear(); | 403 nodesSectionVector.clear(); |
| 422 | 404 |
| 423 // Make a localized date formatter. | 405 // Make a localized date formatter. |
| 424 base::scoped_nsobject<NSDateFormatter> formatter( | 406 base::scoped_nsobject<NSDateFormatter> formatter( |
| 425 [[NSDateFormatter alloc] init]); | 407 [[NSDateFormatter alloc] init]); |
| 426 [formatter setDateFormat:@"MMMM yyyy"]; | 408 [formatter setDateFormat:@"MMMM yyyy"]; |
| 427 // Segregate nodes by creation date. | 409 // Segregate nodes by creation date. |
| 428 // Nodes that were created in the same month are grouped together. | 410 // Nodes that were created in the same month are grouped together. |
| 429 for (auto node : vector) { | 411 for (auto node : vector) { |
| 430 base::mac::ScopedNSAutoreleasePool pool; | 412 base::mac::ScopedNSAutoreleasePool pool; |
| 431 base::Time dateAdded = node->date_added(); | 413 base::Time dateAdded = node->date_added(); |
| 432 base::TimeDelta delta = dateAdded - base::Time::UnixEpoch(); | 414 base::TimeDelta delta = dateAdded - base::Time::UnixEpoch(); |
| 433 base::scoped_nsobject<NSDate> date( | 415 base::scoped_nsobject<NSDate> date( |
| 434 [[NSDate alloc] initWithTimeIntervalSince1970:delta.InSeconds()]); | 416 [[NSDate alloc] initWithTimeIntervalSince1970:delta.InSeconds()]); |
| 435 NSString* dateString = [formatter stringFromDate:date]; | 417 NSString* dateString = [formatter stringFromDate:date]; |
| 436 const std::string timeRepresentation = base::SysNSStringToUTF8(dateString); | 418 const std::string timeRepresentation = base::SysNSStringToUTF8(dateString); |
| 437 | 419 |
| 438 BOOL found = NO; | 420 BOOL found = NO; |
| 439 for (NodesSection* nodesSection : nodesSectionVector) { | 421 for (const auto& nodesSection : nodesSectionVector) { |
| 440 if (nodesSection->timeRepresentation == timeRepresentation) { | 422 if (nodesSection->timeRepresentation == timeRepresentation) { |
| 441 nodesSection->vector.push_back(node); | 423 nodesSection->vector.push_back(node); |
| 442 found = YES; | 424 found = YES; |
| 443 break; | 425 break; |
| 444 } | 426 } |
| 445 } | 427 } |
| 446 | 428 |
| 447 if (found) | 429 if (found) |
| 448 continue; | 430 continue; |
| 449 | 431 |
| 450 // No NodesSection found. | 432 // No NodesSection found. |
| 451 NodesSection* nodesSection = new NodesSection; | 433 auto nodesSection = base::MakeUnique<NodesSection>(); |
| 452 nodesSection->time = dateAdded; | 434 nodesSection->time = dateAdded; |
| 453 nodesSection->timeRepresentation = timeRepresentation; | 435 nodesSection->timeRepresentation = timeRepresentation; |
| 454 nodesSection->vector.push_back(node); | 436 nodesSection->vector.push_back(node); |
| 455 nodesSectionVector.push_back(nodesSection); | 437 nodesSectionVector.push_back(std::move(nodesSection)); |
| 456 } | 438 } |
| 457 | 439 |
| 458 // Sort the NodesSections. | 440 // Sort the NodesSections. |
| 459 std::sort(nodesSectionVector.begin(), nodesSectionVector.end(), | 441 std::sort(nodesSectionVector.begin(), nodesSectionVector.end(), |
| 460 NodesSectionComparator()); | 442 [](const std::unique_ptr<NodesSection>& n1, |
| 443 const std::unique_ptr<NodesSection>& n2) { |
| 444 return n1->time > n2->time; |
| 445 }); |
| 461 | 446 |
| 462 // For each NodesSection, sort the nodes inside. | 447 // For each NodesSection, sort the nodes inside. |
| 463 for (NodesSection* nodesSection : nodesSectionVector) | 448 for (const auto& nodesSection : nodesSectionVector) { |
| 464 std::sort(nodesSection->vector.begin(), nodesSection->vector.end(), | 449 std::sort(nodesSection->vector.begin(), nodesSection->vector.end(), |
| 465 NodeCreationComparator()); | 450 [](const BookmarkNode* n1, const BookmarkNode* n2) { |
| 451 return n1->date_added() > n2->date_added(); |
| 452 }); |
| 453 } |
| 466 } | 454 } |
| 467 | 455 |
| 468 #pragma mark - Useful bookmark manipulation. | 456 #pragma mark - Useful bookmark manipulation. |
| 469 | 457 |
| 470 // Adds all children of |folder| that are not obstructed to |results|. They are | 458 // Adds all children of |folder| that are not obstructed to |results|. They are |
| 471 // placed immediately after |folder|, using a depth-first, then alphabetically | 459 // placed immediately after |folder|, using a depth-first, then alphabetically |
| 472 // ordering. |results| must contain |folder|. | 460 // ordering. |results| must contain |folder|. |
| 473 void UpdateFoldersFromNode(const BookmarkNode* folder, | 461 void UpdateFoldersFromNode(const BookmarkNode* folder, |
| 474 NodeVector* results, | 462 NodeVector* results, |
| 475 const NodeSet& obstructions); | 463 const NodeSet& obstructions); |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 | 683 |
| 696 *position = cache.position; | 684 *position = cache.position; |
| 697 return YES; | 685 return YES; |
| 698 } | 686 } |
| 699 | 687 |
| 700 void ClearPositionCache() { | 688 void ClearPositionCache() { |
| 701 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kPositionCacheKey]; | 689 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kPositionCacheKey]; |
| 702 } | 690 } |
| 703 | 691 |
| 704 } // namespace bookmark_utils_ios | 692 } // namespace bookmark_utils_ios |
| OLD | NEW |