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