OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/chrome/browser/bookmarks/bookmarks_utils.h" | 5 #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "components/bookmarks/browser/bookmark_model.h" | 10 #include "components/bookmarks/browser/bookmark_model.h" |
11 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" | 11 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
12 #include "ios/chrome/browser/pref_names.h" | 12 #include "ios/chrome/browser/pref_names.h" |
13 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.
h" | 13 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.
h" |
14 | 14 |
| 15 using bookmarks::BookmarkModel; |
| 16 using bookmarks::BookmarkNode; |
| 17 |
15 void RecordBookmarkLaunch(BookmarkLaunchLocation launch_location) { | 18 void RecordBookmarkLaunch(BookmarkLaunchLocation launch_location) { |
16 DCHECK(launch_location < BOOKMARK_LAUNCH_LOCATION_COUNT); | 19 DCHECK(launch_location < BOOKMARK_LAUNCH_LOCATION_COUNT); |
17 UMA_HISTOGRAM_ENUMERATION("Stars.LaunchLocation", launch_location, | 20 UMA_HISTOGRAM_ENUMERATION("Stars.LaunchLocation", launch_location, |
18 BOOKMARK_LAUNCH_LOCATION_COUNT); | 21 BOOKMARK_LAUNCH_LOCATION_COUNT); |
19 } | 22 } |
20 | 23 |
21 bool RemoveAllUserBookmarksIOS(ios::ChromeBrowserState* browser_state) { | 24 bool RemoveAllUserBookmarksIOS(ios::ChromeBrowserState* browser_state) { |
22 bookmarks::BookmarkModel* bookmark_model = | 25 BookmarkModel* bookmark_model = |
23 ios::BookmarkModelFactory::GetForBrowserState(browser_state); | 26 ios::BookmarkModelFactory::GetForBrowserState(browser_state); |
24 | 27 |
25 if (!bookmark_model->loaded()) | 28 if (!bookmark_model->loaded()) |
26 return false; | 29 return false; |
27 | 30 |
28 bookmark_model->RemoveAllUserBookmarks(); | 31 bookmark_model->RemoveAllUserBookmarks(); |
29 | 32 |
30 for (int i = 0; i < bookmark_model->root_node()->child_count(); ++i) { | 33 for (int i = 0; i < bookmark_model->root_node()->child_count(); ++i) { |
31 if (!bookmark_model->client()->CanBeEditedByUser( | 34 if (!bookmark_model->client()->CanBeEditedByUser( |
32 bookmark_model->root_node()->GetChild(i))) | 35 bookmark_model->root_node()->GetChild(i))) |
33 continue; | 36 continue; |
34 CHECK(bookmark_model->root_node()->GetChild(i)->empty()) | 37 CHECK(bookmark_model->root_node()->GetChild(i)->empty()) |
35 << "Failed to remove all user bookmarks."; | 38 << "Failed to remove all user bookmarks."; |
36 } | 39 } |
37 | 40 |
38 // The default save folder is reset to the generic one. | 41 // The default save folder is reset to the generic one. |
39 browser_state->GetPrefs()->SetInt64(prefs::kIosBookmarkFolderDefault, -1); | 42 browser_state->GetPrefs()->SetInt64(prefs::kIosBookmarkFolderDefault, -1); |
40 | 43 |
41 return true; | 44 return true; |
42 } | 45 } |
| 46 |
| 47 std::vector<const BookmarkNode*> PrimaryPermanentNodes(BookmarkModel* model) { |
| 48 DCHECK(model->loaded()); |
| 49 std::vector<const BookmarkNode*> nodes; |
| 50 nodes.push_back(model->mobile_node()); |
| 51 nodes.push_back(model->bookmark_bar_node()); |
| 52 nodes.push_back(model->other_node()); |
| 53 return nodes; |
| 54 } |
| 55 |
| 56 std::vector<const BookmarkNode*> RootLevelFolders(BookmarkModel* model) { |
| 57 std::vector<const BookmarkNode*> root_level_folders; |
| 58 |
| 59 // Find the direct folder children of the primary permanent nodes. |
| 60 std::vector<const BookmarkNode*> primary_permanent_nodes = |
| 61 PrimaryPermanentNodes(model); |
| 62 for (const BookmarkNode* parent : primary_permanent_nodes) { |
| 63 int child_count = parent->child_count(); |
| 64 for (int i = 0; i < child_count; ++i) { |
| 65 const BookmarkNode* node = parent->GetChild(i); |
| 66 if (node->is_folder() && node->IsVisible()) |
| 67 root_level_folders.push_back(node); |
| 68 } |
| 69 } |
| 70 return root_level_folders; |
| 71 } |
| 72 |
| 73 bool IsPrimaryPermanentNode(const BookmarkNode* node, BookmarkModel* model) { |
| 74 std::vector<const BookmarkNode*> primary_nodes(PrimaryPermanentNodes(model)); |
| 75 if (std::find(primary_nodes.begin(), primary_nodes.end(), node) != |
| 76 primary_nodes.end()) { |
| 77 return true; |
| 78 } |
| 79 return false; |
| 80 } |
| 81 |
| 82 const BookmarkNode* RootLevelFolderForNode(const BookmarkNode* node, |
| 83 BookmarkModel* model) { |
| 84 // This helper function doesn't work for managed bookmarks. This checks that |
| 85 // |node| is editable by the user, which currently covers all the other |
| 86 // bookmarks except the managed bookmarks. |
| 87 DCHECK(model->client()->CanBeEditedByUser(node)); |
| 88 |
| 89 const std::vector<const BookmarkNode*> root_folders(RootLevelFolders(model)); |
| 90 const BookmarkNode* top = node; |
| 91 while (top && |
| 92 std::find(root_folders.begin(), root_folders.end(), top) == |
| 93 root_folders.end()) { |
| 94 top = top->parent(); |
| 95 } |
| 96 return top; |
| 97 } |
OLD | NEW |