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" | 7 #include "base/string_number_conversions.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/bookmarks/bookmark_model.h" | 9 #include "chrome/browser/bookmarks/bookmark_model.h" |
10 #include "chrome/browser/extensions/extension_bookmark_helpers.h" | 10 #include "chrome/browser/extensions/extension_bookmark_helpers.h" |
11 #include "chrome/browser/extensions/extension_bookmarks_module_constants.h" | |
11 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/sync/profile_sync_service.h" | 14 #include "chrome/browser/sync/profile_sync_service.h" |
14 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
16 #include "content/common/notification_service.h" | 17 #include "content/common/notification_service.h" |
17 | 18 |
19 // TODO(csilv): | |
20 // Much of this implementation is based on the classes defined in | |
21 // extension_bookmarks_module.cc. Longer term we should consider migrating | |
22 // NTP into an embedded extension which would allow us to leverage the same | |
23 // bookmark APIs as the bookmark manager. | |
24 | |
25 namespace keys = extension_bookmarks_module_constants; | |
26 | |
18 BookmarksHandler::BookmarksHandler() { | 27 BookmarksHandler::BookmarksHandler() { |
19 // TODO(csilv): Register for bookmark model change notifications. | |
20 } | 28 } |
21 | 29 |
22 BookmarksHandler::~BookmarksHandler() {} | 30 BookmarksHandler::~BookmarksHandler() { |
31 if (model_) | |
32 model_->RemoveObserver(this); | |
33 } | |
34 | |
35 WebUIMessageHandler* BookmarksHandler::Attach(WebUI* web_ui) { | |
36 WebUIMessageHandler::Attach(web_ui); | |
37 model_ = Profile::FromWebUI(web_ui)->GetBookmarkModel(); | |
38 if (model_) | |
39 model_->AddObserver(this); | |
40 return this; | |
41 } | |
23 | 42 |
24 void BookmarksHandler::RegisterMessages() { | 43 void BookmarksHandler::RegisterMessages() { |
25 web_ui_->RegisterMessageCallback("getBookmarksData", | 44 web_ui_->RegisterMessageCallback("getBookmarksData", |
26 NewCallback(this, &BookmarksHandler::HandleGetBookmarksData)); | 45 NewCallback(this, &BookmarksHandler::HandleGetBookmarksData)); |
27 } | 46 } |
28 | 47 |
29 void BookmarksHandler::Observe(int type, | 48 void BookmarksHandler::Observe(int type, |
30 const NotificationSource& source, | 49 const NotificationSource& source, |
31 const NotificationDetails& details) { | 50 const NotificationDetails& details) { |
32 // TODO(csilv): Update UI based on changes to bookmark notifications. | |
33 switch (type) { | 51 switch (type) { |
34 case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: { | 52 case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: { |
35 registrar_.Remove(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, | 53 registrar_.Remove(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, |
36 Source<Profile>(Profile::FromWebUI(web_ui_))); | 54 Source<Profile>(Profile::FromWebUI(web_ui_))); |
37 HandleGetBookmarksData(NULL); | 55 HandleGetBookmarksData(NULL); |
38 break; | 56 break; |
39 } | 57 } |
40 } | 58 } |
41 } | 59 } |
42 | 60 |
61 void BookmarksHandler::Loaded(BookmarkModel* model, bool ids_reassigned) { | |
62 // Nothing to do here, we register for a LOADED notification instead. | |
63 } | |
64 | |
65 void BookmarksHandler::BookmarkModelBeingDeleted(BookmarkModel* model) { | |
66 // If this occurs it probably means that this tab will close shortly. | |
67 // Discard our reference to the model so that we won't use it again. | |
68 model_ = NULL; | |
69 } | |
70 | |
71 void BookmarksHandler::BookmarkNodeMoved(BookmarkModel* model, | |
72 const BookmarkNode* old_parent, int old_index, | |
73 const BookmarkNode* new_parent, int new_index) { | |
74 const BookmarkNode* node = new_parent->GetChild(new_index); | |
75 StringValue id(base::Int64ToString(node->id())); | |
76 DictionaryValue move_info; | |
77 move_info.SetString(keys::kParentIdKey, | |
78 base::Int64ToString(new_parent->id())); | |
79 move_info.SetInteger(keys::kIndexKey, new_index); | |
80 move_info.SetString(keys::kOldParentIdKey, | |
81 base::Int64ToString(old_parent->id())); | |
82 move_info.SetInteger(keys::kOldIndexKey, old_index); | |
83 | |
84 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeMoved", id, move_info); | |
85 } | |
86 | |
87 void BookmarksHandler::BookmarkNodeAdded(BookmarkModel* model, | |
88 const BookmarkNode* parent, int index) { | |
89 const BookmarkNode* node = parent->GetChild(index); | |
90 StringValue id(base::Int64ToString(node->id())); | |
91 scoped_ptr<DictionaryValue> node_info( | |
92 extension_bookmark_helpers::GetNodeDictionary(node, false, false)); | |
93 | |
94 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeAdded", id, *node_info); | |
95 } | |
96 | |
97 void BookmarksHandler::BookmarkNodeRemoved(BookmarkModel* model, | |
98 const BookmarkNode* parent, int index, const BookmarkNode* node) { | |
99 StringValue id(base::Int64ToString(node->id())); | |
100 DictionaryValue remove_info; | |
101 remove_info.SetString(keys::kParentIdKey, | |
102 base::Int64ToString(parent->id())); | |
103 remove_info.SetInteger(keys::kIndexKey, index); | |
104 | |
105 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeRemoved", id, remove_info); | |
106 } | |
107 | |
108 void BookmarksHandler::BookmarkNodeChanged(BookmarkModel* model, | |
109 const BookmarkNode* node) { | |
110 StringValue id(base::Int64ToString(node->id())); | |
111 DictionaryValue change_info; | |
112 change_info.SetString(keys::kTitleKey, node->GetTitle()); | |
113 if (node->is_url()) | |
114 change_info.SetString(keys::kUrlKey, node->url().spec()); | |
115 | |
116 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeChanged", id, change_info); | |
117 } | |
118 | |
119 void BookmarksHandler::BookmarkNodeFaviconChanged(BookmarkModel* model, | |
120 const BookmarkNode* node) { | |
121 // Ignored. | |
Evan Stade
2011/08/23 21:50:50
explain why or add a TODO
csilv
2011/08/24 23:15:11
Done.
| |
122 } | |
123 | |
124 void BookmarksHandler::BookmarkNodeChildrenReordered(BookmarkModel* model, | |
125 const BookmarkNode* node) { | |
126 StringValue id(base::Int64ToString(node->id())); | |
127 int childCount = node->child_count(); | |
128 ListValue* children = new ListValue(); | |
129 for (int i = 0; i < childCount; ++i) { | |
130 const BookmarkNode* child = node->GetChild(i); | |
131 Value* child_id = new StringValue(base::Int64ToString(child->id())); | |
132 children->Append(child_id); | |
133 } | |
134 DictionaryValue reorder_info; | |
135 reorder_info.Set(keys::kChildIdsKey, children); | |
136 | |
137 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeChildrenReordered", id, | |
138 reorder_info); | |
139 } | |
140 | |
141 void BookmarksHandler::BookmarkImportBeginning(BookmarkModel* model) { | |
142 web_ui_->CallJavascriptFunction("ntp4.bookmarkImportBegan"); | |
143 } | |
144 | |
145 void BookmarksHandler::BookmarkImportEnding(BookmarkModel* model) { | |
146 web_ui_->CallJavascriptFunction("ntp4.bookmarkImportEnded"); | |
147 } | |
148 | |
43 void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) { | 149 void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) { |
44 // At startup, Bookmarks may not be fully loaded. If this is the case, | 150 // At startup, Bookmarks may not be fully loaded. If this is the case, |
45 // we'll wait for the notification to arrive. | 151 // we'll wait for the notification to arrive. |
46 Profile* profile = Profile::FromWebUI(web_ui_); | 152 Profile* profile = Profile::FromWebUI(web_ui_); |
47 BookmarkModel* model = profile->GetBookmarkModel(); | 153 BookmarkModel* model = profile->GetBookmarkModel(); |
48 if (!model->IsLoaded()) { | 154 if (!model->IsLoaded()) { |
49 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, | 155 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, |
50 Source<Profile>(profile)); | 156 Source<Profile>(profile)); |
51 return; | 157 return; |
52 } | 158 } |
53 | 159 |
54 int64 id; | 160 int64 id; |
55 std::string id_string; | 161 std::string id_string; |
56 PrefService* prefs = profile->GetPrefs(); | 162 PrefService* prefs = profile->GetPrefs(); |
57 if (args && args->GetString(0, &id_string) && | 163 if (args && args->GetString(0, &id_string) && |
58 base::StringToInt64(id_string, &id)) { | 164 base::StringToInt64(id_string, &id)) { |
59 // A folder ID was requested, so persist this value. | 165 // A folder ID was requested, so persist this value. |
60 prefs->SetInt64(prefs::kNTPShownBookmarksFolder, id); | 166 prefs->SetInt64(prefs::kNTPShownBookmarksFolder, id); |
61 } else { | 167 } else { |
62 // No folder ID was requested, so get the default (persisted) value. | 168 // No folder ID was requested, so get the default (persisted) value. |
63 id = prefs->GetInt64(prefs::kNTPShownBookmarksFolder); | 169 id = prefs->GetInt64(prefs::kNTPShownBookmarksFolder); |
64 } | 170 } |
65 | 171 |
66 const BookmarkNode* node = model->GetNodeByID(id); | 172 const BookmarkNode* node = model->GetNodeByID(id); |
67 if (!node) | 173 if (!node) |
68 return; | 174 node = model->root_node(); |
69 | 175 |
70 // We wish to merge the root node with the bookmarks bar node. | 176 // We wish to merge the root node with the bookmarks bar node. |
71 if (model->is_root_node(node)) | 177 if (model->is_root_node(node)) |
72 node = model->bookmark_bar_node(); | 178 node = model->bookmark_bar_node(); |
73 | 179 |
74 base::ListValue* items = new base::ListValue(); | 180 base::ListValue* items = new base::ListValue(); |
75 int child_count = node->child_count(); | 181 int child_count = node->child_count(); |
76 for (int i = 0; i < child_count; ++i) { | 182 for (int i = 0; i < child_count; ++i) { |
77 const BookmarkNode* child = node->GetChild(i); | 183 const BookmarkNode* child = node->GetChild(i); |
78 extension_bookmark_helpers::AddNode(child, items, false); | 184 extension_bookmark_helpers::AddNode(child, items, false); |
(...skipping 10 matching lines...) Expand all Loading... | |
89 | 195 |
90 base::DictionaryValue bookmarksData; | 196 base::DictionaryValue bookmarksData; |
91 bookmarksData.Set("items", items); | 197 bookmarksData.Set("items", items); |
92 bookmarksData.Set("navigationItems", navigation_items); | 198 bookmarksData.Set("navigationItems", navigation_items); |
93 web_ui_->CallJavascriptFunction("ntp4.setBookmarksData", bookmarksData); | 199 web_ui_->CallJavascriptFunction("ntp4.setBookmarksData", bookmarksData); |
94 } | 200 } |
95 | 201 |
96 // static | 202 // static |
97 void BookmarksHandler::RegisterUserPrefs(PrefService* prefs) { | 203 void BookmarksHandler::RegisterUserPrefs(PrefService* prefs) { |
98 // Default folder is the root node. | 204 // Default folder is the root node. |
99 // TODO(csilv): Should we default to the Bookmarks bar? | |
100 // TODO(csilv): Should we sync this preference? | 205 // TODO(csilv): Should we sync this preference? |
101 prefs->RegisterInt64Pref(prefs::kNTPShownBookmarksFolder, 0, | 206 prefs->RegisterInt64Pref(prefs::kNTPShownBookmarksFolder, 0, |
102 PrefService::UNSYNCABLE_PREF); | 207 PrefService::UNSYNCABLE_PREF); |
103 } | 208 } |
OLD | NEW |