OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/ntp/bookmarks_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/bookmarks_handler.h" |
6 | 6 |
| 7 #include "base/string_number_conversions.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 10 #include "chrome/browser/extensions/extension_bookmark_helpers.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
7 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
8 #include "content/common/notification_service.h" | 13 #include "content/common/notification_service.h" |
9 | 14 |
10 BookmarksHandler::BookmarksHandler() { | 15 BookmarksHandler::BookmarksHandler() { |
| 16 // TODO(csilv): Register for bookmark model change notifications. |
11 } | 17 } |
12 | 18 |
13 BookmarksHandler::~BookmarksHandler() {} | 19 BookmarksHandler::~BookmarksHandler() {} |
14 | 20 |
15 void BookmarksHandler::RegisterMessages() { | 21 void BookmarksHandler::RegisterMessages() { |
| 22 web_ui_->RegisterMessageCallback("getBookmarksData", |
| 23 NewCallback(this, &BookmarksHandler::HandleGetBookmarksData)); |
16 } | 24 } |
17 | 25 |
18 void BookmarksHandler::Observe(int type, | 26 void BookmarksHandler::Observe(int type, |
19 const NotificationSource& source, | 27 const NotificationSource& source, |
20 const NotificationDetails& details) { | 28 const NotificationDetails& details) { |
| 29 // TODO(csilv): Update UI based on changes to bookmark notifications. |
21 } | 30 } |
| 31 |
| 32 void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) { |
| 33 int64 id; |
| 34 std::string id_string; |
| 35 CHECK(args->GetString(0, &id_string)); |
| 36 if (!base::StringToInt64(id_string, &id)) |
| 37 return; |
| 38 |
| 39 BookmarkModel* model = Profile::FromWebUI(web_ui_)->GetBookmarkModel(); |
| 40 const BookmarkNode* node = model->GetNodeByID(id); |
| 41 if (!node) |
| 42 return; |
| 43 |
| 44 base::ListValue* items = new ListValue(); |
| 45 int child_count = node->child_count(); |
| 46 for (int i = 0; i < child_count; ++i) { |
| 47 const BookmarkNode* child = node->GetChild(i); |
| 48 extension_bookmark_helpers::AddNode(child, items, false); |
| 49 } |
| 50 |
| 51 base::ListValue* navigation_items = new ListValue(); |
| 52 while (node) { |
| 53 extension_bookmark_helpers::AddNode(node, navigation_items, false); |
| 54 node = node->parent(); |
| 55 } |
| 56 |
| 57 base::DictionaryValue bookmarksData; |
| 58 bookmarksData.Set("items", items); |
| 59 bookmarksData.Set("navigationItems", navigation_items); |
| 60 web_ui_->CallJavascriptFunction("ntp4.setBookmarksData", bookmarksData); |
| 61 } |
OLD | NEW |