bookmark_model_observer_for_cocoa.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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.
4
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
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
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
11// bookmark manager, we'd want to simply cancel the editor.
12//
13// This class is conservative and may result in notifications which
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
16// a list of "new parents").  But, just to be sure, notification
17// happens on any removal.
18
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
21#pragma once
22
23#import <Cocoa/Cocoa.h>
24
25#include "base/basictypes.h"
26#include "base/scoped_nsobject.h"
27#include "chrome/browser/bookmarks/bookmark_model.h"
28#include "chrome/browser/bookmarks/bookmark_model_observer.h"
29
30class BookmarkModelObserverForCocoa : public BookmarkModelObserver {
31 public:
32  // When |node| in |model| changes, send |selector| to |object|.
33  // Assumes |selector| is a selector that takes one arg, like an
34  // IBOutlet.  The arg passed is nil.
35  // Many notifications happen independently of node
36  // (e.g. BeingDeleted), so |node| can be nil.
37  //
38  // |object| is NOT retained, since the expected use case is for
39  // ||object| to own the BookmarkModelObserverForCocoa and we don't
40  // want a retain cycle.
41  BookmarkModelObserverForCocoa(const BookmarkNode* node,
42                                BookmarkModel* model,
43                                NSObject* object,
44                                SEL selector) {
45    DCHECK(model);
46    node_ = node;
47    model_ = model;
48    object_ = object;
49    selector_ = selector;
50    model_->AddObserver(this);
51  }
52  virtual ~BookmarkModelObserverForCocoa() {
53    model_->RemoveObserver(this);
54  }
55
56  virtual void BookmarkModelBeingDeleted(BookmarkModel* model) {
57    Notify();
58  }
59  virtual void BookmarkNodeMoved(BookmarkModel* model,
60                                 const BookmarkNode* old_parent,
61                                 int old_index,
62                                 const BookmarkNode* new_parent,
63                                 int new_index) {
64    // Editors often have a tree of parents, so movement of folders
65    // must cause a cancel.
66      Notify();
67  }
68  virtual void BookmarkNodeRemoved(BookmarkModel* model,
69                                   const BookmarkNode* parent,
70                                   int old_index,
71                                   const BookmarkNode* node) {
72    // See comment in BookmarkNodeMoved.
73    Notify();
74  }
75  virtual void BookmarkNodeChanged(BookmarkModel* model,
76                                   const BookmarkNode* node) {
77    if ((node_ == node) || (!node_))
78      Notify();
79  }
80  virtual void BookmarkImportBeginning(BookmarkModel* model) {
81    // Be conservative.
82    Notify();
83  }
84
85  // Some notifications we don't care about, but by being pure virtual
86  // in the base class we must implement them.
87  virtual void Loaded(BookmarkModel* model) {
88  }
89  virtual void BookmarkNodeAdded(BookmarkModel* model,
90                                 const BookmarkNode* parent,
91                                 int index) {
92  }
93  virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model,
94                                         const BookmarkNode* node) {
95  }
96  virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
97                                             const BookmarkNode* node) {
98  }
99
100  virtual void BookmarkImportEnding(BookmarkModel* model) {
101  }
102
103 private:
104  const BookmarkNode* node_;  // Weak; owned by a BookmarkModel.
105  BookmarkModel* model_;  // Weak; it is owned by a Profile.
106  NSObject* object_; // Weak, like a delegate.
107  SEL selector_;
108
109  void Notify() {
110    [object_ performSelector:selector_ withObject:nil];
111  }
112
113  DISALLOW_COPY_AND_ASSIGN(BookmarkModelObserverForCocoa);
114};
115
116#endif  // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H
117