OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bookmarks/bookmark_extension_api.h" | 5 #include "chrome/browser/bookmarks/bookmark_extension_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/i18n/file_util_icu.h" | 9 #include "base/i18n/file_util_icu.h" |
10 #include "base/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 BookmarkExtensionEventRouter::~BookmarkExtensionEventRouter() { | 141 BookmarkExtensionEventRouter::~BookmarkExtensionEventRouter() { |
142 if (model_) { | 142 if (model_) { |
143 model_->RemoveObserver(this); | 143 model_->RemoveObserver(this); |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 void BookmarkExtensionEventRouter::Init() { | 147 void BookmarkExtensionEventRouter::Init() { |
148 model_->AddObserver(this); | 148 model_->AddObserver(this); |
149 } | 149 } |
150 | 150 |
151 void BookmarkExtensionEventRouter::DispatchEvent(Profile *profile, | 151 void BookmarkExtensionEventRouter::DispatchEvent( |
152 const char* event_name, | 152 Profile* profile, |
153 const std::string& json_args) { | 153 const char* event_name, |
| 154 scoped_ptr<ListValue> event_args) { |
154 if (profile->GetExtensionEventRouter()) { | 155 if (profile->GetExtensionEventRouter()) { |
155 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 156 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
156 event_name, json_args, NULL, GURL(), extensions::EventFilteringInfo()); | 157 event_name, event_args.Pass(), NULL, GURL(), |
| 158 extensions::EventFilteringInfo()); |
157 } | 159 } |
158 } | 160 } |
159 | 161 |
160 void BookmarkExtensionEventRouter::Loaded(BookmarkModel* model, | 162 void BookmarkExtensionEventRouter::Loaded(BookmarkModel* model, |
161 bool ids_reassigned) { | 163 bool ids_reassigned) { |
162 // TODO(erikkay): Perhaps we should send this event down to the extension | 164 // TODO(erikkay): Perhaps we should send this event down to the extension |
163 // so they know when it's safe to use the API? | 165 // so they know when it's safe to use the API? |
164 } | 166 } |
165 | 167 |
166 void BookmarkExtensionEventRouter::BookmarkModelBeingDeleted( | 168 void BookmarkExtensionEventRouter::BookmarkModelBeingDeleted( |
167 BookmarkModel* model) { | 169 BookmarkModel* model) { |
168 model_ = NULL; | 170 model_ = NULL; |
169 } | 171 } |
170 | 172 |
171 void BookmarkExtensionEventRouter::BookmarkNodeMoved( | 173 void BookmarkExtensionEventRouter::BookmarkNodeMoved( |
172 BookmarkModel* model, | 174 BookmarkModel* model, |
173 const BookmarkNode* old_parent, | 175 const BookmarkNode* old_parent, |
174 int old_index, | 176 int old_index, |
175 const BookmarkNode* new_parent, | 177 const BookmarkNode* new_parent, |
176 int new_index) { | 178 int new_index) { |
177 ListValue args; | 179 scoped_ptr<ListValue> args(new ListValue()); |
178 const BookmarkNode* node = new_parent->GetChild(new_index); | 180 const BookmarkNode* node = new_parent->GetChild(new_index); |
179 args.Append(new StringValue(base::Int64ToString(node->id()))); | 181 args->Append(new StringValue(base::Int64ToString(node->id()))); |
180 DictionaryValue* object_args = new DictionaryValue(); | 182 DictionaryValue* object_args = new DictionaryValue(); |
181 object_args->SetString(keys::kParentIdKey, | 183 object_args->SetString(keys::kParentIdKey, |
182 base::Int64ToString(new_parent->id())); | 184 base::Int64ToString(new_parent->id())); |
183 object_args->SetInteger(keys::kIndexKey, new_index); | 185 object_args->SetInteger(keys::kIndexKey, new_index); |
184 object_args->SetString(keys::kOldParentIdKey, | 186 object_args->SetString(keys::kOldParentIdKey, |
185 base::Int64ToString(old_parent->id())); | 187 base::Int64ToString(old_parent->id())); |
186 object_args->SetInteger(keys::kOldIndexKey, old_index); | 188 object_args->SetInteger(keys::kOldIndexKey, old_index); |
187 args.Append(object_args); | 189 args->Append(object_args); |
188 | 190 |
189 std::string json_args; | 191 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, args.Pass()); |
190 base::JSONWriter::Write(&args, &json_args); | |
191 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); | |
192 } | 192 } |
193 | 193 |
194 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model, | 194 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model, |
195 const BookmarkNode* parent, | 195 const BookmarkNode* parent, |
196 int index) { | 196 int index) { |
197 ListValue args; | 197 scoped_ptr<ListValue> args(new ListValue()); |
198 const BookmarkNode* node = parent->GetChild(index); | 198 const BookmarkNode* node = parent->GetChild(index); |
199 args.Append(new StringValue(base::Int64ToString(node->id()))); | 199 args->Append(new StringValue(base::Int64ToString(node->id()))); |
200 scoped_ptr<BookmarkTreeNode> tree_node( | 200 scoped_ptr<BookmarkTreeNode> tree_node( |
201 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false)); | 201 bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false)); |
202 args.Append(tree_node->ToValue().release()); | 202 args->Append(tree_node->ToValue().release()); |
203 | 203 |
204 std::string json_args; | 204 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, args.Pass()); |
205 base::JSONWriter::Write(&args, &json_args); | |
206 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args); | |
207 } | 205 } |
208 | 206 |
209 void BookmarkExtensionEventRouter::BookmarkNodeRemoved( | 207 void BookmarkExtensionEventRouter::BookmarkNodeRemoved( |
210 BookmarkModel* model, | 208 BookmarkModel* model, |
211 const BookmarkNode* parent, | 209 const BookmarkNode* parent, |
212 int index, | 210 int index, |
213 const BookmarkNode* node) { | 211 const BookmarkNode* node) { |
214 ListValue args; | 212 scoped_ptr<ListValue> args(new ListValue()); |
215 args.Append(new StringValue(base::Int64ToString(node->id()))); | 213 args->Append(new StringValue(base::Int64ToString(node->id()))); |
216 DictionaryValue* object_args = new DictionaryValue(); | 214 DictionaryValue* object_args = new DictionaryValue(); |
217 object_args->SetString(keys::kParentIdKey, | 215 object_args->SetString(keys::kParentIdKey, |
218 base::Int64ToString(parent->id())); | 216 base::Int64ToString(parent->id())); |
219 object_args->SetInteger(keys::kIndexKey, index); | 217 object_args->SetInteger(keys::kIndexKey, index); |
220 args.Append(object_args); | 218 args->Append(object_args); |
221 | 219 |
222 std::string json_args; | 220 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, args.Pass()); |
223 base::JSONWriter::Write(&args, &json_args); | |
224 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args); | |
225 } | 221 } |
226 | 222 |
227 void BookmarkExtensionEventRouter::BookmarkNodeChanged( | 223 void BookmarkExtensionEventRouter::BookmarkNodeChanged( |
228 BookmarkModel* model, const BookmarkNode* node) { | 224 BookmarkModel* model, const BookmarkNode* node) { |
229 ListValue args; | 225 scoped_ptr<ListValue> args(new ListValue()); |
230 args.Append(new StringValue(base::Int64ToString(node->id()))); | 226 args->Append(new StringValue(base::Int64ToString(node->id()))); |
231 | 227 |
232 // TODO(erikkay) The only three things that BookmarkModel sends this | 228 // TODO(erikkay) The only three things that BookmarkModel sends this |
233 // notification for are title, url and favicon. Since we're currently | 229 // notification for are title, url and favicon. Since we're currently |
234 // ignoring favicon and since the notification doesn't say which one anyway, | 230 // ignoring favicon and since the notification doesn't say which one anyway, |
235 // for now we only include title and url. The ideal thing would be to change | 231 // for now we only include title and url. The ideal thing would be to change |
236 // BookmarkModel to indicate what changed. | 232 // BookmarkModel to indicate what changed. |
237 DictionaryValue* object_args = new DictionaryValue(); | 233 DictionaryValue* object_args = new DictionaryValue(); |
238 object_args->SetString(keys::kTitleKey, node->GetTitle()); | 234 object_args->SetString(keys::kTitleKey, node->GetTitle()); |
239 if (node->is_url()) | 235 if (node->is_url()) |
240 object_args->SetString(keys::kUrlKey, node->url().spec()); | 236 object_args->SetString(keys::kUrlKey, node->url().spec()); |
241 args.Append(object_args); | 237 args->Append(object_args); |
242 | 238 |
243 std::string json_args; | 239 DispatchEvent(model->profile(), keys::kOnBookmarkChanged, args.Pass()); |
244 base::JSONWriter::Write(&args, &json_args); | |
245 DispatchEvent(model->profile(), keys::kOnBookmarkChanged, json_args); | |
246 } | 240 } |
247 | 241 |
248 void BookmarkExtensionEventRouter::BookmarkNodeFaviconChanged( | 242 void BookmarkExtensionEventRouter::BookmarkNodeFaviconChanged( |
249 BookmarkModel* model, const BookmarkNode* node) { | 243 BookmarkModel* model, const BookmarkNode* node) { |
250 // TODO(erikkay) anything we should do here? | 244 // TODO(erikkay) anything we should do here? |
251 } | 245 } |
252 | 246 |
253 void BookmarkExtensionEventRouter::BookmarkNodeChildrenReordered( | 247 void BookmarkExtensionEventRouter::BookmarkNodeChildrenReordered( |
254 BookmarkModel* model, const BookmarkNode* node) { | 248 BookmarkModel* model, const BookmarkNode* node) { |
255 ListValue args; | 249 scoped_ptr<ListValue> args(new ListValue()); |
256 args.Append(new StringValue(base::Int64ToString(node->id()))); | 250 args->Append(new StringValue(base::Int64ToString(node->id()))); |
257 int childCount = node->child_count(); | 251 int childCount = node->child_count(); |
258 ListValue* children = new ListValue(); | 252 ListValue* children = new ListValue(); |
259 for (int i = 0; i < childCount; ++i) { | 253 for (int i = 0; i < childCount; ++i) { |
260 const BookmarkNode* child = node->GetChild(i); | 254 const BookmarkNode* child = node->GetChild(i); |
261 Value* child_id = new StringValue(base::Int64ToString(child->id())); | 255 Value* child_id = new StringValue(base::Int64ToString(child->id())); |
262 children->Append(child_id); | 256 children->Append(child_id); |
263 } | 257 } |
264 DictionaryValue* reorder_info = new DictionaryValue(); | 258 DictionaryValue* reorder_info = new DictionaryValue(); |
265 reorder_info->Set(keys::kChildIdsKey, children); | 259 reorder_info->Set(keys::kChildIdsKey, children); |
266 args.Append(reorder_info); | 260 args->Append(reorder_info); |
267 | 261 |
268 std::string json_args; | 262 DispatchEvent(model->profile(), keys::kOnBookmarkChildrenReordered, |
269 base::JSONWriter::Write(&args, &json_args); | 263 args.Pass()); |
270 DispatchEvent(model->profile(), | |
271 keys::kOnBookmarkChildrenReordered, | |
272 json_args); | |
273 } | 264 } |
274 | 265 |
275 void BookmarkExtensionEventRouter:: | 266 void BookmarkExtensionEventRouter:: |
276 ExtensiveBookmarkChangesBeginning(BookmarkModel* model) { | 267 ExtensiveBookmarkChangesBeginning(BookmarkModel* model) { |
277 ListValue args; | 268 scoped_ptr<ListValue> args(new ListValue()); |
278 std::string json_args; | |
279 base::JSONWriter::Write(&args, &json_args); | |
280 DispatchEvent(model->profile(), | 269 DispatchEvent(model->profile(), |
281 keys::kOnBookmarkImportBegan, | 270 keys::kOnBookmarkImportBegan, |
282 json_args); | 271 args.Pass()); |
283 } | 272 } |
284 | 273 |
285 void BookmarkExtensionEventRouter::ExtensiveBookmarkChangesEnded( | 274 void BookmarkExtensionEventRouter::ExtensiveBookmarkChangesEnded( |
286 BookmarkModel* model) { | 275 BookmarkModel* model) { |
287 ListValue args; | 276 scoped_ptr<ListValue> args(new ListValue()); |
288 std::string json_args; | |
289 base::JSONWriter::Write(&args, &json_args); | |
290 DispatchEvent(model->profile(), | 277 DispatchEvent(model->profile(), |
291 keys::kOnBookmarkImportEnded, | 278 keys::kOnBookmarkImportEnded, |
292 json_args); | 279 args.Pass()); |
293 } | 280 } |
294 | 281 |
295 bool GetBookmarksFunction::RunImpl() { | 282 bool GetBookmarksFunction::RunImpl() { |
296 scoped_ptr<bookmarks::Get::Params> params( | 283 scoped_ptr<bookmarks::Get::Params> params( |
297 bookmarks::Get::Params::Create(*args_)); | 284 bookmarks::Get::Params::Create(*args_)); |
298 EXTENSION_FUNCTION_VALIDATE(params.get()); | 285 EXTENSION_FUNCTION_VALIDATE(params.get()); |
299 | 286 |
300 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 287 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
301 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 288 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
302 if (params->id_or_id_list_type == | 289 if (params->id_or_id_list_type == |
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
965 int index, | 952 int index, |
966 void* params) { | 953 void* params) { |
967 #if !defined(OS_ANDROID) | 954 #if !defined(OS_ANDROID) |
968 // Android does not have support for the standard exporter. | 955 // Android does not have support for the standard exporter. |
969 // TODO(jgreenwald): remove ifdef once extensions are no longer built on | 956 // TODO(jgreenwald): remove ifdef once extensions are no longer built on |
970 // Android. | 957 // Android. |
971 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); | 958 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); |
972 #endif | 959 #endif |
973 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 960 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
974 } | 961 } |
OLD | NEW |