1 // Copyright 2014 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 "components/bookmarks/browser/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 "components/bookmarks/browser/bookmark_model.h"
11 #include "components/bookmarks/browser/bookmark_utils.h"
12 #include "components/bookmarks/common/bookmark_pref_names.h"
16 BookmarkExpandedStateTracker::BookmarkExpandedStateTracker(
17 BookmarkModel
* bookmark_model
,
18 PrefService
* pref_service
)
19 : bookmark_model_(bookmark_model
),
20 pref_service_(pref_service
) {
21 bookmark_model
->AddObserver(this);
24 BookmarkExpandedStateTracker::~BookmarkExpandedStateTracker() {
27 void BookmarkExpandedStateTracker::SetExpandedNodes(const Nodes
& nodes
) {
31 BookmarkExpandedStateTracker::Nodes
32 BookmarkExpandedStateTracker::GetExpandedNodes() {
34 if (!bookmark_model_
->loaded())
40 const base::ListValue
* value
=
41 pref_service_
->GetList(prefs::kBookmarkEditorExpandedNodes
);
46 for (base::ListValue::const_iterator i
= value
->begin();
47 i
!= value
->end(); ++i
) {
50 const BookmarkNode
* node
;
51 if ((*i
)->GetAsString(&value
) && base::StringToInt64(value
, &node_id
) &&
52 (node
= GetBookmarkNodeByID(bookmark_model_
, node_id
)) != NULL
&&
64 void BookmarkExpandedStateTracker::BookmarkModelLoaded(BookmarkModel
* model
,
65 bool ids_reassigned
) {
67 // If the ids change we can't trust the value in preferences and need to
69 SetExpandedNodes(Nodes());
73 void BookmarkExpandedStateTracker::BookmarkModelChanged() {
76 void BookmarkExpandedStateTracker::BookmarkModelBeingDeleted(
77 BookmarkModel
* model
) {
78 model
->RemoveObserver(this);
81 void BookmarkExpandedStateTracker::BookmarkNodeRemoved(
83 const BookmarkNode
* parent
,
85 const BookmarkNode
* node
,
86 const std::set
<GURL
>& removed_urls
) {
87 if (!node
->is_folder())
88 return; // Only care about folders.
90 // Ask for the nodes again, which removes any nodes that were deleted.
94 void BookmarkExpandedStateTracker::BookmarkAllUserNodesRemoved(
96 const std::set
<GURL
>& removed_urls
) {
97 // Ask for the nodes again, which removes any nodes that were deleted.
101 void BookmarkExpandedStateTracker::UpdatePrefs(const Nodes
& nodes
) {
105 base::ListValue values
;
106 for (Nodes::const_iterator i
= nodes
.begin(); i
!= nodes
.end(); ++i
) {
107 values
.Set(values
.GetSize(),
108 new base::StringValue(base::Int64ToString((*i
)->id())));
111 pref_service_
->Set(prefs::kBookmarkEditorExpandedNodes
, values
);
114 } // namespace bookmarks