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 #include "chrome/browser/android/bookmarks/partner_bookmarks_shim.h"
7 #include "base/lazy_instance.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/pref_names.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/pref_registry/pref_registry_syncable.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
17 using bookmarks::BookmarkNode
;
18 using content::BrowserThread
;
22 // PartnerModelKeeper is used as a singleton to store an immutable hierarchy
23 // of partner bookmarks. The hierarchy is retrieved from the partner bookmarks
24 // provider and doesn't depend on the user profile.
25 // The retrieved hierarchy persists
26 // PartnerBookmarksShim is responsible to applying and storing the user changes
27 // (deletions/renames) in the user profile, thus keeping the hierarchy intact.
28 struct PartnerModelKeeper
{
29 scoped_ptr
<BookmarkNode
> partner_bookmarks_root
;
36 base::LazyInstance
<PartnerModelKeeper
> g_partner_model_keeper
=
37 LAZY_INSTANCE_INITIALIZER
;
39 const void* kPartnerBookmarksShimUserDataKey
=
40 &kPartnerBookmarksShimUserDataKey
;
42 // Dictionary keys for entries in the kPartnerBookmarksMapping pref.
43 static const char kMappingUrl
[] = "url";
44 static const char kMappingProviderTitle
[] = "provider_title";
45 static const char kMappingTitle
[] = "mapped_title";
47 static bool g_disable_partner_bookmarks_editing
= false;
52 PartnerBookmarksShim
* PartnerBookmarksShim::BuildForBrowserContext(
53 content::BrowserContext
* browser_context
) {
54 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
56 PartnerBookmarksShim
* data
=
57 static_cast<PartnerBookmarksShim
*>(
58 browser_context
->GetUserData(kPartnerBookmarksShimUserDataKey
));
62 data
= new PartnerBookmarksShim(
63 Profile::FromBrowserContext(browser_context
)->GetPrefs());
64 browser_context
->SetUserData(kPartnerBookmarksShimUserDataKey
, data
);
65 data
->ReloadNodeMapping();
70 void PartnerBookmarksShim::RegisterProfilePrefs(
71 user_prefs::PrefRegistrySyncable
* registry
) {
72 registry
->RegisterListPref(prefs::kPartnerBookmarkMappings
);
76 void PartnerBookmarksShim::DisablePartnerBookmarksEditing() {
77 g_disable_partner_bookmarks_editing
= true;
80 bool PartnerBookmarksShim::IsLoaded() const {
81 return g_partner_model_keeper
.Get().loaded
;
84 bool PartnerBookmarksShim::HasPartnerBookmarks() const {
86 return g_partner_model_keeper
.Get().partner_bookmarks_root
.get() != NULL
;
89 bool PartnerBookmarksShim::IsReachable(const BookmarkNode
* node
) const {
90 DCHECK(IsPartnerBookmark(node
));
91 if (!HasPartnerBookmarks())
93 if (!g_disable_partner_bookmarks_editing
) {
94 for (const BookmarkNode
* i
= node
; i
!= NULL
; i
= i
->parent()) {
95 const NodeRenamingMapKey
key(i
->url(), i
->GetTitle());
96 NodeRenamingMap::const_iterator remap
= node_rename_remove_map_
.find(key
);
97 if (remap
!= node_rename_remove_map_
.end() && remap
->second
.empty())
104 bool PartnerBookmarksShim::IsEditable(const BookmarkNode
* node
) const {
105 DCHECK(IsPartnerBookmark(node
));
106 if (!HasPartnerBookmarks())
108 if (g_disable_partner_bookmarks_editing
)
113 void PartnerBookmarksShim::RemoveBookmark(const BookmarkNode
* node
) {
114 DCHECK(IsEditable(node
));
115 RenameBookmark(node
, base::string16());
118 void PartnerBookmarksShim::RenameBookmark(const BookmarkNode
* node
,
119 const base::string16
& title
) {
120 DCHECK(IsEditable(node
));
121 const NodeRenamingMapKey
key(node
->url(), node
->GetTitle());
122 node_rename_remove_map_
[key
] = title
;
124 FOR_EACH_OBSERVER(PartnerBookmarksShim::Observer
, observers_
,
125 PartnerShimChanged(this));
128 void PartnerBookmarksShim::AddObserver(
129 PartnerBookmarksShim::Observer
* observer
) {
130 observers_
.AddObserver(observer
);
133 void PartnerBookmarksShim::RemoveObserver(
134 PartnerBookmarksShim::Observer
* observer
) {
135 observers_
.RemoveObserver(observer
);
138 const BookmarkNode
* PartnerBookmarksShim::GetNodeByID(int64 id
) const {
140 if (!HasPartnerBookmarks())
142 return GetNodeByID(GetPartnerBookmarksRoot(), id
);
145 base::string16
PartnerBookmarksShim::GetTitle(const BookmarkNode
* node
) const {
147 DCHECK(IsPartnerBookmark(node
));
149 if (!g_disable_partner_bookmarks_editing
) {
150 const NodeRenamingMapKey
key(node
->url(), node
->GetTitle());
151 NodeRenamingMap::const_iterator i
= node_rename_remove_map_
.find(key
);
152 if (i
!= node_rename_remove_map_
.end())
156 return node
->GetTitle();
159 bool PartnerBookmarksShim::IsPartnerBookmark(const BookmarkNode
* node
) const {
161 if (!HasPartnerBookmarks())
163 const BookmarkNode
* parent
= node
;
165 if (parent
== GetPartnerBookmarksRoot())
167 parent
= parent
->parent();
172 const BookmarkNode
* PartnerBookmarksShim::GetPartnerBookmarksRoot() const {
173 return g_partner_model_keeper
.Get().partner_bookmarks_root
.get();
176 void PartnerBookmarksShim::SetPartnerBookmarksRoot(BookmarkNode
* root_node
) {
177 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
178 g_partner_model_keeper
.Get().partner_bookmarks_root
.reset(root_node
);
179 g_partner_model_keeper
.Get().loaded
= true;
180 FOR_EACH_OBSERVER(PartnerBookmarksShim::Observer
, observers_
,
181 PartnerShimLoaded(this));
184 PartnerBookmarksShim::NodeRenamingMapKey::NodeRenamingMapKey(
185 const GURL
& url
, const base::string16
& provider_title
)
186 : url_(url
), provider_title_(provider_title
) {}
188 PartnerBookmarksShim::NodeRenamingMapKey::~NodeRenamingMapKey() {}
190 bool operator<(const PartnerBookmarksShim::NodeRenamingMapKey
& a
,
191 const PartnerBookmarksShim::NodeRenamingMapKey
& b
) {
192 return (a
.url_
< b
.url_
) ||
193 (a
.url_
== b
.url_
&& a
.provider_title_
< b
.provider_title_
);
197 void PartnerBookmarksShim::ClearInBrowserContextForTesting(
198 content::BrowserContext
* browser_context
) {
199 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
200 browser_context
->SetUserData(kPartnerBookmarksShimUserDataKey
, 0);
204 void PartnerBookmarksShim::ClearPartnerModelForTesting() {
205 g_partner_model_keeper
.Get().loaded
= false;
206 g_partner_model_keeper
.Get().partner_bookmarks_root
.reset(0);
210 void PartnerBookmarksShim::EnablePartnerBookmarksEditing() {
211 g_disable_partner_bookmarks_editing
= false;
214 PartnerBookmarksShim::PartnerBookmarksShim(PrefService
* prefs
)
216 observers_(base::ObserverList
<
217 PartnerBookmarksShim::Observer
>::NOTIFY_EXISTING_ONLY
) {
220 PartnerBookmarksShim::~PartnerBookmarksShim() {
221 FOR_EACH_OBSERVER(PartnerBookmarksShim::Observer
, observers_
,
222 ShimBeingDeleted(this));
225 const BookmarkNode
* PartnerBookmarksShim::GetNodeByID(
226 const BookmarkNode
* parent
, int64 id
) const {
227 if (parent
->id() == id
)
229 for (int i
= 0, child_count
= parent
->child_count(); i
< child_count
; ++i
) {
230 const BookmarkNode
* result
= GetNodeByID(parent
->GetChild(i
), id
);
237 void PartnerBookmarksShim::ReloadNodeMapping() {
238 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
240 node_rename_remove_map_
.clear();
244 const base::ListValue
* list
=
245 prefs_
->GetList(prefs::kPartnerBookmarkMappings
);
249 for (base::ListValue::const_iterator it
= list
->begin();
250 it
!= list
->end(); ++it
) {
251 const base::DictionaryValue
* dict
= NULL
;
252 if (!*it
|| !(*it
)->GetAsDictionary(&dict
)) {
258 base::string16 provider_title
;
259 base::string16 mapped_title
;
260 if (!dict
->GetString(kMappingUrl
, &url
) ||
261 !dict
->GetString(kMappingProviderTitle
, &provider_title
) ||
262 !dict
->GetString(kMappingTitle
, &mapped_title
)) {
267 const NodeRenamingMapKey
key(GURL(url
), provider_title
);
268 node_rename_remove_map_
[key
] = mapped_title
;
272 void PartnerBookmarksShim::SaveNodeMapping() {
273 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
277 base::ListValue list
;
278 for (NodeRenamingMap::const_iterator i
= node_rename_remove_map_
.begin();
279 i
!= node_rename_remove_map_
.end();
281 base::DictionaryValue
* dict
= new base::DictionaryValue();
282 dict
->SetString(kMappingUrl
, i
->first
.url().spec());
283 dict
->SetString(kMappingProviderTitle
, i
->first
.provider_title());
284 dict
->SetString(kMappingTitle
, i
->second
);
287 prefs_
->Set(prefs::kPartnerBookmarkMappings
, list
);