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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/browser/android/bookmarks/managed_bookmarks_shim.h"
11 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
12 #include "chrome/browser/favicon/favicon_service.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"
17 // The handler for Javascript messages related to the bookmarks.
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.
24 // All bookmarks and subfolder is returned by bookmarks() javascript callback
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.
31 // A sample result looks like:
33 // title: 'Bookmark Bar',
39 // url: 'http://www.google.com',
49 class BookmarksHandler : public content::WebUIMessageHandler,
50 public BaseBookmarkModelObserver,
51 public PartnerBookmarksShim::Observer,
52 public ManagedBookmarksShim::Observer {
55 virtual ~BookmarksHandler();
57 // WebUIMessageHandler override and implementation.
58 virtual void RegisterMessages() OVERRIDE;
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);
71 // Notify the UI that a change occurred to the bookmark model.
72 virtual void NotifyModelChanged(const DictionaryValue& status);
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,
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;
89 // Override the methods of PartnerBookmarksShim::Observer
90 virtual void PartnerShimChanged(PartnerBookmarksShim* shim) OVERRIDE;
91 virtual void PartnerShimLoaded(PartnerBookmarksShim* shim) OVERRIDE;
92 virtual void ShimBeingDeleted(PartnerBookmarksShim* shim) OVERRIDE;
94 // Override the methods of ManagedBookmarksShim::Observer
95 virtual void OnManagedBookmarksChanged() OVERRIDE;
98 // The bookmark model being observed (if it has been attached).
99 BookmarkModel* bookmark_model_;
101 // Information about the Partner bookmarks (must check for IsLoaded())
102 PartnerBookmarksShim* partner_bookmarks_shim_;
104 // Contains the bookmarks managed via enterprise policy.
105 scoped_ptr<ManagedBookmarksShim> managed_bookmarks_shim_;
107 // Whether the bookmark data has been requested by the UI yet.
108 bool bookmark_data_requested_;
110 // Indicates that extensive changes to the BookmarkModel is on-going.
111 bool extensive_changes_;
113 // Used for loading bookmark node.
114 CancelableTaskTracker cancelable_task_tracker_;
116 // Generates the string encoded ID to be used by the NTP.
117 std::string GetBookmarkIdForNtp(const BookmarkNode* node);
119 // Sets the necessary parent information in the response object to be sent
120 // to the UI renderer.
121 void SetParentInBookmarksResult(const BookmarkNode* parent,
122 DictionaryValue* result);
124 // Convert the given bookmark |node| into a dictionary format to be returned
126 void PopulateBookmark(const BookmarkNode* node, ListValue* result);
128 // Given a bookmark folder node, |folder|, populate the |result| with the
129 // structured JavaScript-formatted data regarding the folder.
130 void PopulateBookmarksInFolder(const BookmarkNode* folder,
131 DictionaryValue* result);
133 // Sends all bookmarks and sub folders in the given folder back to the NTP.
134 void QueryBookmarkFolder(const BookmarkNode* node);
136 // Sends bookmark bar's bookmarks and sub folders and other folders back to
138 void QueryInitialBookmarks();
140 // Sends the result back to Javascript
141 void SendResult(const DictionaryValue& result);
143 // Called once the favicon is loaded during creation of the bookmark shortcuts
144 // and is available for use.
145 void OnShortcutFaviconDataAvailable(
146 const BookmarkNode* node,
147 const chrome::FaviconBitmapResult& bitmap_result);
149 // Looks at an optional bookmark ID in |args| and returns the corresponding
150 // node if found, otherwise returns NULL.
151 const BookmarkNode* GetNodeByID(const base::ListValue* args) const;
153 // Returns the parent of |node|, or NULL if it's the root node.
154 const BookmarkNode* GetParentOf(const BookmarkNode* node) const;
156 // Returns the title of |node|, possibly remapped (if a partner bookmark).
157 base::string16 GetTitle(const BookmarkNode* node) const;
159 // Returns true if the node is reachable.
160 bool IsReachable(const BookmarkNode* node) const;
162 // Returns true if |node| can be modified by the user.
163 bool IsEditable(const BookmarkNode* node) const;
165 // Returns true if |node| is the real root node (not the root node of the
166 // partner bookmarks shim nor the managed bookmark shim root).
167 bool IsRoot(const BookmarkNode* node) const;
169 DISALLOW_COPY_AND_ASSIGN(BookmarksHandler);
172 #endif // CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_