- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / ntp / android / bookmarks_handler.h
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/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"
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 PartnerShimChanged(PartnerBookmarksShim* shim) OVERRIDE;
91   virtual void PartnerShimLoaded(PartnerBookmarksShim* shim) OVERRIDE;
92   virtual void ShimBeingDeleted(PartnerBookmarksShim* shim) OVERRIDE;
93
94   // Override the methods of ManagedBookmarksShim::Observer
95   virtual void OnManagedBookmarksChanged() OVERRIDE;
96
97  private:
98   // The bookmark model being observed (if it has been attached).
99   BookmarkModel* bookmark_model_;
100
101   // Information about the Partner bookmarks (must check for IsLoaded())
102   PartnerBookmarksShim* partner_bookmarks_shim_;
103
104   // Contains the bookmarks managed via enterprise policy.
105   scoped_ptr<ManagedBookmarksShim> managed_bookmarks_shim_;
106
107   // Whether the bookmark data has been requested by the UI yet.
108   bool bookmark_data_requested_;
109
110   // Indicates that extensive changes to the BookmarkModel is on-going.
111   bool extensive_changes_;
112
113   // Used for loading bookmark node.
114   CancelableTaskTracker cancelable_task_tracker_;
115
116   // Generates the string encoded ID to be used by the NTP.
117   std::string GetBookmarkIdForNtp(const BookmarkNode* node);
118
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);
123
124   // Convert the given bookmark |node| into a dictionary format to be returned
125   // to JavaScript.
126   void PopulateBookmark(const BookmarkNode* node, ListValue* result);
127
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);
132
133   // Sends all bookmarks and sub folders in the given folder back to the NTP.
134   void QueryBookmarkFolder(const BookmarkNode* node);
135
136   // Sends bookmark bar's bookmarks and sub folders and other folders back to
137   // NTP.
138   void QueryInitialBookmarks();
139
140   // Sends the result back to Javascript
141   void SendResult(const DictionaryValue& result);
142
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);
148
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;
152
153   // Returns the parent of |node|, or NULL if it's the root node.
154   const BookmarkNode* GetParentOf(const BookmarkNode* node) const;
155
156   // Returns the title of |node|, possibly remapped (if a partner bookmark).
157   base::string16 GetTitle(const BookmarkNode* node) const;
158
159   // Returns true if the node is reachable.
160   bool IsReachable(const BookmarkNode* node) const;
161
162   // Returns true if |node| can be modified by the user.
163   bool IsEditable(const BookmarkNode* node) const;
164
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;
168
169   DISALLOW_COPY_AND_ASSIGN(BookmarksHandler);
170 };
171
172 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_