Home | History | Annotate | Download | only in bookmarks
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_ANDROID_BOOKMARKS_PARTNER_BOOKMARKS_SHIM_H_
      6 #define CHROME_BROWSER_ANDROID_BOOKMARKS_PARTNER_BOOKMARKS_SHIM_H_
      7 
      8 #include "base/android/jni_helper.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/strings/string16.h"
     11 #include "base/supports_user_data.h"
     12 #include "chrome/browser/bookmarks/bookmark_model.h"
     13 #include "url/gurl.h"
     14 
     15 class PrefService;
     16 
     17 namespace content {
     18 class WebContents;
     19 }
     20 
     21 namespace user_prefs {
     22 class PrefRegistrySyncable;
     23 }
     24 
     25 // A shim that lives on top of a BookmarkModel that allows the injection of
     26 // partner bookmarks without submitting changes to the bookmark model.
     27 // The shim persists bookmark renames/deletions in a user profile and could be
     28 // queried via shim->GetTitle(node) and shim->IsReachable(node).
     29 // Note that node->GetTitle() returns an original (unmodified) title.
     30 class PartnerBookmarksShim : public base::SupportsUserData::Data {
     31  public:
     32   // Returns an instance of the shim for a given |browser_context|.
     33   static PartnerBookmarksShim* BuildForBrowserContext(
     34       content::BrowserContext* browser_context);
     35 
     36   // Registers preferences.
     37   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
     38 
     39   // Returns true if everything got loaded.
     40   bool IsLoaded() const;
     41 
     42   // Returns true if there are partner bookmarks.
     43   bool HasPartnerBookmarks() const;
     44 
     45   // Returns true if a given bookmark is reachable (i.e. neither the bookmark,
     46   // nor any of its parents were "removed").
     47   bool IsReachable(const BookmarkNode* node) const;
     48 
     49   // Removes a given bookmark.
     50   // Makes the |node| (and, consequently, all its children) unreachable.
     51   void RemoveBookmark(const BookmarkNode* node);
     52 
     53   // Renames a given bookmark.
     54   void RenameBookmark(const BookmarkNode* node, const base::string16& title);
     55 
     56   // For Loaded/Changed/ShimBeingDeleted notifications
     57   class Observer {
     58    public:
     59     // Called when the set of bookmarks, or their values/visibility changes
     60     virtual void PartnerShimChanged(PartnerBookmarksShim*) {}
     61     // Called when everything is loaded
     62     virtual void PartnerShimLoaded(PartnerBookmarksShim*) {}
     63     // Called just before everything got destroyed
     64     virtual void ShimBeingDeleted(PartnerBookmarksShim*) {}
     65    protected:
     66     virtual ~Observer() {}
     67   };
     68 
     69   void AddObserver(Observer* observer);
     70   void RemoveObserver(Observer* observer);
     71 
     72   // PartnerBookmarksShim versions of BookmarkModel/BookmarkNode methods
     73   const BookmarkNode* GetNodeByID(int64 id) const;
     74   base::string16 GetTitle(const BookmarkNode* node) const;
     75 
     76   bool IsPartnerBookmark(const BookmarkNode* node) const;
     77   const BookmarkNode* GetPartnerBookmarksRoot() const;
     78 
     79   // Sets the root node of the partner bookmarks and notifies any observers that
     80   // the shim has now been loaded.  Takes ownership of |root_node|.
     81   void SetPartnerBookmarksRoot(BookmarkNode* root_node);
     82 
     83   // Used as a "unique" identifier of the partner bookmark node for the purposes
     84   // of node deletion and title editing. Two bookmarks with the same URLs and
     85   // titles are considered indistinguishable.
     86   class NodeRenamingMapKey {
     87    public:
     88     NodeRenamingMapKey(const GURL& url, const base::string16& provider_title);
     89     ~NodeRenamingMapKey();
     90     const GURL& url() const { return url_; }
     91     const base::string16& provider_title() const { return provider_title_; }
     92     friend bool operator<(const NodeRenamingMapKey& a,
     93                           const NodeRenamingMapKey& b);
     94    private:
     95     GURL url_;
     96     base::string16 provider_title_;
     97   };
     98   typedef std::map<NodeRenamingMapKey, base::string16> NodeRenamingMap;
     99 
    100   // For testing: clears an instance of the shim in a given |browser_context|.
    101   static void ClearInBrowserContextForTesting(
    102       content::BrowserContext* browser_context);
    103 
    104   // For testing: clears partner bookmark model data.
    105   static void ClearPartnerModelForTesting();
    106 
    107  private:
    108   explicit PartnerBookmarksShim(PrefService* prefs);
    109   virtual ~PartnerBookmarksShim();
    110 
    111   const BookmarkNode* GetNodeByID(const BookmarkNode* parent, int64 id) const;
    112   void ReloadNodeMapping();
    113   void SaveNodeMapping();
    114 
    115   scoped_ptr<BookmarkNode> partner_bookmarks_root_;
    116   PrefService* prefs_;
    117   NodeRenamingMap node_rename_remove_map_;
    118 
    119   // The observers.
    120   ObserverList<Observer> observers_;
    121 
    122   DISALLOW_COPY_AND_ASSIGN(PartnerBookmarksShim);
    123 };
    124 
    125 #endif  // CHROME_BROWSER_ANDROID_BOOKMARKS_PARTNER_BOOKMARKS_SHIM_H_
    126