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
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/memory/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  virtual ~BookmarkModelObserverForCocoa();
46
47  virtual void BookmarkModelBeingDeleted(BookmarkModel* model);
48  virtual void BookmarkNodeMoved(BookmarkModel* model,
49                                 const BookmarkNode* old_parent,
50                                 int old_index,
51                                 const BookmarkNode* new_parent,
52                                 int new_index);
53  virtual void BookmarkNodeRemoved(BookmarkModel* model,
54                                   const BookmarkNode* parent,
55                                   int old_index,
56                                   const BookmarkNode* node);
57  virtual void BookmarkNodeChanged(BookmarkModel* model,
58                                   const BookmarkNode* node);
59  virtual void BookmarkImportBeginning(BookmarkModel* model);
60
61  // Some notifications we don't care about, but by being pure virtual
62  // in the base class we must implement them.
63  virtual void Loaded(BookmarkModel* model) {
64  }
65  virtual void BookmarkNodeAdded(BookmarkModel* model,
66                                 const BookmarkNode* parent,
67                                 int index) {
68  }
69  virtual void BookmarkNodeFaviconLoaded(BookmarkModel* model,
70                                         const BookmarkNode* node) {
71  }
72  virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
73                                             const BookmarkNode* node) {
74  }
75
76  virtual void BookmarkImportEnding(BookmarkModel* model) {
77  }
78
79 private:
80  const BookmarkNode* node_;  // Weak; owned by a BookmarkModel.
81  BookmarkModel* model_;  // Weak; it is owned by a Profile.
82  NSObject* object_; // Weak, like a delegate.
83  SEL selector_;
84
85  void Notify();
86
87  DISALLOW_COPY_AND_ASSIGN(BookmarkModelObserverForCocoa);
88};
89
90#endif  // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H
91