OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #include "chrome/browser/ui/webui/ntp/bookmarks_handler.h" | |
6 | |
7 #include "base/auto_reset.h" | |
8 #include "base/bind.h" | |
9 #include "base/bind_helpers.h" | |
10 #include "base/string_number_conversions.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/bookmarks/bookmark_extension_api_constants.h" | |
13 #include "chrome/browser/bookmarks/bookmark_model.h" | |
14 #include "chrome/browser/prefs/pref_service.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/sync/profile_sync_service.h" | |
17 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | |
18 #include "chrome/common/chrome_notification_types.h" | |
19 #include "chrome/common/pref_names.h" | |
20 | |
21 // TODO(csilv): | |
22 // Much of this implementation is based on the classes defined in | |
23 // extension_bookmarks_module.cc. Longer term we should consider migrating | |
24 // NTP into an embedded extension which would allow us to leverage the same | |
25 // bookmark APIs as the bookmark manager. | |
26 | |
27 namespace keys = bookmark_extension_api_constants; | |
28 | |
29 BookmarksHandler::BookmarksHandler() : model_(NULL), | |
30 dom_ready_(false), | |
31 from_current_page_(false) { | |
32 } | |
33 | |
34 BookmarksHandler::~BookmarksHandler() { | |
35 if (model_) | |
36 model_->RemoveObserver(this); | |
37 } | |
38 | |
39 WebUIMessageHandler* BookmarksHandler::Attach(WebUI* web_ui) { | |
40 WebUIMessageHandler::Attach(web_ui); | |
41 model_ = Profile::FromWebUI(web_ui)->GetBookmarkModel(); | |
42 if (model_) | |
43 model_->AddObserver(this); | |
44 return this; | |
45 } | |
46 | |
47 void BookmarksHandler::RegisterMessages() { | |
48 web_ui_->RegisterMessageCallback("createBookmark", | |
49 base::Bind(&BookmarksHandler::HandleCreateBookmark, | |
50 base::Unretained(this))); | |
51 web_ui_->RegisterMessageCallback("getBookmarksData", | |
52 base::Bind(&BookmarksHandler::HandleGetBookmarksData, | |
53 base::Unretained(this))); | |
54 web_ui_->RegisterMessageCallback("moveBookmark", | |
55 base::Bind(&BookmarksHandler::HandleMoveBookmark, | |
56 base::Unretained(this))); | |
57 web_ui_->RegisterMessageCallback("removeBookmark", | |
58 base::Bind(&BookmarksHandler::HandleRemoveBookmark, | |
59 base::Unretained(this))); | |
60 } | |
61 | |
62 void BookmarksHandler::Loaded(BookmarkModel* model, bool ids_reassigned) { | |
63 if (dom_ready_) | |
64 HandleGetBookmarksData(NULL); | |
65 } | |
66 | |
67 void BookmarksHandler::BookmarkModelBeingDeleted(BookmarkModel* model) { | |
68 // If this occurs it probably means that this tab will close shortly. | |
69 // Discard our reference to the model so that we won't use it again. | |
70 model_ = NULL; | |
71 } | |
72 | |
73 void BookmarksHandler::BookmarkNodeMoved(BookmarkModel* model, | |
74 const BookmarkNode* old_parent, | |
75 int old_index, | |
76 const BookmarkNode* new_parent, | |
77 int new_index) { | |
78 if (!dom_ready_) return; | |
79 const BookmarkNode* node = new_parent->GetChild(new_index); | |
80 base::StringValue id(base::Int64ToString(node->id())); | |
81 base::DictionaryValue move_info; | |
82 move_info.SetString(keys::kParentIdKey, | |
83 base::Int64ToString(new_parent->id())); | |
84 move_info.SetInteger(keys::kIndexKey, new_index); | |
85 move_info.SetString(keys::kOldParentIdKey, | |
86 base::Int64ToString(old_parent->id())); | |
87 move_info.SetInteger(keys::kOldIndexKey, old_index); | |
88 base::FundamentalValue from_page(from_current_page_); | |
89 | |
90 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeMoved", id, move_info, | |
91 from_page); | |
92 } | |
93 | |
94 void BookmarksHandler::BookmarkNodeAdded(BookmarkModel* model, | |
95 const BookmarkNode* parent, | |
96 int index) { | |
97 if (!dom_ready_) return; | |
98 const BookmarkNode* node = parent->GetChild(index); | |
99 base::StringValue id(base::Int64ToString(node->id())); | |
100 scoped_ptr<base::DictionaryValue> node_info(GetNodeDictionary(node)); | |
101 base::FundamentalValue from_page(from_current_page_); | |
102 | |
103 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeAdded", id, *node_info, | |
104 from_page); | |
105 } | |
106 | |
107 void BookmarksHandler::BookmarkNodeRemoved(BookmarkModel* model, | |
108 const BookmarkNode* parent, | |
109 int index, | |
110 const BookmarkNode* node) { | |
111 if (!dom_ready_) return; | |
112 | |
113 base::StringValue id(base::Int64ToString(node->id())); | |
114 base::DictionaryValue remove_info; | |
115 remove_info.SetString(keys::kParentIdKey, base::Int64ToString(parent->id())); | |
116 remove_info.SetInteger(keys::kIndexKey, index); | |
117 base::FundamentalValue from_page(from_current_page_); | |
118 | |
119 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeRemoved", id, remove_info, | |
120 from_page); | |
121 } | |
122 | |
123 void BookmarksHandler::BookmarkNodeChanged(BookmarkModel* model, | |
124 const BookmarkNode* node) { | |
125 if (!dom_ready_) return; | |
126 base::StringValue id(base::Int64ToString(node->id())); | |
127 base::DictionaryValue change_info; | |
128 change_info.SetString(keys::kTitleKey, node->GetTitle()); | |
129 if (node->is_url()) | |
130 change_info.SetString(keys::kUrlKey, node->url().spec()); | |
131 base::FundamentalValue from_page(from_current_page_); | |
132 | |
133 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeChanged", id, change_info, | |
134 from_page); | |
135 } | |
136 | |
137 void BookmarksHandler::BookmarkNodeFaviconChanged(BookmarkModel* model, | |
138 const BookmarkNode* node) { | |
139 // Favicons are handled by through use of the chrome://favicon protocol, so | |
140 // there's nothing for us to do here (but we need to provide an | |
141 // implementation). | |
142 } | |
143 | |
144 void BookmarksHandler::BookmarkNodeChildrenReordered(BookmarkModel* model, | |
145 const BookmarkNode* node) { | |
146 if (!dom_ready_) return; | |
147 base::StringValue id(base::Int64ToString(node->id())); | |
148 base::ListValue* children = new base::ListValue; | |
149 for (int i = 0; i < node->child_count(); ++i) { | |
150 const BookmarkNode* child = node->GetChild(i); | |
151 Value* child_id = new StringValue(base::Int64ToString(child->id())); | |
152 children->Append(child_id); | |
153 } | |
154 base::DictionaryValue reorder_info; | |
155 reorder_info.Set(keys::kChildIdsKey, children); | |
156 base::FundamentalValue from_page(from_current_page_); | |
157 | |
158 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeChildrenReordered", id, | |
159 reorder_info, from_page); | |
160 } | |
161 | |
162 void BookmarksHandler::BookmarkImportBeginning(BookmarkModel* model) { | |
163 if (dom_ready_) | |
164 web_ui_->CallJavascriptFunction("ntp4.bookmarkImportBegan"); | |
165 } | |
166 | |
167 void BookmarksHandler::BookmarkImportEnding(BookmarkModel* model) { | |
168 if (dom_ready_) | |
169 web_ui_->CallJavascriptFunction("ntp4.bookmarkImportEnded"); | |
170 } | |
171 | |
172 void BookmarksHandler::HandleCreateBookmark(const ListValue* args) { | |
173 if (!model_) return; | |
174 | |
175 // This is the only required argument - a stringified int64 parent ID. | |
176 std::string parent_id_string; | |
177 CHECK(args->GetString(0, &parent_id_string)); | |
178 int64 parent_id; | |
179 CHECK(base::StringToInt64(parent_id_string, &parent_id)); | |
180 | |
181 const BookmarkNode* parent = model_->GetNodeByID(parent_id); | |
182 if (!parent) return; | |
183 | |
184 double index; | |
185 if (!args->GetDouble(1, &index) || | |
186 (index > parent->child_count() || index < 0)) { | |
187 index = parent->child_count(); | |
188 } | |
189 | |
190 // If they didn't pass the argument, just use a blank string. | |
191 string16 title; | |
192 if (!args->GetString(2, &title)) | |
193 title = string16(); | |
194 | |
195 // We'll be creating either a bookmark or a folder, so set this for both. | |
196 AutoReset<bool> from_page(&from_current_page_, true); | |
197 | |
198 // According to the bookmarks API, "If url is NULL or missing, it will be a | |
199 // folder.". Let's just follow the same behavior. | |
200 std::string url_string; | |
201 if (args->GetString(3, &url_string)) { | |
202 GURL url(url_string); | |
203 if (!url.is_empty() && url.is_valid()) { | |
204 // Only valid case for a bookmark as opposed to folder. | |
205 model_->AddURL(parent, static_cast<int>(index), title, url); | |
206 return; | |
207 } | |
208 } | |
209 | |
210 // We didn't have all the valid parts for a bookmark, just make a folder. | |
211 model_->AddFolder(parent, static_cast<int>(index), title); | |
212 } | |
213 | |
214 void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) { | |
215 dom_ready_ = true; | |
216 | |
217 // At startup, Bookmarks may not be fully loaded. If this is the case, | |
218 // we'll wait for the notification to arrive. | |
219 Profile* profile = Profile::FromWebUI(web_ui_); | |
220 BookmarkModel* model = profile->GetBookmarkModel(); | |
221 if (!model->IsLoaded()) return; | |
222 | |
223 int64 id; | |
224 std::string id_string; | |
225 PrefService* prefs = profile->GetPrefs(); | |
226 if (args && args->GetString(0, &id_string) && | |
227 base::StringToInt64(id_string, &id)) { | |
228 // A folder ID was requested, so persist this value. | |
229 prefs->SetInt64(prefs::kNTPShownBookmarksFolder, id); | |
230 } else { | |
231 // No folder ID was requested, so get the default (persisted) value. | |
232 id = prefs->GetInt64(prefs::kNTPShownBookmarksFolder); | |
233 } | |
234 | |
235 const BookmarkNode* node = model->GetNodeByID(id); | |
236 if (!node) | |
237 node = model->root_node(); | |
238 | |
239 // We wish to merge the root node with the bookmarks bar node. | |
240 if (model->is_root_node(node)) | |
241 node = model->bookmark_bar_node(); | |
242 | |
243 base::ListValue* items = new base::ListValue; | |
244 for (int i = 0; i < node->child_count(); ++i) { | |
245 const BookmarkNode* child = node->GetChild(i); | |
246 AddNode(child, items); | |
247 } | |
248 if (node == model->bookmark_bar_node() && model->other_node()->child_count()) | |
249 AddNode(model->other_node(), items); | |
250 | |
251 base::ListValue* navigation_items = new base::ListValue; | |
252 while (node) { | |
253 if (node != model->bookmark_bar_node()) | |
254 AddNode(node, navigation_items); | |
255 node = node->parent(); | |
256 } | |
257 | |
258 base::DictionaryValue bookmarksData; | |
259 bookmarksData.Set("items", items); | |
260 bookmarksData.Set("navigationItems", navigation_items); | |
261 web_ui_->CallJavascriptFunction("ntp4.setBookmarksData", bookmarksData); | |
262 } | |
263 | |
264 void BookmarksHandler::HandleRemoveBookmark(const ListValue* args) { | |
265 if (!model_) return; | |
266 | |
267 std::string id_string; | |
268 CHECK(args->GetString(0, &id_string)); | |
269 int64 id; | |
270 CHECK(base::StringToInt64(id_string, &id)); | |
271 | |
272 const BookmarkNode* node = model_->GetNodeByID(id); | |
273 if (!node) return; | |
274 | |
275 AutoReset<bool> from_page(&from_current_page_, true); | |
276 model_->Remove(node->parent(), node->parent()->GetIndexOf(node)); | |
277 } | |
278 | |
279 void BookmarksHandler::HandleMoveBookmark(const ListValue* args) { | |
280 if (!model_) return; | |
281 | |
282 std::string id_string; | |
283 CHECK(args->GetString(0, &id_string)); | |
284 int64 id; | |
285 CHECK(base::StringToInt64(id_string, &id)); | |
286 | |
287 std::string parent_id_string; | |
288 CHECK(args->GetString(1, &parent_id_string)); | |
289 int64 parent_id; | |
290 CHECK(base::StringToInt64(parent_id_string, &parent_id)); | |
291 | |
292 double index; | |
293 args->GetDouble(2, &index); | |
294 | |
295 const BookmarkNode* parent = model_->GetNodeByID(parent_id); | |
296 if (!parent) return; | |
297 | |
298 const BookmarkNode* node = model_->GetNodeByID(id); | |
299 if (!node) return; | |
300 | |
301 AutoReset<bool> from_page(&from_current_page_, true); | |
302 model_->Move(node, parent, static_cast<int>(index)); | |
303 } | |
304 | |
305 base::DictionaryValue* BookmarksHandler::GetNodeDictionary( | |
306 const BookmarkNode* node) { | |
307 base::DictionaryValue* dict = new base::DictionaryValue(); | |
308 dict->SetString(keys::kIdKey, base::Int64ToString(node->id())); | |
309 | |
310 const BookmarkNode* parent = node->parent(); | |
311 if (parent) { | |
312 dict->SetString(keys::kParentIdKey, base::Int64ToString(parent->id())); | |
313 dict->SetInteger(keys::kIndexKey, parent->GetIndexOf(node)); | |
314 } | |
315 | |
316 NewTabUI::SetURLTitleAndDirection(dict, node->GetTitle(), node->url()); | |
317 | |
318 if (!node->is_folder()) | |
319 dict->SetString(keys::kUrlKey, node->url().spec()); | |
320 | |
321 return dict; | |
322 } | |
323 | |
324 void BookmarksHandler::AddNode(const BookmarkNode* node, | |
325 base::ListValue* list) { | |
326 if (node->IsVisible()) { | |
327 base::DictionaryValue* dict = GetNodeDictionary(node); | |
328 list->Append(dict); | |
329 } | |
330 } | |
331 | |
332 // static | |
333 void BookmarksHandler::RegisterUserPrefs(PrefService* prefs) { | |
334 // Default folder is the root node. | |
335 // TODO(csilv): Should we sync this preference? | |
336 prefs->RegisterInt64Pref(prefs::kNTPShownBookmarksFolder, 0, | |
337 PrefService::UNSYNCABLE_PREF); | |
338 } | |
OLD | NEW |