blob: 11facbfd414606d97b2f991d48606f62f3198597 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/bookmarks/bookmark_expanded_state_tracker.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/user_prefs.h"
BookmarkExpandedStateTracker::BookmarkExpandedStateTracker(
content::BrowserContext* browser_context,
BookmarkModel* bookmark_model)
: browser_context_(browser_context),
bookmark_model_(bookmark_model) {
bookmark_model->AddObserver(this);
}
BookmarkExpandedStateTracker::~BookmarkExpandedStateTracker() {
}
void BookmarkExpandedStateTracker::SetExpandedNodes(const Nodes& nodes) {
UpdatePrefs(nodes);
}
BookmarkExpandedStateTracker::Nodes
BookmarkExpandedStateTracker::GetExpandedNodes() {
Nodes nodes;
if (!bookmark_model_->loaded())
return nodes;
PrefService* prefs = user_prefs::UserPrefs::Get(browser_context_);
if (!prefs)
return nodes;
const ListValue* value = prefs->GetList(prefs::kBookmarkEditorExpandedNodes);
if (!value)
return nodes;
bool changed = false;
for (ListValue::const_iterator i = value->begin(); i != value->end(); ++i) {
std::string value;
int64 node_id;
const BookmarkNode* node;
if ((*i)->GetAsString(&value) && base::StringToInt64(value, &node_id) &&
(node = bookmark_model_->GetNodeByID(node_id)) != NULL &&
node->is_folder()) {
nodes.insert(node);
} else {
changed = true;
}
}
if (changed)
UpdatePrefs(nodes);
return nodes;
}
void BookmarkExpandedStateTracker::Loaded(BookmarkModel* model,
bool ids_reassigned) {
if (ids_reassigned) {
// If the ids change we can't trust the value in preferences and need to
// reset it.
SetExpandedNodes(Nodes());
}
}
void BookmarkExpandedStateTracker::BookmarkModelChanged() {
}
void BookmarkExpandedStateTracker::BookmarkModelBeingDeleted(
BookmarkModel* model) {
model->RemoveObserver(this);
}
void BookmarkExpandedStateTracker::BookmarkNodeRemoved(
BookmarkModel* model,
const BookmarkNode* parent,
int old_index,
const BookmarkNode* node) {
if (!node->is_folder())
return; // Only care about folders.
// Ask for the nodes again, which removes any nodes that were deleted.
GetExpandedNodes();
}
void BookmarkExpandedStateTracker::BookmarkAllNodesRemoved(
BookmarkModel* model) {
// Ask for the nodes again, which removes any nodes that were deleted.
GetExpandedNodes();
}
void BookmarkExpandedStateTracker::UpdatePrefs(const Nodes& nodes) {
PrefService* prefs = user_prefs::UserPrefs::Get(browser_context_);
if (!prefs)
return;
ListValue values;
for (Nodes::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
values.Set(values.GetSize(),
new StringValue(base::Int64ToString((*i)->id())));
}
prefs->Set(prefs::kBookmarkEditorExpandedNodes, values);
}
|