Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_expanded_state_tracker.cc
blobf64fd693da9174cbfa7208c424b58e149e7ed5db
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) {
26 UpdatePrefs(nodes);
29 BookmarkExpandedStateTracker::Nodes
30 BookmarkExpandedStateTracker::GetExpandedNodes() {
31 Nodes nodes;
32 if (!bookmark_model_->loaded())
33 return nodes;
35 if (!pref_service_)
36 return nodes;
38 const base::ListValue* value =
39 pref_service_->GetList(prefs::kBookmarkEditorExpandedNodes);
40 if (!value)
41 return nodes;
43 bool changed = false;
44 for (base::ListValue::const_iterator i = value->begin();
45 i != value->end(); ++i) {
46 std::string value;
47 int64 node_id;
48 const BookmarkNode* node;
49 if ((*i)->GetAsString(&value) && base::StringToInt64(value, &node_id) &&
50 (node = bookmark_model_->GetNodeByID(node_id)) != NULL &&
51 node->is_folder()) {
52 nodes.insert(node);
53 } else {
54 changed = true;
57 if (changed)
58 UpdatePrefs(nodes);
59 return nodes;
62 void BookmarkExpandedStateTracker::BookmarkModelLoaded(BookmarkModel* model,
63 bool ids_reassigned) {
64 if (ids_reassigned) {
65 // If the ids change we can't trust the value in preferences and need to
66 // reset it.
67 SetExpandedNodes(Nodes());
71 void BookmarkExpandedStateTracker::BookmarkModelChanged() {
74 void BookmarkExpandedStateTracker::BookmarkModelBeingDeleted(
75 BookmarkModel* model) {
76 model->RemoveObserver(this);
79 void BookmarkExpandedStateTracker::BookmarkNodeRemoved(
80 BookmarkModel* model,
81 const BookmarkNode* parent,
82 int old_index,
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.
88 GetExpandedNodes();
91 void BookmarkExpandedStateTracker::BookmarkAllNodesRemoved(
92 BookmarkModel* model) {
93 // Ask for the nodes again, which removes any nodes that were deleted.
94 GetExpandedNodes();
97 void BookmarkExpandedStateTracker::UpdatePrefs(const Nodes& nodes) {
98 if (!pref_service_)
99 return;
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);