Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / android / bookmarks / partner_bookmarks_shim.h
blobbc78106b71969dd92826fdc845df98393d2cdef0
1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_ANDROID_BOOKMARKS_PARTNER_BOOKMARKS_SHIM_H_
6 #define CHROME_BROWSER_ANDROID_BOOKMARKS_PARTNER_BOOKMARKS_SHIM_H_
8 #include "base/android/jni_weak_ref.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string16.h"
11 #include "base/supports_user_data.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "url/gurl.h"
15 class PrefService;
17 namespace content {
18 class BrowserContext;
19 class WebContents;
22 namespace user_prefs {
23 class PrefRegistrySyncable;
26 // A shim that lives on top of a BookmarkModel that allows the injection of
27 // partner bookmarks without submitting changes to the bookmark model.
28 // The shim persists bookmark renames/deletions in a user profile and could be
29 // queried via shim->GetTitle(node) and shim->IsReachable(node).
30 // Note that node->GetTitle() returns an original (unmodified) title.
31 class PartnerBookmarksShim : public base::SupportsUserData::Data {
32 public:
33 // Returns an instance of the shim for a given |browser_context|.
34 static PartnerBookmarksShim* BuildForBrowserContext(
35 content::BrowserContext* browser_context);
37 // Registers preferences.
38 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
40 // Disables the editing and stops any edits from being applied.
41 // The user will start to see the original (unedited) partner bookmarks.
42 // Edits are stored in the user profile, so once the editing is enabled
43 // ("not disabled") the user would see the edited partner bookmarks.
44 // This method should be called as early as possible: it does NOT send any
45 // notifications to already existing shims.
46 static void DisablePartnerBookmarksEditing();
48 // Returns true if everything got loaded.
49 bool IsLoaded() const;
51 // Returns true if there are partner bookmarks.
52 bool HasPartnerBookmarks() const;
54 // Returns true if a given bookmark is reachable (i.e. neither the bookmark,
55 // nor any of its parents were "removed").
56 bool IsReachable(const bookmarks::BookmarkNode* node) const;
58 // Returns true if a given node is editable and if editing is allowed.
59 bool IsEditable(const bookmarks::BookmarkNode* node) const;
61 // Removes a given bookmark.
62 // Makes the |node| (and, consequently, all its children) unreachable.
63 void RemoveBookmark(const bookmarks::BookmarkNode* node);
65 // Renames a given bookmark.
66 void RenameBookmark(const bookmarks::BookmarkNode* node,
67 const base::string16& title);
69 // For Loaded/Changed/ShimBeingDeleted notifications
70 class Observer {
71 public:
72 // Called when the set of bookmarks, or their values/visibility changes
73 virtual void PartnerShimChanged(PartnerBookmarksShim*) {}
74 // Called when everything is loaded
75 virtual void PartnerShimLoaded(PartnerBookmarksShim*) {}
76 // Called just before everything got destroyed
77 virtual void ShimBeingDeleted(PartnerBookmarksShim*) {}
78 protected:
79 virtual ~Observer() {}
82 void AddObserver(Observer* observer);
83 void RemoveObserver(Observer* observer);
85 // PartnerBookmarksShim versions of BookmarkModel/BookmarkNode methods
86 const bookmarks::BookmarkNode* GetNodeByID(int64 id) const;
87 base::string16 GetTitle(const bookmarks::BookmarkNode* node) const;
89 bool IsPartnerBookmark(const bookmarks::BookmarkNode* node) const;
90 const bookmarks::BookmarkNode* GetPartnerBookmarksRoot() const;
92 // Sets the root node of the partner bookmarks and notifies any observers that
93 // the shim has now been loaded. Takes ownership of |root_node|.
94 void SetPartnerBookmarksRoot(bookmarks::BookmarkNode* root_node);
96 // Used as a "unique" identifier of the partner bookmark node for the purposes
97 // of node deletion and title editing. Two bookmarks with the same URLs and
98 // titles are considered indistinguishable.
99 class NodeRenamingMapKey {
100 public:
101 NodeRenamingMapKey(const GURL& url, const base::string16& provider_title);
102 ~NodeRenamingMapKey();
103 const GURL& url() const { return url_; }
104 const base::string16& provider_title() const { return provider_title_; }
105 friend bool operator<(const NodeRenamingMapKey& a,
106 const NodeRenamingMapKey& b);
107 private:
108 GURL url_;
109 base::string16 provider_title_;
111 typedef std::map<NodeRenamingMapKey, base::string16> NodeRenamingMap;
113 // For testing: clears an instance of the shim in a given |browser_context|.
114 static void ClearInBrowserContextForTesting(
115 content::BrowserContext* browser_context);
117 // For testing: clears partner bookmark model data.
118 static void ClearPartnerModelForTesting();
120 // For testing: re-enables partner bookmarks editing.
121 static void EnablePartnerBookmarksEditing();
123 private:
124 explicit PartnerBookmarksShim(PrefService* prefs);
125 ~PartnerBookmarksShim() override;
127 const bookmarks::BookmarkNode* GetNodeByID(
128 const bookmarks::BookmarkNode* parent,
129 int64 id) const;
130 void ReloadNodeMapping();
131 void SaveNodeMapping();
133 scoped_ptr<bookmarks::BookmarkNode> partner_bookmarks_root_;
134 PrefService* prefs_;
135 NodeRenamingMap node_rename_remove_map_;
137 // The observers.
138 base::ObserverList<Observer> observers_;
140 DISALLOW_COPY_AND_ASSIGN(PartnerBookmarksShim);
143 #endif // CHROME_BROWSER_ANDROID_BOOKMARKS_PARTNER_BOOKMARKS_SHIM_H_