Home | History | Annotate | Download | only in browser
      1 // Copyright 2014 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 COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_H_
      6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/task/cancelable_task_tracker.h"
     10 #include "base/time/time.h"
     11 #include "components/favicon_base/favicon_types.h"
     12 #include "ui/base/models/tree_node_model.h"
     13 #include "ui/gfx/image/image.h"
     14 #include "url/gurl.h"
     15 
     16 class BookmarkModel;
     17 
     18 // BookmarkNode ---------------------------------------------------------------
     19 
     20 // BookmarkNode contains information about a starred entry: title, URL, favicon,
     21 // id and type. BookmarkNodes are returned from BookmarkModel.
     22 class BookmarkNode : public ui::TreeNode<BookmarkNode> {
     23  public:
     24   enum Type {
     25     URL,
     26     FOLDER,
     27     BOOKMARK_BAR,
     28     OTHER_NODE,
     29     MOBILE
     30   };
     31 
     32   enum FaviconState {
     33     INVALID_FAVICON,
     34     LOADING_FAVICON,
     35     LOADED_FAVICON,
     36   };
     37 
     38   typedef std::map<std::string, std::string> MetaInfoMap;
     39 
     40   static const int64 kInvalidSyncTransactionVersion;
     41 
     42   // Creates a new node with an id of 0 and |url|.
     43   explicit BookmarkNode(const GURL& url);
     44   // Creates a new node with |id| and |url|.
     45   BookmarkNode(int64 id, const GURL& url);
     46 
     47   virtual ~BookmarkNode();
     48 
     49   // Set the node's internal title. Note that this neither invokes observers
     50   // nor updates any bookmark model this node may be in. For that functionality,
     51   // BookmarkModel::SetTitle(..) should be used instead.
     52   virtual void SetTitle(const base::string16& title) OVERRIDE;
     53 
     54   // Returns an unique id for this node.
     55   // For bookmark nodes that are managed by the bookmark model, the IDs are
     56   // persisted across sessions.
     57   int64 id() const { return id_; }
     58   void set_id(int64 id) { id_ = id; }
     59 
     60   const GURL& url() const { return url_; }
     61   void set_url(const GURL& url) { url_ = url; }
     62 
     63   // Returns the favicon's URL. Returns an empty URL if there is no favicon
     64   // associated with this bookmark.
     65   const GURL& icon_url() const { return icon_url_; }
     66 
     67   Type type() const { return type_; }
     68   void set_type(Type type) { type_ = type; }
     69 
     70   // Returns the time the node was added.
     71   const base::Time& date_added() const { return date_added_; }
     72   void set_date_added(const base::Time& date) { date_added_ = date; }
     73 
     74   // Returns the last time the folder was modified. This is only maintained
     75   // for folders (including the bookmark bar and other folder).
     76   const base::Time& date_folder_modified() const {
     77     return date_folder_modified_;
     78   }
     79   void set_date_folder_modified(const base::Time& date) {
     80     date_folder_modified_ = date;
     81   }
     82 
     83   // Convenience for testing if this node represents a folder. A folder is a
     84   // node whose type is not URL.
     85   bool is_folder() const { return type_ != URL; }
     86   bool is_url() const { return type_ == URL; }
     87 
     88   bool is_favicon_loaded() const { return favicon_state_ == LOADED_FAVICON; }
     89 
     90   // Accessor method for controlling the visibility of a bookmark node/sub-tree.
     91   // Note that visibility is not propagated down the tree hierarchy so if a
     92   // parent node is marked as invisible, a child node may return "Visible". This
     93   // function is primarily useful when traversing the model to generate a UI
     94   // representation but we may want to suppress some nodes.
     95   virtual bool IsVisible() const;
     96 
     97   // Gets/sets/deletes value of |key| in the meta info represented by
     98   // |meta_info_str_|. Return true if key is found in meta info for gets or
     99   // meta info is changed indeed for sets/deletes.
    100   bool GetMetaInfo(const std::string& key, std::string* value) const;
    101   bool SetMetaInfo(const std::string& key, const std::string& value);
    102   bool DeleteMetaInfo(const std::string& key);
    103   void SetMetaInfoMap(const MetaInfoMap& meta_info_map);
    104   // Returns NULL if there are no values in the map.
    105   const MetaInfoMap* GetMetaInfoMap() const;
    106 
    107   void set_sync_transaction_version(int64 sync_transaction_version) {
    108     sync_transaction_version_ = sync_transaction_version;
    109   }
    110   int64 sync_transaction_version() const {
    111     return sync_transaction_version_;
    112   }
    113 
    114   // TODO(sky): Consider adding last visit time here, it'll greatly simplify
    115   // HistoryContentsProvider.
    116 
    117  private:
    118   friend class BookmarkModel;
    119 
    120   // A helper function to initialize various fields during construction.
    121   void Initialize(int64 id);
    122 
    123   // Called when the favicon becomes invalid.
    124   void InvalidateFavicon();
    125 
    126   // Sets the favicon's URL.
    127   void set_icon_url(const GURL& icon_url) {
    128     icon_url_ = icon_url;
    129   }
    130 
    131   // Returns the favicon. In nearly all cases you should use the method
    132   // BookmarkModel::GetFavicon rather than this one as it takes care of
    133   // loading the favicon if it isn't already loaded.
    134   const gfx::Image& favicon() const { return favicon_; }
    135   void set_favicon(const gfx::Image& icon) { favicon_ = icon; }
    136 
    137   favicon_base::IconType favicon_type() const { return favicon_type_; }
    138   void set_favicon_type(favicon_base::IconType type) { favicon_type_ = type; }
    139 
    140   FaviconState favicon_state() const { return favicon_state_; }
    141   void set_favicon_state(FaviconState state) { favicon_state_ = state; }
    142 
    143   base::CancelableTaskTracker::TaskId favicon_load_task_id() const {
    144     return favicon_load_task_id_;
    145   }
    146   void set_favicon_load_task_id(base::CancelableTaskTracker::TaskId id) {
    147     favicon_load_task_id_ = id;
    148   }
    149 
    150   // The unique identifier for this node.
    151   int64 id_;
    152 
    153   // The URL of this node. BookmarkModel maintains maps off this URL, so changes
    154   // to the URL must be done through the BookmarkModel.
    155   GURL url_;
    156 
    157   // The type of this node. See enum above.
    158   Type type_;
    159 
    160   // Date of when this node was created.
    161   base::Time date_added_;
    162 
    163   // Date of the last modification. Only used for folders.
    164   base::Time date_folder_modified_;
    165 
    166   // The favicon of this node.
    167   gfx::Image favicon_;
    168 
    169   // The type of favicon currently loaded.
    170   favicon_base::IconType favicon_type_;
    171 
    172   // The URL of the node's favicon.
    173   GURL icon_url_;
    174 
    175   // The loading state of the favicon.
    176   FaviconState favicon_state_;
    177 
    178   // If not base::CancelableTaskTracker::kBadTaskId, it indicates
    179   // we're loading the
    180   // favicon and the task is tracked by CancelabelTaskTracker.
    181   base::CancelableTaskTracker::TaskId favicon_load_task_id_;
    182 
    183   // A map that stores arbitrary meta information about the node.
    184   scoped_ptr<MetaInfoMap> meta_info_map_;
    185 
    186   // The sync transaction version. Defaults to kInvalidSyncTransactionVersion.
    187   int64 sync_transaction_version_;
    188 
    189   DISALLOW_COPY_AND_ASSIGN(BookmarkNode);
    190 };
    191 
    192 // BookmarkPermanentNode -------------------------------------------------------
    193 
    194 // Node used for the permanent folders (excluding the root).
    195 class BookmarkPermanentNode : public BookmarkNode {
    196  public:
    197   explicit BookmarkPermanentNode(int64 id);
    198   virtual ~BookmarkPermanentNode();
    199 
    200   // WARNING: this code is used for other projects. Contact noyau@ for details.
    201   void set_visible(bool value) { visible_ = value; }
    202 
    203   // BookmarkNode overrides:
    204   virtual bool IsVisible() const OVERRIDE;
    205 
    206  private:
    207   bool visible_;
    208 
    209   DISALLOW_COPY_AND_ASSIGN(BookmarkPermanentNode);
    210 };
    211 
    212 #endif  // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_H_
    213