bookmark_load_details.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/bookmarks/browser/bookmark_load_details.h"
  5. #include "base/guid.h"
  6. #include "components/bookmarks/browser/bookmark_client.h"
  7. #include "components/bookmarks/browser/titled_url_index.h"
  8. #include "components/bookmarks/browser/url_index.h"
  9. namespace bookmarks {
  10. BookmarkLoadDetails::BookmarkLoadDetails(BookmarkClient* client)
  11. : load_managed_node_callback_(client->GetLoadManagedNodeCallback()),
  12. index_(std::make_unique<TitledUrlIndex>()),
  13. load_start_(base::TimeTicks::Now()) {
  14. // WARNING: do NOT add |client| as a member. Much of this code runs on another
  15. // thread, and |client_| is not thread safe, and/or may be destroyed before
  16. // this.
  17. root_node_ = std::make_unique<BookmarkNode>(
  18. /*id=*/0, base::GUID::ParseLowercase(BookmarkNode::kRootNodeGuid),
  19. GURL());
  20. root_node_ptr_ = root_node_.get();
  21. // WARNING: order is important here, various places assume the order is
  22. // constant (but can vary between embedders with the initial visibility
  23. // of permanent nodes).
  24. bb_node_ = static_cast<BookmarkPermanentNode*>(
  25. root_node_->Add(BookmarkPermanentNode::CreateBookmarkBar(
  26. max_id_++, client->IsPermanentNodeVisibleWhenEmpty(
  27. BookmarkNode::BOOKMARK_BAR))));
  28. other_folder_node_ = static_cast<BookmarkPermanentNode*>(
  29. root_node_->Add(BookmarkPermanentNode::CreateOtherBookmarks(
  30. max_id_++,
  31. client->IsPermanentNodeVisibleWhenEmpty(BookmarkNode::OTHER_NODE))));
  32. mobile_folder_node_ = static_cast<BookmarkPermanentNode*>(
  33. root_node_->Add(BookmarkPermanentNode::CreateMobileBookmarks(
  34. max_id_++,
  35. client->IsPermanentNodeVisibleWhenEmpty(BookmarkNode::MOBILE))));
  36. }
  37. BookmarkLoadDetails::~BookmarkLoadDetails() = default;
  38. bool BookmarkLoadDetails::LoadManagedNode() {
  39. if (!load_managed_node_callback_)
  40. return false;
  41. std::unique_ptr<BookmarkPermanentNode> managed_node =
  42. std::move(load_managed_node_callback_).Run(&max_id_);
  43. if (!managed_node)
  44. return false;
  45. bool has_children = !managed_node->children().empty();
  46. root_node_->Add(std::move(managed_node));
  47. return has_children;
  48. }
  49. void BookmarkLoadDetails::CreateUrlIndex() {
  50. url_index_ = base::MakeRefCounted<UrlIndex>(std::move(root_node_));
  51. }
  52. } // namespace bookmarks