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