OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/undo/bookmark_undo_service.h" | 5 #include "components/undo/bookmark_undo_service.h" |
6 | 6 |
7 #include "components/bookmarks/browser/bookmark_model.h" | |
8 #include "components/bookmarks/browser/bookmark_node_data.h" | 7 #include "components/bookmarks/browser/bookmark_node_data.h" |
9 #include "components/bookmarks/browser/bookmark_utils.h" | 8 #include "components/bookmarks/browser/bookmark_utils.h" |
10 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" | 9 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" |
11 #include "components/undo/bookmark_renumber_observer.h" | |
12 #include "components/undo/undo_operation.h" | 10 #include "components/undo/undo_operation.h" |
13 #include "grit/components_strings.h" | 11 #include "grit/components_strings.h" |
14 | 12 |
15 using bookmarks::BookmarkModel; | 13 using bookmarks::BookmarkModel; |
16 using bookmarks::BookmarkNode; | 14 using bookmarks::BookmarkNode; |
17 using bookmarks::BookmarkNodeData; | 15 using bookmarks::BookmarkNodeData; |
18 | 16 |
19 namespace { | 17 namespace { |
20 | 18 |
21 // BookmarkUndoOperation ------------------------------------------------------ | 19 // BookmarkUndoOperation ------------------------------------------------------ |
22 | 20 |
23 // Base class for all bookmark related UndoOperations that facilitates access to | 21 // Base class for all bookmark related UndoOperations that facilitates access to |
24 // the BookmarkUndoService. | 22 // the BookmarkUndoService. |
25 class BookmarkUndoOperation : public UndoOperation, | 23 class BookmarkUndoOperation : public UndoOperation { |
26 public BookmarkRenumberObserver { | |
27 public: | 24 public: |
28 BookmarkUndoOperation(BookmarkModel* bookmark_model, | 25 explicit BookmarkUndoOperation(BookmarkModel* bookmark_model) |
29 BookmarkRenumberObserver* undo_renumber_observer) | 26 : bookmark_model_(bookmark_model) {} |
30 : bookmark_model_(bookmark_model), | |
31 undo_renumber_observer_(undo_renumber_observer) {} | |
32 ~BookmarkUndoOperation() override {} | 27 ~BookmarkUndoOperation() override {} |
33 | 28 |
34 BookmarkModel* bookmark_model() { return bookmark_model_; } | 29 BookmarkModel* bookmark_model() { return bookmark_model_; } |
35 | 30 |
36 BookmarkRenumberObserver* undo_renumber_observer() { | |
37 return undo_renumber_observer_; | |
38 } | |
39 | |
40 private: | 31 private: |
41 BookmarkModel* bookmark_model_; | 32 BookmarkModel* bookmark_model_; |
42 BookmarkRenumberObserver* undo_renumber_observer_; | |
43 }; | 33 }; |
44 | 34 |
45 // BookmarkAddOperation ------------------------------------------------------- | 35 // BookmarkAddOperation ------------------------------------------------------- |
46 | 36 |
47 // Handles the undo of the insertion of a bookmark or folder. | 37 // Handles the undo of the insertion of a bookmark or folder. |
48 class BookmarkAddOperation : public BookmarkUndoOperation { | 38 class BookmarkAddOperation : public BookmarkUndoOperation { |
49 public: | 39 public: |
50 BookmarkAddOperation(BookmarkModel* bookmark_model, | 40 BookmarkAddOperation(BookmarkModel* bookmark_model, |
51 BookmarkRenumberObserver* undo_renumber_observer, | |
52 const BookmarkNode* parent, | 41 const BookmarkNode* parent, |
53 int index); | 42 int index); |
54 ~BookmarkAddOperation() override {} | 43 ~BookmarkAddOperation() override {} |
55 | 44 |
56 // UndoOperation: | 45 // UndoOperation: |
57 void Undo() override; | 46 void Undo() override; |
58 int GetUndoLabelId() const override; | 47 int GetUndoLabelId() const override; |
59 int GetRedoLabelId() const override; | 48 int GetRedoLabelId() const override; |
60 | 49 |
61 // BookmarkRenumberObserver: | |
62 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
63 | |
64 private: | 50 private: |
65 int64 parent_id_; | 51 int64 parent_id_; |
66 const int index_; | 52 const int index_; |
67 | 53 |
68 DISALLOW_COPY_AND_ASSIGN(BookmarkAddOperation); | 54 DISALLOW_COPY_AND_ASSIGN(BookmarkAddOperation); |
69 }; | 55 }; |
70 | 56 |
71 BookmarkAddOperation::BookmarkAddOperation( | 57 BookmarkAddOperation::BookmarkAddOperation( |
72 BookmarkModel* bookmark_model, | 58 BookmarkModel* bookmark_model, |
73 BookmarkRenumberObserver* undo_renumber_observer, | |
74 const BookmarkNode* parent, | 59 const BookmarkNode* parent, |
75 int index) | 60 int index) |
76 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 61 : BookmarkUndoOperation(bookmark_model), |
77 parent_id_(parent->id()), | 62 parent_id_(parent->id()), |
78 index_(index) { | 63 index_(index) { |
79 } | 64 } |
80 | 65 |
81 void BookmarkAddOperation::Undo() { | 66 void BookmarkAddOperation::Undo() { |
82 BookmarkModel* model = bookmark_model(); | 67 BookmarkModel* model = bookmark_model(); |
83 const BookmarkNode* parent = | 68 const BookmarkNode* parent = |
84 bookmarks::GetBookmarkNodeByID(model, parent_id_); | 69 bookmarks::GetBookmarkNodeByID(model, parent_id_); |
85 DCHECK(parent); | 70 DCHECK(parent); |
86 | 71 |
87 model->Remove(parent->GetChild(index_)); | 72 model->Remove(parent->GetChild(index_)); |
88 } | 73 } |
89 | 74 |
90 int BookmarkAddOperation::GetUndoLabelId() const { | 75 int BookmarkAddOperation::GetUndoLabelId() const { |
91 return IDS_BOOKMARK_BAR_UNDO_ADD; | 76 return IDS_BOOKMARK_BAR_UNDO_ADD; |
92 } | 77 } |
93 | 78 |
94 int BookmarkAddOperation::GetRedoLabelId() const { | 79 int BookmarkAddOperation::GetRedoLabelId() const { |
95 return IDS_BOOKMARK_BAR_REDO_DELETE; | 80 return IDS_BOOKMARK_BAR_REDO_DELETE; |
96 } | 81 } |
97 | 82 |
98 void BookmarkAddOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | |
99 if (parent_id_ == old_id) | |
100 parent_id_ = new_id; | |
101 } | |
102 | |
103 // BookmarkRemoveOperation ---------------------------------------------------- | 83 // BookmarkRemoveOperation ---------------------------------------------------- |
104 | 84 |
105 // Handles the undo of the deletion of a bookmark node. For a bookmark folder, | 85 // Handles the undo of the deletion of a bookmark node. For a bookmark folder, |
106 // the information for all descendant bookmark nodes is maintained. | 86 // the information for all descendant bookmark nodes is maintained. |
107 // | 87 // |
108 // The BookmarkModel allows only single bookmark node to be removed. | 88 // The BookmarkModel allows only single bookmark node to be removed. |
109 class BookmarkRemoveOperation : public BookmarkUndoOperation { | 89 class BookmarkRemoveOperation : public BookmarkUndoOperation { |
110 public: | 90 public: |
111 BookmarkRemoveOperation(BookmarkModel* bookmark_model, | 91 BookmarkRemoveOperation(BookmarkModel* bookmark_model, |
112 BookmarkRenumberObserver* undo_renumber_observer, | 92 int64_t parent_node_id, |
113 const BookmarkNode* parent, | 93 int index, |
114 int old_index, | 94 BookmarkNode* node); |
115 const BookmarkNode* node); | 95 ~BookmarkRemoveOperation() override; |
116 ~BookmarkRemoveOperation() override {} | |
117 | 96 |
118 // UndoOperation: | 97 // UndoOperation: |
119 void Undo() override; | 98 void Undo() override; |
120 int GetUndoLabelId() const override; | 99 int GetUndoLabelId() const override; |
121 int GetRedoLabelId() const override; | 100 int GetRedoLabelId() const override; |
122 | 101 |
123 // BookmarkRenumberObserver: | |
124 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
125 | |
126 private: | 102 private: |
127 void UpdateBookmarkIds(const BookmarkNodeData::Element& element, | 103 int64_t parent_node_id_; |
128 const BookmarkNode* parent, | 104 int index_; |
129 int index_added_at); | 105 scoped_ptr<BookmarkNode> node_; |
130 | |
131 int64 parent_id_; | |
132 const int old_index_; | |
133 BookmarkNodeData removed_node_; | |
134 | 106 |
135 DISALLOW_COPY_AND_ASSIGN(BookmarkRemoveOperation); | 107 DISALLOW_COPY_AND_ASSIGN(BookmarkRemoveOperation); |
136 }; | 108 }; |
137 | 109 |
138 BookmarkRemoveOperation::BookmarkRemoveOperation( | 110 BookmarkRemoveOperation::BookmarkRemoveOperation( |
139 BookmarkModel* bookmark_model, | 111 BookmarkModel* bookmark_model, |
140 BookmarkRenumberObserver* undo_renumber_observer, | 112 int64_t parent_node_id, |
141 const BookmarkNode* parent, | 113 int index, |
142 int old_index, | 114 BookmarkNode* node) |
143 const BookmarkNode* node) | 115 : BookmarkUndoOperation(bookmark_model), |
144 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 116 parent_node_id_(parent_node_id), |
145 parent_id_(parent->id()), | 117 index_(index), |
146 old_index_(old_index), | 118 node_(node) { |
147 removed_node_(node) { | 119 } |
| 120 |
| 121 BookmarkRemoveOperation::~BookmarkRemoveOperation() { |
148 } | 122 } |
149 | 123 |
150 void BookmarkRemoveOperation::Undo() { | 124 void BookmarkRemoveOperation::Undo() { |
151 DCHECK(removed_node_.is_valid()); | 125 DCHECK(node_.get()); |
152 BookmarkModel* model = bookmark_model(); | |
153 const BookmarkNode* parent = | |
154 bookmarks::GetBookmarkNodeByID(model, parent_id_); | |
155 DCHECK(parent); | |
156 | 126 |
157 bookmarks::CloneBookmarkNode( | 127 const BookmarkNode* parent = bookmarks::GetBookmarkNodeByID( |
158 model, removed_node_.elements, parent, old_index_, false); | 128 bookmark_model(), parent_node_id_); |
159 UpdateBookmarkIds(removed_node_.elements[0], parent, old_index_); | 129 if (!parent) |
| 130 return; |
| 131 bookmark_model()->AddNode( |
| 132 const_cast<BookmarkNode*>(parent), index_, node_.release()); |
160 } | 133 } |
161 | 134 |
162 int BookmarkRemoveOperation::GetUndoLabelId() const { | 135 int BookmarkRemoveOperation::GetUndoLabelId() const { |
163 return IDS_BOOKMARK_BAR_UNDO_DELETE; | 136 return IDS_BOOKMARK_BAR_UNDO_DELETE; |
164 } | 137 } |
165 | 138 |
166 int BookmarkRemoveOperation::GetRedoLabelId() const { | 139 int BookmarkRemoveOperation::GetRedoLabelId() const { |
167 return IDS_BOOKMARK_BAR_REDO_ADD; | 140 return IDS_BOOKMARK_BAR_REDO_ADD; |
168 } | 141 } |
169 | 142 |
170 void BookmarkRemoveOperation::UpdateBookmarkIds( | |
171 const BookmarkNodeData::Element& element, | |
172 const BookmarkNode* parent, | |
173 int index_added_at) { | |
174 const BookmarkNode* node = parent->GetChild(index_added_at); | |
175 if (element.id() != node->id()) | |
176 undo_renumber_observer()->OnBookmarkRenumbered(element.id(), node->id()); | |
177 if (!element.is_url) { | |
178 for (int i = 0; i < static_cast<int>(element.children.size()); ++i) | |
179 UpdateBookmarkIds(element.children[i], node, i); | |
180 } | |
181 } | |
182 | |
183 void BookmarkRemoveOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | |
184 if (parent_id_ == old_id) | |
185 parent_id_ = new_id; | |
186 } | |
187 | |
188 // BookmarkEditOperation ------------------------------------------------------ | 143 // BookmarkEditOperation ------------------------------------------------------ |
189 | 144 |
190 // Handles the undo of the modification of a bookmark node. | 145 // Handles the undo of the modification of a bookmark node. |
191 class BookmarkEditOperation : public BookmarkUndoOperation { | 146 class BookmarkEditOperation : public BookmarkUndoOperation { |
192 public: | 147 public: |
193 BookmarkEditOperation(BookmarkModel* bookmark_model, | 148 BookmarkEditOperation(BookmarkModel* bookmark_model, |
194 BookmarkRenumberObserver* undo_renumber_observer, | |
195 const BookmarkNode* node); | 149 const BookmarkNode* node); |
196 ~BookmarkEditOperation() override {} | 150 ~BookmarkEditOperation() override {} |
197 | 151 |
198 // UndoOperation: | 152 // UndoOperation: |
199 void Undo() override; | 153 void Undo() override; |
200 int GetUndoLabelId() const override; | 154 int GetUndoLabelId() const override; |
201 int GetRedoLabelId() const override; | 155 int GetRedoLabelId() const override; |
202 | 156 |
203 // BookmarkRenumberObserver: | |
204 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
205 | |
206 private: | 157 private: |
207 int64 node_id_; | 158 int64 node_id_; |
208 BookmarkNodeData original_bookmark_; | 159 BookmarkNodeData original_bookmark_; |
209 | 160 |
210 DISALLOW_COPY_AND_ASSIGN(BookmarkEditOperation); | 161 DISALLOW_COPY_AND_ASSIGN(BookmarkEditOperation); |
211 }; | 162 }; |
212 | 163 |
213 BookmarkEditOperation::BookmarkEditOperation( | 164 BookmarkEditOperation::BookmarkEditOperation( |
214 BookmarkModel* bookmark_model, | 165 BookmarkModel* bookmark_model, |
215 BookmarkRenumberObserver* undo_renumber_observer, | |
216 const BookmarkNode* node) | 166 const BookmarkNode* node) |
217 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 167 : BookmarkUndoOperation(bookmark_model), |
218 node_id_(node->id()), | 168 node_id_(node->id()), |
219 original_bookmark_(node) { | 169 original_bookmark_(node) { |
220 } | 170 } |
221 | 171 |
222 void BookmarkEditOperation::Undo() { | 172 void BookmarkEditOperation::Undo() { |
223 DCHECK(original_bookmark_.is_valid()); | 173 DCHECK(original_bookmark_.is_valid()); |
224 BookmarkModel* model = bookmark_model(); | 174 BookmarkModel* model = bookmark_model(); |
225 const BookmarkNode* node = bookmarks::GetBookmarkNodeByID(model, node_id_); | 175 const BookmarkNode* node = bookmarks::GetBookmarkNodeByID(model, node_id_); |
226 DCHECK(node); | 176 DCHECK(node); |
227 | 177 |
228 model->SetTitle(node, original_bookmark_.elements[0].title); | 178 model->SetTitle(node, original_bookmark_.elements[0].title); |
229 if (original_bookmark_.elements[0].is_url) | 179 if (original_bookmark_.elements[0].is_url) |
230 model->SetURL(node, original_bookmark_.elements[0].url); | 180 model->SetURL(node, original_bookmark_.elements[0].url); |
231 } | 181 } |
232 | 182 |
233 int BookmarkEditOperation::GetUndoLabelId() const { | 183 int BookmarkEditOperation::GetUndoLabelId() const { |
234 return IDS_BOOKMARK_BAR_UNDO_EDIT; | 184 return IDS_BOOKMARK_BAR_UNDO_EDIT; |
235 } | 185 } |
236 | 186 |
237 int BookmarkEditOperation::GetRedoLabelId() const { | 187 int BookmarkEditOperation::GetRedoLabelId() const { |
238 return IDS_BOOKMARK_BAR_REDO_EDIT; | 188 return IDS_BOOKMARK_BAR_REDO_EDIT; |
239 } | 189 } |
240 | 190 |
241 void BookmarkEditOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | |
242 if (node_id_ == old_id) | |
243 node_id_ = new_id; | |
244 } | |
245 | |
246 // BookmarkMoveOperation ------------------------------------------------------ | 191 // BookmarkMoveOperation ------------------------------------------------------ |
247 | 192 |
248 // Handles the undo of a bookmark being moved to a new location. | 193 // Handles the undo of a bookmark being moved to a new location. |
249 class BookmarkMoveOperation : public BookmarkUndoOperation { | 194 class BookmarkMoveOperation : public BookmarkUndoOperation { |
250 public: | 195 public: |
251 BookmarkMoveOperation(BookmarkModel* bookmark_model, | 196 BookmarkMoveOperation(BookmarkModel* bookmark_model, |
252 BookmarkRenumberObserver* undo_renumber_observer, | |
253 const BookmarkNode* old_parent, | 197 const BookmarkNode* old_parent, |
254 int old_index, | 198 int old_index, |
255 const BookmarkNode* new_parent, | 199 const BookmarkNode* new_parent, |
256 int new_index); | 200 int new_index); |
257 ~BookmarkMoveOperation() override {} | 201 ~BookmarkMoveOperation() override {} |
258 int GetUndoLabelId() const override; | 202 int GetUndoLabelId() const override; |
259 int GetRedoLabelId() const override; | 203 int GetRedoLabelId() const override; |
260 | 204 |
261 // UndoOperation: | 205 // UndoOperation: |
262 void Undo() override; | 206 void Undo() override; |
263 | 207 |
264 // BookmarkRenumberObserver: | |
265 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
266 | |
267 private: | 208 private: |
268 int64 old_parent_id_; | 209 int64 old_parent_id_; |
269 int64 new_parent_id_; | 210 int64 new_parent_id_; |
270 int old_index_; | 211 int old_index_; |
271 int new_index_; | 212 int new_index_; |
272 | 213 |
273 DISALLOW_COPY_AND_ASSIGN(BookmarkMoveOperation); | 214 DISALLOW_COPY_AND_ASSIGN(BookmarkMoveOperation); |
274 }; | 215 }; |
275 | 216 |
276 BookmarkMoveOperation::BookmarkMoveOperation( | 217 BookmarkMoveOperation::BookmarkMoveOperation( |
277 BookmarkModel* bookmark_model, | 218 BookmarkModel* bookmark_model, |
278 BookmarkRenumberObserver* undo_renumber_observer, | |
279 const BookmarkNode* old_parent, | 219 const BookmarkNode* old_parent, |
280 int old_index, | 220 int old_index, |
281 const BookmarkNode* new_parent, | 221 const BookmarkNode* new_parent, |
282 int new_index) | 222 int new_index) |
283 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 223 : BookmarkUndoOperation(bookmark_model), |
284 old_parent_id_(old_parent->id()), | 224 old_parent_id_(old_parent->id()), |
285 new_parent_id_(new_parent->id()), | 225 new_parent_id_(new_parent->id()), |
286 old_index_(old_index), | 226 old_index_(old_index), |
287 new_index_(new_index) { | 227 new_index_(new_index) { |
288 } | 228 } |
289 | 229 |
290 void BookmarkMoveOperation::Undo() { | 230 void BookmarkMoveOperation::Undo() { |
291 BookmarkModel* model = bookmark_model(); | 231 BookmarkModel* model = bookmark_model(); |
292 const BookmarkNode* old_parent = | 232 const BookmarkNode* old_parent = |
293 bookmarks::GetBookmarkNodeByID(model, old_parent_id_); | 233 bookmarks::GetBookmarkNodeByID(model, old_parent_id_); |
(...skipping 15 matching lines...) Expand all Loading... |
309 } | 249 } |
310 | 250 |
311 int BookmarkMoveOperation::GetUndoLabelId() const { | 251 int BookmarkMoveOperation::GetUndoLabelId() const { |
312 return IDS_BOOKMARK_BAR_UNDO_MOVE; | 252 return IDS_BOOKMARK_BAR_UNDO_MOVE; |
313 } | 253 } |
314 | 254 |
315 int BookmarkMoveOperation::GetRedoLabelId() const { | 255 int BookmarkMoveOperation::GetRedoLabelId() const { |
316 return IDS_BOOKMARK_BAR_REDO_MOVE; | 256 return IDS_BOOKMARK_BAR_REDO_MOVE; |
317 } | 257 } |
318 | 258 |
319 void BookmarkMoveOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | |
320 if (old_parent_id_ == old_id) | |
321 old_parent_id_ = new_id; | |
322 if (new_parent_id_ == old_id) | |
323 new_parent_id_ = new_id; | |
324 } | |
325 | |
326 // BookmarkReorderOperation --------------------------------------------------- | 259 // BookmarkReorderOperation --------------------------------------------------- |
327 | 260 |
328 // Handle the undo of reordering of bookmarks that can happen as a result of | 261 // Handle the undo of reordering of bookmarks that can happen as a result of |
329 // sorting a bookmark folder by name or the undo of that operation. The change | 262 // sorting a bookmark folder by name or the undo of that operation. The change |
330 // of order is not recursive so only the order of the immediate children of the | 263 // of order is not recursive so only the order of the immediate children of the |
331 // folder need to be restored. | 264 // folder need to be restored. |
332 class BookmarkReorderOperation : public BookmarkUndoOperation { | 265 class BookmarkReorderOperation : public BookmarkUndoOperation { |
333 public: | 266 public: |
334 BookmarkReorderOperation(BookmarkModel* bookmark_model, | 267 BookmarkReorderOperation(BookmarkModel* bookmark_model, |
335 BookmarkRenumberObserver* undo_renumber_observer, | |
336 const BookmarkNode* parent); | 268 const BookmarkNode* parent); |
337 ~BookmarkReorderOperation() override; | 269 ~BookmarkReorderOperation() override; |
338 | 270 |
339 // UndoOperation: | 271 // UndoOperation: |
340 void Undo() override; | 272 void Undo() override; |
341 int GetUndoLabelId() const override; | 273 int GetUndoLabelId() const override; |
342 int GetRedoLabelId() const override; | 274 int GetRedoLabelId() const override; |
343 | 275 |
344 // BookmarkRenumberObserver: | |
345 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
346 | |
347 private: | 276 private: |
348 int64 parent_id_; | 277 int64 parent_id_; |
349 std::vector<int64> ordered_bookmarks_; | 278 std::vector<int64> ordered_bookmarks_; |
350 | 279 |
351 DISALLOW_COPY_AND_ASSIGN(BookmarkReorderOperation); | 280 DISALLOW_COPY_AND_ASSIGN(BookmarkReorderOperation); |
352 }; | 281 }; |
353 | 282 |
354 BookmarkReorderOperation::BookmarkReorderOperation( | 283 BookmarkReorderOperation::BookmarkReorderOperation( |
355 BookmarkModel* bookmark_model, | 284 BookmarkModel* bookmark_model, |
356 BookmarkRenumberObserver* undo_renumber_observer, | |
357 const BookmarkNode* parent) | 285 const BookmarkNode* parent) |
358 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 286 : BookmarkUndoOperation(bookmark_model), |
359 parent_id_(parent->id()) { | 287 parent_id_(parent->id()) { |
360 ordered_bookmarks_.resize(parent->child_count()); | 288 ordered_bookmarks_.resize(parent->child_count()); |
361 for (int i = 0; i < parent->child_count(); ++i) | 289 for (int i = 0; i < parent->child_count(); ++i) |
362 ordered_bookmarks_[i] = parent->GetChild(i)->id(); | 290 ordered_bookmarks_[i] = parent->GetChild(i)->id(); |
363 } | 291 } |
364 | 292 |
365 BookmarkReorderOperation::~BookmarkReorderOperation() { | 293 BookmarkReorderOperation::~BookmarkReorderOperation() { |
366 } | 294 } |
367 | 295 |
368 void BookmarkReorderOperation::Undo() { | 296 void BookmarkReorderOperation::Undo() { |
(...skipping 12 matching lines...) Expand all Loading... |
381 } | 309 } |
382 | 310 |
383 int BookmarkReorderOperation::GetUndoLabelId() const { | 311 int BookmarkReorderOperation::GetUndoLabelId() const { |
384 return IDS_BOOKMARK_BAR_UNDO_REORDER; | 312 return IDS_BOOKMARK_BAR_UNDO_REORDER; |
385 } | 313 } |
386 | 314 |
387 int BookmarkReorderOperation::GetRedoLabelId() const { | 315 int BookmarkReorderOperation::GetRedoLabelId() const { |
388 return IDS_BOOKMARK_BAR_REDO_REORDER; | 316 return IDS_BOOKMARK_BAR_REDO_REORDER; |
389 } | 317 } |
390 | 318 |
391 void BookmarkReorderOperation::OnBookmarkRenumbered(int64 old_id, | |
392 int64 new_id) { | |
393 if (parent_id_ == old_id) | |
394 parent_id_ = new_id; | |
395 for (size_t i = 0; i < ordered_bookmarks_.size(); ++i) { | |
396 if (ordered_bookmarks_[i] == old_id) | |
397 ordered_bookmarks_[i] = new_id; | |
398 } | |
399 } | |
400 | |
401 } // namespace | 319 } // namespace |
402 | 320 |
403 // BookmarkUndoService -------------------------------------------------------- | 321 // BookmarkUndoService -------------------------------------------------------- |
404 | 322 |
405 BookmarkUndoService::BookmarkUndoService() : scoped_observer_(this) { | 323 BookmarkUndoService::BookmarkUndoService() |
| 324 : model_(NULL), scoped_observer_(this) { |
406 } | 325 } |
407 | 326 |
408 BookmarkUndoService::~BookmarkUndoService() { | 327 BookmarkUndoService::~BookmarkUndoService() { |
409 } | 328 } |
410 | 329 |
411 void BookmarkUndoService::Start(BookmarkModel* model) { | 330 void BookmarkUndoService::Start(BookmarkModel* model) { |
| 331 DCHECK(!model_); |
| 332 model_ = model; |
412 scoped_observer_.Add(model); | 333 scoped_observer_.Add(model); |
| 334 model->set_undo_delegate(this); |
413 } | 335 } |
414 | 336 |
415 void BookmarkUndoService::Shutdown() { | 337 void BookmarkUndoService::Shutdown() { |
| 338 DCHECK(model_); |
416 scoped_observer_.RemoveAll(); | 339 scoped_observer_.RemoveAll(); |
| 340 model_->set_undo_delegate(nullptr); |
417 } | 341 } |
418 | 342 |
419 void BookmarkUndoService::BookmarkModelLoaded(BookmarkModel* model, | 343 void BookmarkUndoService::BookmarkModelLoaded(BookmarkModel* model, |
420 bool ids_reassigned) { | 344 bool ids_reassigned) { |
421 undo_manager_.RemoveAllOperations(); | 345 undo_manager_.RemoveAllOperations(); |
422 } | 346 } |
423 | 347 |
424 void BookmarkUndoService::BookmarkModelBeingDeleted(BookmarkModel* model) { | 348 void BookmarkUndoService::BookmarkModelBeingDeleted(BookmarkModel* model) { |
425 undo_manager_.RemoveAllOperations(); | 349 undo_manager_.RemoveAllOperations(); |
426 } | 350 } |
427 | 351 |
428 void BookmarkUndoService::BookmarkNodeMoved(BookmarkModel* model, | 352 void BookmarkUndoService::BookmarkNodeMoved(BookmarkModel* model, |
429 const BookmarkNode* old_parent, | 353 const BookmarkNode* old_parent, |
430 int old_index, | 354 int old_index, |
431 const BookmarkNode* new_parent, | 355 const BookmarkNode* new_parent, |
432 int new_index) { | 356 int new_index) { |
433 scoped_ptr<UndoOperation> op(new BookmarkMoveOperation( | 357 scoped_ptr<UndoOperation> op(new BookmarkMoveOperation( |
434 model, this, old_parent, old_index, new_parent, new_index)); | 358 model, old_parent, old_index, new_parent, new_index)); |
435 undo_manager()->AddUndoOperation(op.Pass()); | 359 undo_manager()->AddUndoOperation(op.Pass()); |
436 } | 360 } |
437 | 361 |
438 void BookmarkUndoService::BookmarkNodeAdded(BookmarkModel* model, | 362 void BookmarkUndoService::BookmarkNodeAdded(BookmarkModel* model, |
439 const BookmarkNode* parent, | 363 const BookmarkNode* parent, |
440 int index) { | 364 int index) { |
441 scoped_ptr<UndoOperation> op( | 365 scoped_ptr<UndoOperation> op( |
442 new BookmarkAddOperation(model, this, parent, index)); | 366 new BookmarkAddOperation(model, parent, index)); |
443 undo_manager()->AddUndoOperation(op.Pass()); | 367 undo_manager()->AddUndoOperation(op.Pass()); |
444 } | 368 } |
445 | 369 |
446 void BookmarkUndoService::OnWillRemoveBookmarks(BookmarkModel* model, | |
447 const BookmarkNode* parent, | |
448 int old_index, | |
449 const BookmarkNode* node) { | |
450 scoped_ptr<UndoOperation> op( | |
451 new BookmarkRemoveOperation(model, this, parent, old_index, node)); | |
452 undo_manager()->AddUndoOperation(op.Pass()); | |
453 } | |
454 | |
455 void BookmarkUndoService::OnWillRemoveAllUserBookmarks(BookmarkModel* model) { | |
456 bookmarks::ScopedGroupBookmarkActions merge_removes(model); | |
457 for (int i = 0; i < model->root_node()->child_count(); ++i) { | |
458 const BookmarkNode* permanent_node = model->root_node()->GetChild(i); | |
459 for (int j = permanent_node->child_count() - 1; j >= 0; --j) { | |
460 scoped_ptr<UndoOperation> op(new BookmarkRemoveOperation( | |
461 model, this, permanent_node, j, permanent_node->GetChild(j))); | |
462 undo_manager()->AddUndoOperation(op.Pass()); | |
463 } | |
464 } | |
465 } | |
466 | |
467 void BookmarkUndoService::OnWillChangeBookmarkNode(BookmarkModel* model, | 370 void BookmarkUndoService::OnWillChangeBookmarkNode(BookmarkModel* model, |
468 const BookmarkNode* node) { | 371 const BookmarkNode* node) { |
469 scoped_ptr<UndoOperation> op(new BookmarkEditOperation(model, this, node)); | 372 scoped_ptr<UndoOperation> op(new BookmarkEditOperation(model, node)); |
470 undo_manager()->AddUndoOperation(op.Pass()); | 373 undo_manager()->AddUndoOperation(op.Pass()); |
471 } | 374 } |
472 | 375 |
473 void BookmarkUndoService::OnWillReorderBookmarkNode(BookmarkModel* model, | 376 void BookmarkUndoService::OnWillReorderBookmarkNode(BookmarkModel* model, |
474 const BookmarkNode* node) { | 377 const BookmarkNode* node) { |
475 scoped_ptr<UndoOperation> op(new BookmarkReorderOperation(model, this, node)); | 378 scoped_ptr<UndoOperation> op(new BookmarkReorderOperation(model, node)); |
476 undo_manager()->AddUndoOperation(op.Pass()); | 379 undo_manager()->AddUndoOperation(op.Pass()); |
477 } | 380 } |
478 | 381 |
479 void BookmarkUndoService::GroupedBookmarkChangesBeginning( | 382 void BookmarkUndoService::GroupedBookmarkChangesBeginning( |
480 BookmarkModel* model) { | 383 BookmarkModel* model) { |
481 undo_manager()->StartGroupingActions(); | 384 undo_manager()->StartGroupingActions(); |
482 } | 385 } |
483 | 386 |
484 void BookmarkUndoService::GroupedBookmarkChangesEnded(BookmarkModel* model) { | 387 void BookmarkUndoService::GroupedBookmarkChangesEnded(BookmarkModel* model) { |
485 undo_manager()->EndGroupingActions(); | 388 undo_manager()->EndGroupingActions(); |
486 } | 389 } |
487 | 390 |
488 void BookmarkUndoService::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | 391 void BookmarkUndoService::OnBookmarkNodeDeleted(BookmarkModel* model, |
489 std::vector<UndoOperation*> all_operations = | 392 int64_t parent_node_id, |
490 undo_manager()->GetAllUndoOperations(); | 393 int index, |
491 for (UndoOperation* op : all_operations) { | 394 BookmarkNode* node) { |
492 static_cast<BookmarkUndoOperation*>(op) | 395 scoped_ptr<UndoOperation> op( |
493 ->OnBookmarkRenumbered(old_id, new_id); | 396 new BookmarkRemoveOperation(model, parent_node_id, index, node)); |
494 } | 397 undo_manager()->AddUndoOperation(op.Pass()); |
495 } | 398 } |
OLD | NEW |