1 // Copyright (c) 2012 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/bookmarks/bookmark_expanded_state_tracker.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/values.h"
10 #include "chrome/browser/bookmarks/bookmark_model.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/common/pref_names.h"
14 BookmarkExpandedStateTracker::BookmarkExpandedStateTracker(
15 BookmarkModel
* bookmark_model
,
16 PrefService
* pref_service
)
17 : bookmark_model_(bookmark_model
),
18 pref_service_(pref_service
) {
19 bookmark_model
->AddObserver(this);
22 BookmarkExpandedStateTracker::~BookmarkExpandedStateTracker() {
25 void BookmarkExpandedStateTracker::SetExpandedNodes(const Nodes
& nodes
) {
29 BookmarkExpandedStateTracker::Nodes
30 BookmarkExpandedStateTracker::GetExpandedNodes() {
32 if (!bookmark_model_
->loaded())
38 const base::ListValue
* value
=
39 pref_service_
->GetList(prefs::kBookmarkEditorExpandedNodes
);
44 for (base::ListValue::const_iterator i
= value
->begin();
45 i
!= value
->end(); ++i
) {
48 const BookmarkNode
* node
;
49 if ((*i
)->GetAsString(&value
) && base::StringToInt64(value
, &node_id
) &&
50 (node
= bookmark_model_
->GetNodeByID(node_id
)) != NULL
&&
62 void BookmarkExpandedStateTracker::BookmarkModelLoaded(BookmarkModel
* model
,
63 bool ids_reassigned
) {
65 // If the ids change we can't trust the value in preferences and need to
67 SetExpandedNodes(Nodes());
71 void BookmarkExpandedStateTracker::BookmarkModelChanged() {
74 void BookmarkExpandedStateTracker::BookmarkModelBeingDeleted(
75 BookmarkModel
* model
) {
76 model
->RemoveObserver(this);
79 void BookmarkExpandedStateTracker::BookmarkNodeRemoved(
81 const BookmarkNode
* parent
,
83 const BookmarkNode
* node
) {
84 if (!node
->is_folder())
85 return; // Only care about folders.
87 // Ask for the nodes again, which removes any nodes that were deleted.
91 void BookmarkExpandedStateTracker::BookmarkAllNodesRemoved(
92 BookmarkModel
* model
) {
93 // Ask for the nodes again, which removes any nodes that were deleted.
97 void BookmarkExpandedStateTracker::UpdatePrefs(const Nodes
& nodes
) {
101 base::ListValue values
;
102 for (Nodes::const_iterator i
= nodes
.begin(); i
!= nodes
.end(); ++i
) {
103 values
.Set(values
.GetSize(),
104 new base::StringValue(base::Int64ToString((*i
)->id())));
107 pref_service_
->Set(prefs::kBookmarkEditorExpandedNodes
, values
);