1 // Copyright 2014 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 
5 #ifndef COMPONENTS_BOOKMARKS_MANAGED_MANAGED_BOOKMARKS_TRACKER_H_
6 #define COMPONENTS_BOOKMARKS_MANAGED_MANAGED_BOOKMARKS_TRACKER_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 
13 #include "base/callback_forward.h"
14 #include "base/macros.h"
15 #include "base/strings/string16.h"
16 #include "components/prefs/pref_change_registrar.h"
17 
18 class GURL;
19 class PrefService;
20 
21 namespace base {
22 class ListValue;
23 }
24 
25 namespace bookmarks {
26 
27 class BookmarkModel;
28 class BookmarkNode;
29 class BookmarkPermanentNode;
30 
31 // Tracks either the Managed Bookmarks pref (set by policy) and makes the
32 // managed_node() in the BookmarkModel follow the policy-defined bookmark tree.
33 class ManagedBookmarksTracker {
34  public:
35   using GetManagementDomainCallback = base::RepeatingCallback<std::string()>;
36 
37   // Shared constants used in the policy configuration.
38   static const char kName[];
39   static const char kUrl[];
40   static const char kChildren[];
41   static const char kFolderName[];
42 
43   ManagedBookmarksTracker(BookmarkModel* model,
44                           PrefService* prefs,
45                           GetManagementDomainCallback callback);
46   ~ManagedBookmarksTracker();
47 
48   // Returns the initial list of managed bookmarks, which can be passed to
49   // LoadInitial() to do the initial load.
50   std::unique_ptr<base::ListValue> GetInitialManagedBookmarks();
51 
52   // Loads the initial managed bookmarks in |list| into |folder|.
53   // New nodes will be assigned IDs starting at |next_node_id|.
54   // Returns the next node ID to use.
55   static int64_t LoadInitial(BookmarkNode* folder,
56                              const base::ListValue* list,
57                              int64_t next_node_id);
58 
59   // Starts tracking the pref for updates to the managed bookmarks.
60   // Should be called after loading the initial bookmarks.
61   void Init(BookmarkPermanentNode* managed_node);
62 
63  private:
64   base::string16 GetBookmarksFolderTitle() const;
65 
66   void ReloadManagedBookmarks();
67 
68   void UpdateBookmarks(const BookmarkNode* folder, const base::ListValue* list);
69   static bool LoadBookmark(const base::ListValue* list,
70                            size_t index,
71                            base::string16* title,
72                            GURL* url,
73                            const base::ListValue** children);
74 
75   BookmarkModel* model_;
76   BookmarkPermanentNode* managed_node_;
77   PrefService* prefs_;
78   PrefChangeRegistrar registrar_;
79   GetManagementDomainCallback get_management_domain_callback_;
80 
81   DISALLOW_COPY_AND_ASSIGN(ManagedBookmarksTracker);
82 };
83 
84 }  // namespace bookmarks
85 
86 #endif  // COMPONENTS_BOOKMARKS_MANAGED_MANAGED_BOOKMARKS_TRACKER_H_
87 
88