Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / android / bookmarks / partner_bookmarks_shim.cc
blob5a4d75574e0ddc1b478ca8e2956b9e7f5aa17c7e
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;
20 namespace {
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;
30 bool loaded;
32 PartnerModelKeeper()
33 : loaded(false) {}
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;
49 } // namespace
51 // static
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));
59 if (data)
60 return data;
62 data = new PartnerBookmarksShim(
63 Profile::FromBrowserContext(browser_context)->GetPrefs());
64 browser_context->SetUserData(kPartnerBookmarksShimUserDataKey, data);
65 data->ReloadNodeMapping();
66 return data;
69 // static
70 void PartnerBookmarksShim::RegisterProfilePrefs(
71 user_prefs::PrefRegistrySyncable* registry) {
72 registry->RegisterListPref(prefs::kPartnerBookmarkMappings);
75 // static
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 {
85 DCHECK(IsLoaded());
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())
92 return false;
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())
98 return false;
101 return true;
104 bool PartnerBookmarksShim::IsEditable(const BookmarkNode* node) const {
105 DCHECK(IsPartnerBookmark(node));
106 if (!HasPartnerBookmarks())
107 return false;
108 if (g_disable_partner_bookmarks_editing)
109 return false;
110 return true;
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;
123 SaveNodeMapping();
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 {
139 DCHECK(IsLoaded());
140 if (!HasPartnerBookmarks())
141 return NULL;
142 return GetNodeByID(GetPartnerBookmarksRoot(), id);
145 base::string16 PartnerBookmarksShim::GetTitle(const BookmarkNode* node) const {
146 DCHECK(node);
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())
153 return i->second;
156 return node->GetTitle();
159 bool PartnerBookmarksShim::IsPartnerBookmark(const BookmarkNode* node) const {
160 DCHECK(IsLoaded());
161 if (!HasPartnerBookmarks())
162 return false;
163 const BookmarkNode* parent = node;
164 while (parent) {
165 if (parent == GetPartnerBookmarksRoot())
166 return true;
167 parent = parent->parent();
169 return false;
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_);
196 // static
197 void PartnerBookmarksShim::ClearInBrowserContextForTesting(
198 content::BrowserContext* browser_context) {
199 DCHECK_CURRENTLY_ON(BrowserThread::UI);
200 browser_context->SetUserData(kPartnerBookmarksShimUserDataKey, 0);
203 // static
204 void PartnerBookmarksShim::ClearPartnerModelForTesting() {
205 g_partner_model_keeper.Get().loaded = false;
206 g_partner_model_keeper.Get().partner_bookmarks_root.reset(0);
209 // static
210 void PartnerBookmarksShim::EnablePartnerBookmarksEditing() {
211 g_disable_partner_bookmarks_editing = false;
214 PartnerBookmarksShim::PartnerBookmarksShim(PrefService* prefs)
215 : prefs_(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)
228 return parent;
229 for (int i = 0, child_count = parent->child_count(); i < child_count; ++i) {
230 const BookmarkNode* result = GetNodeByID(parent->GetChild(i), id);
231 if (result)
232 return result;
234 return NULL;
237 void PartnerBookmarksShim::ReloadNodeMapping() {
238 DCHECK_CURRENTLY_ON(BrowserThread::UI);
240 node_rename_remove_map_.clear();
241 if (!prefs_)
242 return;
244 const base::ListValue* list =
245 prefs_->GetList(prefs::kPartnerBookmarkMappings);
246 if (!list)
247 return;
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)) {
253 NOTREACHED();
254 continue;
257 std::string url;
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)) {
263 NOTREACHED();
264 continue;
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);
274 if (!prefs_)
275 return;
277 base::ListValue list;
278 for (NodeRenamingMap::const_iterator i = node_rename_remove_map_.begin();
279 i != node_rename_remove_map_.end();
280 ++i) {
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);
285 list.Append(dict);
287 prefs_->Set(prefs::kPartnerBookmarkMappings, list);