closure: fix compile error by adding missing externs
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_expanded_state_tracker.cc
blob4b4f04c07df1690553b537a65831fddc31da52eb
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"
14 namespace bookmarks {
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) {
28 UpdatePrefs(nodes);
31 BookmarkExpandedStateTracker::Nodes
32 BookmarkExpandedStateTracker::GetExpandedNodes() {
33 Nodes nodes;
34 if (!bookmark_model_->loaded())
35 return nodes;
37 if (!pref_service_)
38 return nodes;
40 const base::ListValue* value =
41 pref_service_->GetList(prefs::kBookmarkEditorExpandedNodes);
42 if (!value)
43 return nodes;
45 bool changed = false;
46 for (base::ListValue::const_iterator i = value->begin();
47 i != value->end(); ++i) {
48 std::string value;
49 int64 node_id;
50 const BookmarkNode* node;
51 if ((*i)->GetAsString(&value) && base::StringToInt64(value, &node_id) &&
52 (node = GetBookmarkNodeByID(bookmark_model_, node_id)) != NULL &&
53 node->is_folder()) {
54 nodes.insert(node);
55 } else {
56 changed = true;
59 if (changed)
60 UpdatePrefs(nodes);
61 return nodes;
64 void BookmarkExpandedStateTracker::BookmarkModelLoaded(BookmarkModel* model,
65 bool ids_reassigned) {
66 if (ids_reassigned) {
67 // If the ids change we can't trust the value in preferences and need to
68 // reset it.
69 SetExpandedNodes(Nodes());
73 void BookmarkExpandedStateTracker::BookmarkModelChanged() {
76 void BookmarkExpandedStateTracker::BookmarkModelBeingDeleted(
77 BookmarkModel* model) {
78 model->RemoveObserver(this);
81 void BookmarkExpandedStateTracker::BookmarkNodeRemoved(
82 BookmarkModel* model,
83 const BookmarkNode* parent,
84 int old_index,
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.
91 GetExpandedNodes();
94 void BookmarkExpandedStateTracker::BookmarkAllUserNodesRemoved(
95 BookmarkModel* model,
96 const std::set<GURL>& removed_urls) {
97 // Ask for the nodes again, which removes any nodes that were deleted.
98 GetExpandedNodes();
101 void BookmarkExpandedStateTracker::UpdatePrefs(const Nodes& nodes) {
102 if (!pref_service_)
103 return;
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