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::Loaded(BookmarkModel* model, bool ids_reassigned) { |
30 const NotificationSource& source, | 49 if (getBookmarksDataIsPending_) { |
31 const NotificationDetails& details) { | 50 HandleGetBookmarksData(NULL); |
32 // TODO(csilv): Update UI based on changes to bookmark notifications. | 51 getBookmarksDataIsPending_ = false; |
33 switch (type) { | |
34 case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: { | |
35 registrar_.Remove(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, | |
36 Source<Profile>(Profile::FromWebUI(web_ui_))); | |
37 HandleGetBookmarksData(NULL); | |
38 break; | |
39 } | |
40 } | 52 } |
41 } | 53 } |
42 | 54 |
55 void BookmarksHandler::BookmarkModelBeingDeleted(BookmarkModel* model) { | |
56 // If this occurs it probably means that this tab will close shortly. | |
57 // Discard our reference to the model so that we won't use it again. | |
58 model_ = NULL; | |
59 } | |
60 | |
61 void BookmarksHandler::BookmarkNodeMoved(BookmarkModel* model, | |
62 const BookmarkNode* old_parent, int old_index, | |
63 const BookmarkNode* new_parent, int new_index) { | |
64 const BookmarkNode* node = new_parent->GetChild(new_index); | |
65 StringValue id(base::Int64ToString(node->id())); | |
66 DictionaryValue move_info; | |
67 move_info.SetString(keys::kParentIdKey, | |
68 base::Int64ToString(new_parent->id())); | |
69 move_info.SetInteger(keys::kIndexKey, new_index); | |
70 move_info.SetString(keys::kOldParentIdKey, | |
71 base::Int64ToString(old_parent->id())); | |
72 move_info.SetInteger(keys::kOldIndexKey, old_index); | |
73 | |
74 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeMoved", id, move_info); | |
75 } | |
76 | |
77 void BookmarksHandler::BookmarkNodeAdded(BookmarkModel* model, | |
78 const BookmarkNode* parent, int index) { | |
79 const BookmarkNode* node = parent->GetChild(index); | |
80 StringValue id(base::Int64ToString(node->id())); | |
81 scoped_ptr<DictionaryValue> node_info( | |
82 extension_bookmark_helpers::GetNodeDictionary(node, false, false)); | |
83 | |
84 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeAdded", id, *node_info); | |
85 } | |
86 | |
87 void BookmarksHandler::BookmarkNodeRemoved(BookmarkModel* model, | |
88 const BookmarkNode* parent, int index, const BookmarkNode* node) { | |
89 StringValue id(base::Int64ToString(node->id())); | |
90 DictionaryValue remove_info; | |
91 remove_info.SetString(keys::kParentIdKey, | |
92 base::Int64ToString(parent->id())); | |
93 remove_info.SetInteger(keys::kIndexKey, index); | |
94 | |
95 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeRemoved", id, remove_info); | |
96 } | |
97 | |
98 void BookmarksHandler::BookmarkNodeChanged(BookmarkModel* model, | |
99 const BookmarkNode* node) { | |
100 StringValue id(base::Int64ToString(node->id())); | |
101 DictionaryValue change_info; | |
102 change_info.SetString(keys::kTitleKey, node->GetTitle()); | |
103 if (node->is_url()) | |
104 change_info.SetString(keys::kUrlKey, node->url().spec()); | |
105 | |
106 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeChanged", id, change_info); | |
107 } | |
108 | |
109 void BookmarksHandler::BookmarkNodeFaviconChanged(BookmarkModel* model, | |
110 const BookmarkNode* node) { | |
111 // Favicons are handled by through use of the chrome://favicon protocol, so | |
112 // there's nothing for us to do here (but we need to provide an | |
113 // implementation). | |
114 } | |
115 | |
116 void BookmarksHandler::BookmarkNodeChildrenReordered(BookmarkModel* model, | |
117 const BookmarkNode* node) { | |
118 StringValue id(base::Int64ToString(node->id())); | |
119 int childCount = node->child_count(); | |
120 ListValue* children = new ListValue(); | |
121 for (int i = 0; i < childCount; ++i) { | |
122 const BookmarkNode* child = node->GetChild(i); | |
123 Value* child_id = new StringValue(base::Int64ToString(child->id())); | |
124 children->Append(child_id); | |
125 } | |
126 DictionaryValue reorder_info; | |
127 reorder_info.Set(keys::kChildIdsKey, children); | |
128 | |
129 web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeChildrenReordered", id, | |
130 reorder_info); | |
131 } | |
132 | |
133 void BookmarksHandler::BookmarkImportBeginning(BookmarkModel* model) { | |
134 web_ui_->CallJavascriptFunction("ntp4.bookmarkImportBegan"); | |
135 } | |
136 | |
137 void BookmarksHandler::BookmarkImportEnding(BookmarkModel* model) { | |
138 web_ui_->CallJavascriptFunction("ntp4.bookmarkImportEnded"); | |
139 } | |
140 | |
43 void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) { | 141 void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) { |
44 // At startup, Bookmarks may not be fully loaded. If this is the case, | 142 // At startup, Bookmarks may not be fully loaded. If this is the case, |
45 // we'll wait for the notification to arrive. | 143 // we'll wait for the notification to arrive. |
46 Profile* profile = Profile::FromWebUI(web_ui_); | 144 Profile* profile = Profile::FromWebUI(web_ui_); |
47 BookmarkModel* model = profile->GetBookmarkModel(); | 145 BookmarkModel* model = profile->GetBookmarkModel(); |
48 if (!model->IsLoaded()) { | 146 if (!model->IsLoaded()) { |
49 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, | 147 getBookmarksDataIsPending_ = true; |
Evan Stade
2011/08/26 23:54:37
I don't really think this is necessary. Loaded onl
csilv
2011/08/29 17:54:03
Yes, it only happens once. But the problem is tha
Evan Stade
2011/08/29 18:17:03
I'm not saying the model->IsLoaded() check is unne
csilv
2011/08/29 19:18:48
It's important to have. Without that flag, the Bo
| |
50 Source<Profile>(profile)); | |
51 return; | 148 return; |
52 } | 149 } |
53 | 150 |
54 int64 id; | 151 int64 id; |
55 std::string id_string; | 152 std::string id_string; |
56 PrefService* prefs = profile->GetPrefs(); | 153 PrefService* prefs = profile->GetPrefs(); |
57 if (args && args->GetString(0, &id_string) && | 154 if (args && args->GetString(0, &id_string) && |
58 base::StringToInt64(id_string, &id)) { | 155 base::StringToInt64(id_string, &id)) { |
59 // A folder ID was requested, so persist this value. | 156 // A folder ID was requested, so persist this value. |
60 prefs->SetInt64(prefs::kNTPShownBookmarksFolder, id); | 157 prefs->SetInt64(prefs::kNTPShownBookmarksFolder, id); |
61 } else { | 158 } else { |
62 // No folder ID was requested, so get the default (persisted) value. | 159 // No folder ID was requested, so get the default (persisted) value. |
63 id = prefs->GetInt64(prefs::kNTPShownBookmarksFolder); | 160 id = prefs->GetInt64(prefs::kNTPShownBookmarksFolder); |
64 } | 161 } |
65 | 162 |
66 const BookmarkNode* node = model->GetNodeByID(id); | 163 const BookmarkNode* node = model->GetNodeByID(id); |
67 if (!node) | 164 if (!node) |
68 return; | 165 node = model->root_node(); |
69 | 166 |
70 // We wish to merge the root node with the bookmarks bar node. | 167 // We wish to merge the root node with the bookmarks bar node. |
71 if (model->is_root_node(node)) | 168 if (model->is_root_node(node)) |
72 node = model->bookmark_bar_node(); | 169 node = model->bookmark_bar_node(); |
73 | 170 |
74 base::ListValue* items = new base::ListValue(); | 171 base::ListValue* items = new base::ListValue(); |
75 int child_count = node->child_count(); | 172 int child_count = node->child_count(); |
76 for (int i = 0; i < child_count; ++i) { | 173 for (int i = 0; i < child_count; ++i) { |
77 const BookmarkNode* child = node->GetChild(i); | 174 const BookmarkNode* child = node->GetChild(i); |
78 extension_bookmark_helpers::AddNode(child, items, false); | 175 extension_bookmark_helpers::AddNode(child, items, false); |
(...skipping 10 matching lines...) Expand all Loading... | |
89 | 186 |
90 base::DictionaryValue bookmarksData; | 187 base::DictionaryValue bookmarksData; |
91 bookmarksData.Set("items", items); | 188 bookmarksData.Set("items", items); |
92 bookmarksData.Set("navigationItems", navigation_items); | 189 bookmarksData.Set("navigationItems", navigation_items); |
93 web_ui_->CallJavascriptFunction("ntp4.setBookmarksData", bookmarksData); | 190 web_ui_->CallJavascriptFunction("ntp4.setBookmarksData", bookmarksData); |
94 } | 191 } |
95 | 192 |
96 // static | 193 // static |
97 void BookmarksHandler::RegisterUserPrefs(PrefService* prefs) { | 194 void BookmarksHandler::RegisterUserPrefs(PrefService* prefs) { |
98 // Default folder is the root node. | 195 // Default folder is the root node. |
99 // TODO(csilv): Should we default to the Bookmarks bar? | |
100 // TODO(csilv): Should we sync this preference? | 196 // TODO(csilv): Should we sync this preference? |
101 prefs->RegisterInt64Pref(prefs::kNTPShownBookmarksFolder, 0, | 197 prefs->RegisterInt64Pref(prefs::kNTPShownBookmarksFolder, 0, |
102 PrefService::UNSYNCABLE_PREF); | 198 PrefService::UNSYNCABLE_PREF); |
103 } | 199 } |
OLD | NEW |