Upstream version 7.35.144.0
[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/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"
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 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);
70
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,
79                                    int old_index,
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;
86
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;
91
92   // Override the methods of ManagedBookmarksShim::Observer
93   virtual void OnManagedBookmarksChanged() OVERRIDE;
94
95  private:
96   // The bookmark model being observed (if it has been attached).
97   BookmarkModel* bookmark_model_;
98
99   // Information about the Partner bookmarks (must check for IsLoaded())
100   PartnerBookmarksShim* partner_bookmarks_shim_;
101
102   // Contains the bookmarks managed via enterprise policy.
103   scoped_ptr<ManagedBookmarksShim> managed_bookmarks_shim_;
104
105   // Whether the bookmark data has been requested by the UI yet.
106   bool bookmark_data_requested_;
107
108   // Indicates that extensive changes to the BookmarkModel is on-going.
109   bool extensive_changes_;
110
111   // Used for loading bookmark node.
112   base::CancelableTaskTracker cancelable_task_tracker_;
113
114   // Returns true iff bookmark model and partner bookmarks shim are loaded.
115   bool AreModelsLoaded() const;
116
117   // Notify the UI that a change occurred to the bookmark model.
118   void NotifyModelChanged(const base::DictionaryValue& status);
119
120   // Generates the string encoded ID to be used by the NTP.
121   std::string GetBookmarkIdForNtp(const BookmarkNode* node);
122
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);
127
128   // Convert the given bookmark |node| into a dictionary format to be returned
129   // to JavaScript.
130   void PopulateBookmark(const BookmarkNode* node, base::ListValue* result);
131
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);
136
137   // Sends all bookmarks and sub folders in the given folder back to the NTP.
138   void QueryBookmarkFolder(const BookmarkNode* node);
139
140   // Sends bookmark bar's bookmarks and sub folders and other folders back to
141   // NTP.
142   void QueryInitialBookmarks();
143
144   // Sends the result back to Javascript
145   void SendResult(const base::DictionaryValue& result);
146
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);
152
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;
156
157   // Returns the parent of |node|, or NULL if it's the root node.
158   const BookmarkNode* GetParentOf(const BookmarkNode* node) const;
159
160   // Returns the title of |node|, possibly remapped (if a partner bookmark).
161   base::string16 GetTitle(const BookmarkNode* node) const;
162
163   // Returns true if the node is reachable.
164   bool IsReachable(const BookmarkNode* node) const;
165
166   // Returns true if |node| can be modified by the user.
167   bool IsEditable(const BookmarkNode* node) const;
168
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;
172
173   DISALLOW_COPY_AND_ASSIGN(BookmarksHandler);
174 };
175
176 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_ANDROID_BOOKMARKS_HANDLER_H_