OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/bookmarks/bookmark_expanded_state_tracker.h" | 5 #include "chrome/browser/bookmarks/bookmark_expanded_state_tracker.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/bookmarks/bookmark_model.h" | 9 #include "chrome/browser/bookmarks/bookmark_model.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 | 12 |
13 BookmarkExpandedStateTracker::BookmarkExpandedStateTracker(Profile* profile, | 13 BookmarkExpandedStateTracker::BookmarkExpandedStateTracker( |
14 const char* path) | 14 Profile* profile, |
| 15 const char* path, |
| 16 BookmarkModel* bookmark_model) |
15 : profile_(profile), | 17 : profile_(profile), |
16 pref_path_(path) { | 18 pref_path_(path) { |
17 profile_->GetBookmarkModel()->AddObserver(this); | 19 bookmark_model->AddObserver(this); |
18 } | 20 } |
19 | 21 |
20 BookmarkExpandedStateTracker::~BookmarkExpandedStateTracker() { | 22 BookmarkExpandedStateTracker::~BookmarkExpandedStateTracker() { |
21 profile_->GetBookmarkModel()->RemoveObserver(this); | |
22 } | 23 } |
23 | 24 |
24 void BookmarkExpandedStateTracker::SetExpandedNodes(const Nodes& nodes) { | 25 void BookmarkExpandedStateTracker::SetExpandedNodes(const Nodes& nodes) { |
25 UpdatePrefs(nodes); | 26 UpdatePrefs(nodes); |
26 } | 27 } |
27 | 28 |
28 BookmarkExpandedStateTracker::Nodes | 29 BookmarkExpandedStateTracker::Nodes |
29 BookmarkExpandedStateTracker::GetExpandedNodes() { | 30 BookmarkExpandedStateTracker::GetExpandedNodes() { |
30 Nodes nodes; | 31 Nodes nodes; |
31 BookmarkModel* model = profile_->GetBookmarkModel(); | 32 BookmarkModel* model = profile_->GetBookmarkModel(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // reset it. | 65 // reset it. |
65 SetExpandedNodes(Nodes()); | 66 SetExpandedNodes(Nodes()); |
66 } | 67 } |
67 } | 68 } |
68 | 69 |
69 void BookmarkExpandedStateTracker::BookmarkModelChanged() { | 70 void BookmarkExpandedStateTracker::BookmarkModelChanged() { |
70 } | 71 } |
71 | 72 |
72 void BookmarkExpandedStateTracker::BookmarkModelBeingDeleted( | 73 void BookmarkExpandedStateTracker::BookmarkModelBeingDeleted( |
73 BookmarkModel* model) { | 74 BookmarkModel* model) { |
| 75 model->RemoveObserver(this); |
74 } | 76 } |
75 | 77 |
76 void BookmarkExpandedStateTracker::BookmarkNodeRemoved( | 78 void BookmarkExpandedStateTracker::BookmarkNodeRemoved( |
77 BookmarkModel* model, | 79 BookmarkModel* model, |
78 const BookmarkNode* parent, | 80 const BookmarkNode* parent, |
79 int old_index, | 81 int old_index, |
80 const BookmarkNode* node) { | 82 const BookmarkNode* node) { |
81 if (!node->is_folder()) | 83 if (!node->is_folder()) |
82 return; // Only care about folders. | 84 return; // Only care about folders. |
83 | 85 |
84 // Ask for the nodes again, which removes any nodes that were deleted. | 86 // Ask for the nodes again, which removes any nodes that were deleted. |
85 GetExpandedNodes(); | 87 GetExpandedNodes(); |
86 } | 88 } |
87 | 89 |
88 void BookmarkExpandedStateTracker::UpdatePrefs(const Nodes& nodes) { | 90 void BookmarkExpandedStateTracker::UpdatePrefs(const Nodes& nodes) { |
89 if (!profile_->GetPrefs()) | 91 if (!profile_->GetPrefs()) |
90 return; | 92 return; |
91 | 93 |
92 ListValue values; | 94 ListValue values; |
93 for (Nodes::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { | 95 for (Nodes::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { |
94 values.Set(values.GetSize(), | 96 values.Set(values.GetSize(), |
95 new StringValue(base::Int64ToString((*i)->id()))); | 97 new StringValue(base::Int64ToString((*i)->id()))); |
96 } | 98 } |
97 profile_->GetPrefs()->Set(pref_path_, values); | 99 profile_->GetPrefs()->Set(pref_path_, values); |
98 } | 100 } |
OLD | NEW |