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"
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
{
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
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
*) {}
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
{
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
);
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();
124 explicit PartnerBookmarksShim(PrefService
* prefs
);
125 ~PartnerBookmarksShim() override
;
127 const bookmarks::BookmarkNode
* GetNodeByID(
128 const bookmarks::BookmarkNode
* parent
,
130 void ReloadNodeMapping();
131 void SaveNodeMapping();
133 scoped_ptr
<bookmarks::BookmarkNode
> partner_bookmarks_root_
;
135 NodeRenamingMap node_rename_remove_map_
;
138 base::ObserverList
<Observer
> observers_
;
140 DISALLOW_COPY_AND_ASSIGN(PartnerBookmarksShim
);
143 #endif // CHROME_BROWSER_ANDROID_BOOKMARKS_PARTNER_BOOKMARKS_SHIM_H_