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/task/cancelable_task_tracker.h"
10 #include "base/values.h"
11 #include "chrome/browser/android/bookmarks/managed_bookmarks_shim.h"
12 #include "chrome/browser/android/bookmarks/partner_bookmarks_shim.h"
13 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
14 #include "chrome/browser/favicon/favicon_service.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 base::ListValue
* args
);
62 // Callback for the "deleteBookmark" message.
63 void HandleDeleteBookmark(const base::ListValue
* args
);
64 // Callback for the "editBookmark" message.
65 void HandleEditBookmark(const base::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 // Override the methods of BookmarkModelObserver
72 virtual void BookmarkModelLoaded(BookmarkModel
* model
,
73 bool ids_reassigned
) OVERRIDE
;
74 virtual void BookmarkModelChanged() OVERRIDE
;
75 virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel
* model
) OVERRIDE
;
76 virtual void ExtensiveBookmarkChangesEnded(BookmarkModel
* model
) OVERRIDE
;
77 virtual void BookmarkNodeRemoved(BookmarkModel
* model
,
78 const BookmarkNode
* parent
,
80 const BookmarkNode
* node
) OVERRIDE
;
81 virtual void BookmarkAllNodesRemoved(BookmarkModel
* model
) OVERRIDE
;
82 virtual void BookmarkNodeAdded(
83 BookmarkModel
* model
, const BookmarkNode
* parent
, int index
) OVERRIDE
;
84 virtual void BookmarkNodeChanged(BookmarkModel
* model
,
85 const BookmarkNode
* node
) OVERRIDE
;
87 // Override the methods of PartnerBookmarksShim::Observer
88 virtual void PartnerShimChanged(PartnerBookmarksShim
* shim
) OVERRIDE
;
89 virtual void PartnerShimLoaded(PartnerBookmarksShim
* shim
) OVERRIDE
;
90 virtual void ShimBeingDeleted(PartnerBookmarksShim
* shim
) OVERRIDE
;
92 // Override the methods of ManagedBookmarksShim::Observer
93 virtual void OnManagedBookmarksChanged() OVERRIDE
;
96 // The bookmark model being observed (if it has been attached).
97 BookmarkModel
* bookmark_model_
;
99 // Information about the Partner bookmarks (must check for IsLoaded())
100 PartnerBookmarksShim
* partner_bookmarks_shim_
;
102 // Contains the bookmarks managed via enterprise policy.
103 scoped_ptr
<ManagedBookmarksShim
> managed_bookmarks_shim_
;
105 // Whether the bookmark data has been requested by the UI yet.
106 bool bookmark_data_requested_
;
108 // Indicates that extensive changes to the BookmarkModel is on-going.
109 bool extensive_changes_
;
111 // Used for loading bookmark node.
112 base::CancelableTaskTracker cancelable_task_tracker_
;
114 // Returns true iff bookmark model and partner bookmarks shim are loaded.
115 bool AreModelsLoaded() const;
117 // Notify the UI that a change occurred to the bookmark model.
118 void NotifyModelChanged(const base::DictionaryValue
& status
);
120 // Generates the string encoded ID to be used by the NTP.
121 std::string
GetBookmarkIdForNtp(const BookmarkNode
* node
);
123 // Sets the necessary parent information in the response object to be sent
124 // to the UI renderer.
125 void SetParentInBookmarksResult(const BookmarkNode
* parent
,
126 base::DictionaryValue
* result
);
128 // Convert the given bookmark |node| into a dictionary format to be returned
130 void PopulateBookmark(const BookmarkNode
* node
, base::ListValue
* result
);
132 // Given a bookmark folder node, |folder|, populate the |result| with the
133 // structured JavaScript-formatted data regarding the folder.
134 void PopulateBookmarksInFolder(const BookmarkNode
* folder
,
135 base::DictionaryValue
* result
);
137 // Sends all bookmarks and sub folders in the given folder back to the NTP.
138 void QueryBookmarkFolder(const BookmarkNode
* node
);
140 // Sends bookmark bar's bookmarks and sub folders and other folders back to
142 void QueryInitialBookmarks();
144 // Sends the result back to Javascript
145 void SendResult(const base::DictionaryValue
& result
);
147 // Called once the favicon is loaded during creation of the bookmark shortcuts
148 // and is available for use.
149 void OnShortcutFaviconDataAvailable(
150 const BookmarkNode
* node
,
151 const chrome::FaviconBitmapResult
& bitmap_result
);
153 // Looks at an optional bookmark ID in |args| and returns the corresponding
154 // node if found, otherwise returns NULL.
155 const BookmarkNode
* GetNodeByID(const base::ListValue
* args
) const;
157 // Returns the parent of |node|, or NULL if it's the root node.
158 const BookmarkNode
* GetParentOf(const BookmarkNode
* node
) const;
160 // Returns the title of |node|, possibly remapped (if a partner bookmark).
161 base::string16
GetTitle(const BookmarkNode
* node
) const;
163 // Returns true if the node is reachable.
164 bool IsReachable(const BookmarkNode
* node
) const;
166 // Returns true if |node| can be modified by the user.
167 bool IsEditable(const BookmarkNode
* node
) const;
169 // Returns true if |node| is the real root node (not the root node of the
170 // partner bookmarks shim nor the managed bookmark shim root).
171 bool IsRoot(const BookmarkNode
* node
) const;
173 DISALLOW_COPY_AND_ASSIGN(BookmarksHandler
);
176 #endif // CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_