Home | History | Annotate | Download | only in bookmarks
      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
      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 
     22 #import <Cocoa/Cocoa.h>
     23 
     24 #include <set>
     25 
     26 #include "base/basictypes.h"
     27 #include "base/mac/scoped_block.h"
     28 #include "components/bookmarks/browser/bookmark_model.h"
     29 #include "components/bookmarks/browser/bookmark_model_observer.h"
     30 
     31 class BookmarkModelObserverForCocoa : public BookmarkModelObserver {
     32  public:
     33   // Callback called on a significant model change. |nodeWasDeleted| will
     34   // be YES if an observed node was deleted in the change.
     35   typedef void(^ChangeCallback)(BOOL nodeWasDeleted);
     36 
     37   // When a |model| changes, or an observed node within it does, call a
     38   // |callback|.
     39   BookmarkModelObserverForCocoa(BookmarkModel* model,
     40                                 ChangeCallback callback);
     41   virtual ~BookmarkModelObserverForCocoa();
     42 
     43   // Starts and stops observing a specified |node|; the node must be contained
     44   // within the model.
     45   void StartObservingNode(const BookmarkNode* node);
     46   void StopObservingNode(const BookmarkNode* node);
     47 
     48   // BookmarkModelObserver:
     49   virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
     50   virtual void BookmarkNodeMoved(BookmarkModel* model,
     51                                  const BookmarkNode* old_parent,
     52                                  int old_index,
     53                                  const BookmarkNode* new_parent,
     54                                  int new_index) OVERRIDE;
     55   virtual void BookmarkNodeRemoved(BookmarkModel* model,
     56                                    const BookmarkNode* parent,
     57                                    int old_index,
     58                                    const BookmarkNode* node,
     59                                    const std::set<GURL>& removed_urls) OVERRIDE;
     60   virtual void BookmarkAllUserNodesRemoved(
     61       BookmarkModel* model,
     62       const std::set<GURL>& removed_urls) OVERRIDE;
     63   virtual void BookmarkNodeChanged(BookmarkModel* model,
     64                                    const BookmarkNode* node) OVERRIDE;
     65 
     66   // Some notifications we don't care about, but by being pure virtual
     67   // in the base class we must implement them.
     68 
     69   virtual void BookmarkModelLoaded(BookmarkModel* model,
     70                                    bool ids_reassigned) OVERRIDE {}
     71   virtual void BookmarkNodeAdded(BookmarkModel* model,
     72                                  const BookmarkNode* parent,
     73                                  int index) OVERRIDE {}
     74   virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
     75                                           const BookmarkNode* node) OVERRIDE {}
     76   virtual void BookmarkNodeChildrenReordered(
     77       BookmarkModel* model,
     78       const BookmarkNode* node) OVERRIDE {}
     79 
     80   virtual void ExtensiveBookmarkChangesBeginning(
     81       BookmarkModel* model) OVERRIDE {}
     82 
     83   virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE {}
     84 
     85  private:
     86   BookmarkModel* model_;  // Weak; it is owned by a Profile.
     87   std::set<const BookmarkNode*> nodes_;  // Weak items owned by a BookmarkModel.
     88   base::mac::ScopedBlock<ChangeCallback> callback_;
     89 
     90   // Send a notification to the client; |deleted| is YES if an observed node was
     91   // deleted in the change.
     92   void Notify(BOOL deleted);
     93 
     94   DISALLOW_COPY_AND_ASSIGN(BookmarkModelObserverForCocoa);
     95 };
     96 
     97 #endif  // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_FOR_COCOA_H
     98