Home | History | Annotate | Download | only in android
      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 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_
      6 #define CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/values.h"
     10 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
     11 #include "chrome/browser/favicon/favicon_service.h"
     12 #include "chrome/browser/ui/webui/ntp/android/managed_bookmarks_shim.h"
     13 #include "chrome/browser/ui/webui/ntp/android/partner_bookmarks_shim.h"
     14 #include "chrome/common/cancelable_task_tracker.h"
     15 #include "content/public/browser/web_ui_message_handler.h"
     16 
     17 // The handler for Javascript messages related to the bookmarks.
     18 //
     19 // In Javascript if getBookmarks() is called without any parameter, the 'Other
     20 // Bookmark' folder and bookmark bar's bookmarks and folders are returned.
     21 // If getBookmarks() is called with a valid bookmark folder id, the given
     22 // folder's bookmarks and sub folders of it are returned.
     23 //
     24 // All bookmarks and subfolder is returned by bookmarks() javascript callback
     25 // function.
     26 // The returned field 'folder' indicates whether the data is a folder. The
     27 // returned field 'root' indicates whether or not the bookmark list that was
     28 // returned is the root list or not.  Besides these fields, a folder has id
     29 // and title fields; A bookmark has url and title fields.
     30 //
     31 // A sample result looks like:
     32 // {
     33 //   title: 'Bookmark Bar',
     34 //   id: '1',
     35 //   root: true,
     36 //   bookmarks: [
     37 //     {
     38 //       title: 'Cake',
     39 //       url: 'http://www.google.com',
     40 //       folder: false
     41 //     },
     42 //     {
     43 //       title: 'Puppies',
     44 //       folder: true,
     45 //       id: '2'
     46 //     }
     47 //   ]
     48 // }
     49 class BookmarksHandler : public content::WebUIMessageHandler,
     50                          public BaseBookmarkModelObserver,
     51                          public PartnerBookmarksShim::Observer,
     52                          public ManagedBookmarksShim::Observer {
     53  public:
     54   BookmarksHandler();
     55   virtual ~BookmarksHandler();
     56 
     57   // WebUIMessageHandler override and implementation.
     58   virtual void RegisterMessages() OVERRIDE;
     59 
     60   // Callback for the "getBookmarks" message.
     61   void HandleGetBookmarks(const ListValue* args);
     62   // Callback for the "deleteBookmark" message.
     63   void HandleDeleteBookmark(const ListValue* args);
     64   // Callback for the "editBookmark" message.
     65   void HandleEditBookmark(const ListValue* args);
     66   // Callback for the "createHomeScreenBookmarkShortcut" message.  Used when
     67   // creating a shortcut on the home screen that should open the bookmark
     68   // specified in |args|.
     69   void HandleCreateHomeScreenBookmarkShortcut(const base::ListValue* args);
     70 
     71   // Notify the UI that a change occurred to the bookmark model.
     72   virtual void NotifyModelChanged(const DictionaryValue& status);
     73 
     74   // Override the methods of BookmarkModelObserver
     75   virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE;
     76   virtual void BookmarkModelChanged() OVERRIDE;
     77   virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE;
     78   virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE;
     79   virtual void BookmarkNodeRemoved(BookmarkModel* model,
     80                                    const BookmarkNode* parent,
     81                                    int old_index,
     82                                    const BookmarkNode* node) OVERRIDE;
     83   virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE;
     84   virtual void BookmarkNodeAdded(
     85       BookmarkModel* model, const BookmarkNode* parent, int index) OVERRIDE;
     86   virtual void BookmarkNodeChanged(BookmarkModel* model,
     87                                    const BookmarkNode* node) OVERRIDE;
     88 
     89   // Override the methods of PartnerBookmarksShim::Observer
     90   virtual void PartnerShimLoaded(PartnerBookmarksShim* shim) OVERRIDE;
     91   virtual void ShimBeingDeleted(PartnerBookmarksShim* shim) OVERRIDE;
     92 
     93   // Override the methods of ManagedBookmarksShim::Observer
     94   virtual void OnManagedBookmarksChanged() OVERRIDE;
     95 
     96  private:
     97   // The bookmark model being observed (if it has been attached).
     98   BookmarkModel* bookmark_model_;
     99 
    100   // Information about the Partner bookmarks (must check for IsLoaded())
    101   PartnerBookmarksShim* partner_bookmarks_shim_;
    102 
    103   // Contains the bookmarks managed via enterprise policy.
    104   scoped_ptr<ManagedBookmarksShim> managed_bookmarks_shim_;
    105 
    106   // Whether the bookmark data has been requested by the UI yet.
    107   bool bookmark_data_requested_;
    108 
    109   // Indicates that extensive changes to the BookmarkModel is on-going.
    110   bool extensive_changes_;
    111 
    112   // Used for loading bookmark node.
    113   CancelableTaskTracker cancelable_task_tracker_;
    114 
    115   // Generates the string encoded ID to be used by the NTP.
    116   std::string GetBookmarkIdForNtp(const BookmarkNode* node);
    117 
    118   // Sets the necessary parent information in the response object to be sent
    119   // to the UI renderer.
    120   void SetParentInBookmarksResult(const BookmarkNode* parent,
    121                                   DictionaryValue* result);
    122 
    123   // Convert the given bookmark |node| into a dictionary format to be returned
    124   // to JavaScript.
    125   void PopulateBookmark(const BookmarkNode* node, ListValue* result);
    126 
    127   // Given a bookmark folder node, |folder|, populate the |result| with the
    128   // structured JavaScript-formatted data regarding the folder.
    129   void PopulateBookmarksInFolder(const BookmarkNode* folder,
    130                                  DictionaryValue* result);
    131 
    132   // Sends all bookmarks and sub folders in the given folder back to the NTP.
    133   void QueryBookmarkFolder(const BookmarkNode* node);
    134 
    135   // Sends bookmark bar's bookmarks and sub folders and other folders back to
    136   // NTP.
    137   void QueryInitialBookmarks();
    138 
    139   // Sends the result back to Javascript
    140   void SendResult(const DictionaryValue& result);
    141 
    142   // Called once the favicon is loaded during creation of the bookmark shortcuts
    143   // and is available for use.
    144   void OnShortcutFaviconDataAvailable(
    145       const BookmarkNode* node,
    146       const chrome::FaviconBitmapResult& bitmap_result);
    147 
    148   // Looks at an optional bookmark ID in |args| and returns the corresponding
    149   // node if found, otherwise returns NULL.
    150   const BookmarkNode* GetNodeByID(const base::ListValue* args) const;
    151 
    152   // Returns the parent of |node|, or NULL if it's the root node.
    153   const BookmarkNode* GetParentOf(const BookmarkNode* node) const;
    154 
    155   // Returns true if |node| can be modified by the user.
    156   bool IsEditable(const BookmarkNode* node) const;
    157 
    158   // Returns true if |node| is the real root node (not the root node of the
    159   // partner bookmarks shim nor the managed bookmark shim root).
    160   bool IsRoot(const BookmarkNode* node) const;
    161 
    162   DISALLOW_COPY_AND_ASSIGN(BookmarksHandler);
    163 };
    164 
    165 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_
    166