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/prefs/pref_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/sync/profile_sync_service.h" |
7 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/pref_names.h" |
8 #include "content/common/notification_service.h" | 16 #include "content/common/notification_service.h" |
9 | 17 |
10 BookmarksHandler::BookmarksHandler() { | 18 BookmarksHandler::BookmarksHandler() { |
| 19 // TODO(csilv): Register for bookmark model change notifications. |
11 } | 20 } |
12 | 21 |
13 BookmarksHandler::~BookmarksHandler() {} | 22 BookmarksHandler::~BookmarksHandler() {} |
14 | 23 |
15 void BookmarksHandler::RegisterMessages() { | 24 void BookmarksHandler::RegisterMessages() { |
| 25 web_ui_->RegisterMessageCallback("getBookmarksData", |
| 26 NewCallback(this, &BookmarksHandler::HandleGetBookmarksData)); |
16 } | 27 } |
17 | 28 |
18 void BookmarksHandler::Observe(int type, | 29 void BookmarksHandler::Observe(int type, |
19 const NotificationSource& source, | 30 const NotificationSource& source, |
20 const NotificationDetails& details) { | 31 const NotificationDetails& details) { |
| 32 // TODO(csilv): Update UI based on changes to bookmark notifications. |
21 } | 33 } |
| 34 |
| 35 void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) { |
| 36 int64 id; |
| 37 std::string id_string; |
| 38 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); |
| 39 if (args->GetString(0, &id_string) && base::StringToInt64(id_string, &id)) { |
| 40 // A folder ID was requested, so persist this value. |
| 41 prefs->SetInt64(prefs::kNTPShownBookmarksFolder, id); |
| 42 } else { |
| 43 // No folder ID was requested, so get the default (persisted) value. |
| 44 id = prefs->GetInt64(prefs::kNTPShownBookmarksFolder); |
| 45 } |
| 46 |
| 47 BookmarkModel* model = Profile::FromWebUI(web_ui_)->GetBookmarkModel(); |
| 48 const BookmarkNode* node = model->GetNodeByID(id); |
| 49 if (!node) |
| 50 return; |
| 51 |
| 52 base::ListValue* items = new base::ListValue(); |
| 53 int child_count = node->child_count(); |
| 54 for (int i = 0; i < child_count; ++i) { |
| 55 const BookmarkNode* child = node->GetChild(i); |
| 56 extension_bookmark_helpers::AddNode(child, items, false); |
| 57 } |
| 58 |
| 59 base::ListValue* navigation_items = new base::ListValue(); |
| 60 while (node) { |
| 61 extension_bookmark_helpers::AddNode(node, navigation_items, false); |
| 62 node = node->parent(); |
| 63 } |
| 64 |
| 65 base::DictionaryValue bookmarksData; |
| 66 bookmarksData.Set("items", items); |
| 67 bookmarksData.Set("navigationItems", navigation_items); |
| 68 web_ui_->CallJavascriptFunction("ntp4.setBookmarksData", bookmarksData); |
| 69 } |
| 70 |
| 71 // static |
| 72 void BookmarksHandler::RegisterUserPrefs(PrefService* prefs) { |
| 73 // Default folder is the root node. |
| 74 // TODO(csilv): Should we default to the Bookmarks bar? |
| 75 // TODO(csilv): Should we sync this preference? |
| 76 prefs->RegisterInt64Pref(prefs::kNTPShownBookmarksFolder, 0, |
| 77 PrefService::UNSYNCABLE_PREF); |
| 78 } |
OLD | NEW |