OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // C++ bridge class to send a selector to a Cocoa object when the | 5 // C++ bridge class to send a selector to a Cocoa object when the |
6 // bookmark model changes. Some Cocoa objects edit the bookmark model | 6 // bookmark model changes. Some Cocoa objects edit the bookmark model |
7 // and temporarily save a copy of the state (e.g. bookmark button | 7 // and temporarily save a copy of the state (e.g. bookmark button |
8 // editor). As a fail-safe, these objects want an easy cancel if the | 8 // editor). As a fail-safe, these objects want an easy cancel if the |
9 // model changes out from under them. For example, if you have the | 9 // model changes out from under them. For example, if you have the |
10 // bookmark button editor sheet open, then edit the bookmark in the | 10 // bookmark button editor sheet open, then edit the bookmark in the |
11 // bookmark manager, we'd want to simply cancel the editor. | 11 // bookmark manager, we'd want to simply cancel the editor. |
12 // | 12 // |
13 // This class is conservative and may result in notifications which | 13 // This class is conservative and may result in notifications which |
14 // aren't strictly necessary. For example, node removal only needs to | 14 // aren't strictly necessary. For example, node removal only needs to |
15 // cancel an edit if the removed node is a folder (editors often have | 15 // cancel an edit if the removed node is a folder (editors often have |
16 // a list of "new parents"). But, just to be sure, notification | 16 // a list of "new parents"). But, just to be sure, notification |
17 // happens on any removal. | 17 // happens on any removal. |
18 | 18 |
19 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H | 19 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H |
20 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H | 20 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H |
21 | 21 |
22 #import <Cocoa/Cocoa.h> | 22 #import <Cocoa/Cocoa.h> |
23 | 23 |
| 24 #include <set> |
| 25 |
24 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
| 27 #include "base/mac/scoped_block.h" |
25 #include "base/memory/scoped_nsobject.h" | 28 #include "base/memory/scoped_nsobject.h" |
26 #include "chrome/browser/bookmarks/bookmark_model.h" | 29 #include "chrome/browser/bookmarks/bookmark_model.h" |
27 #include "chrome/browser/bookmarks/bookmark_model_observer.h" | 30 #include "chrome/browser/bookmarks/bookmark_model_observer.h" |
28 | 31 |
29 class BookmarkModelObserverForCocoa : public BookmarkModelObserver { | 32 class BookmarkModelObserverForCocoa : public BookmarkModelObserver { |
30 public: | 33 public: |
31 // When |node| in |model| changes, send |selector| to |object|. | 34 // Callback called on a significant model change. |nodeWasDeleted| will |
32 // Assumes |selector| is a selector that takes one arg, like an | 35 // be YES if an observed node was deleted in the change. |
33 // IBOutlet. The arg passed is nil. | 36 typedef void(^ChangeCallback)(BOOL nodeWasDeleted); |
34 // Many notifications happen independently of node | 37 |
35 // (e.g. BeingDeleted), so |node| can be nil. | 38 // When a |model| changes, or an observed node within it does, call a |
36 // | 39 // |callback|. |
37 // |object| is NOT retained, since the expected use case is for | 40 BookmarkModelObserverForCocoa(BookmarkModel* model, |
38 // ||object| to own the BookmarkModelObserverForCocoa and we don't | 41 ChangeCallback callback); |
39 // want a retain cycle. | |
40 BookmarkModelObserverForCocoa(const BookmarkNode* node, | |
41 BookmarkModel* model, | |
42 NSObject* object, | |
43 SEL selector); | |
44 virtual ~BookmarkModelObserverForCocoa(); | 42 virtual ~BookmarkModelObserverForCocoa(); |
45 | 43 |
| 44 // Starts and stops observing a specified |node|; the node must be contained |
| 45 // within the model. |
| 46 void StartObservingNode(const BookmarkNode* node); |
| 47 void StopObservingNode(const BookmarkNode* node); |
| 48 |
| 49 // BookmarkModelObserver: |
46 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; | 50 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; |
47 virtual void BookmarkNodeMoved(BookmarkModel* model, | 51 virtual void BookmarkNodeMoved(BookmarkModel* model, |
48 const BookmarkNode* old_parent, | 52 const BookmarkNode* old_parent, |
49 int old_index, | 53 int old_index, |
50 const BookmarkNode* new_parent, | 54 const BookmarkNode* new_parent, |
51 int new_index) OVERRIDE; | 55 int new_index) OVERRIDE; |
52 virtual void BookmarkNodeRemoved(BookmarkModel* model, | 56 virtual void BookmarkNodeRemoved(BookmarkModel* model, |
53 const BookmarkNode* parent, | 57 const BookmarkNode* parent, |
54 int old_index, | 58 int old_index, |
55 const BookmarkNode* node) OVERRIDE; | 59 const BookmarkNode* node) OVERRIDE; |
56 virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE; | 60 virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE; |
57 virtual void BookmarkNodeChanged(BookmarkModel* model, | 61 virtual void BookmarkNodeChanged(BookmarkModel* model, |
58 const BookmarkNode* node) OVERRIDE; | 62 const BookmarkNode* node) OVERRIDE; |
59 virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE; | |
60 | 63 |
61 // Some notifications we don't care about, but by being pure virtual | 64 // Some notifications we don't care about, but by being pure virtual |
62 // in the base class we must implement them. | 65 // in the base class we must implement them. |
63 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE { | 66 |
64 } | 67 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE { } |
65 virtual void BookmarkNodeAdded(BookmarkModel* model, | 68 virtual void BookmarkNodeAdded(BookmarkModel* model, |
66 const BookmarkNode* parent, | 69 const BookmarkNode* parent, |
67 int index) OVERRIDE { | 70 int index) OVERRIDE { } |
68 } | |
69 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, | 71 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, |
70 const BookmarkNode* node) OVERRIDE { | 72 const BookmarkNode* node) OVERRIDE { } |
71 } | |
72 virtual void BookmarkNodeChildrenReordered( | 73 virtual void BookmarkNodeChildrenReordered( |
73 BookmarkModel* model, | 74 BookmarkModel* model, |
74 const BookmarkNode* node) OVERRIDE { | 75 const BookmarkNode* node) OVERRIDE { } |
75 } | |
76 | 76 |
77 virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE { | 77 virtual void ExtensiveBookmarkChangesBeginning( |
78 } | 78 BookmarkModel* model) OVERRIDE { } |
| 79 |
| 80 virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE { } |
79 | 81 |
80 private: | 82 private: |
81 const BookmarkNode* node_; // Weak; owned by a BookmarkModel. | |
82 BookmarkModel* model_; // Weak; it is owned by a Profile. | 83 BookmarkModel* model_; // Weak; it is owned by a Profile. |
83 NSObject* object_; // Weak, like a delegate. | 84 std::set<const BookmarkNode*> nodes_; // Weak items owned by a BookmarkModel. |
84 SEL selector_; | 85 base::mac::ScopedBlock<ChangeCallback> callback_; |
85 | 86 |
86 void Notify(); | 87 // Send a notification to the client; |deleted| is YES if an observed node was |
| 88 // deleted in the change. |
| 89 void Notify(BOOL deleted); |
87 | 90 |
88 DISALLOW_COPY_AND_ASSIGN(BookmarkModelObserverForCocoa); | 91 DISALLOW_COPY_AND_ASSIGN(BookmarkModelObserverForCocoa); |
89 }; | 92 }; |
90 | 93 |
91 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H | 94 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H |
OLD | NEW |