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 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 keys::kOnBookmarkImportEnded, | 290 keys::kOnBookmarkImportEnded, |
291 json_args); | 291 json_args); |
292 } | 292 } |
293 | 293 |
294 bool GetBookmarksFunction::RunImpl() { | 294 bool GetBookmarksFunction::RunImpl() { |
295 scoped_ptr<bookmarks::Get::Params> params( | 295 scoped_ptr<bookmarks::Get::Params> params( |
296 bookmarks::Get::Params::Create(*args_)); | 296 bookmarks::Get::Params::Create(*args_)); |
297 EXTENSION_FUNCTION_VALIDATE(params.get()); | 297 EXTENSION_FUNCTION_VALIDATE(params.get()); |
298 | 298 |
299 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 299 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
300 BookmarkModel* model = profile()->GetBookmarkModel(); | 300 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
301 if (params->id_or_id_list_type == | 301 if (params->id_or_id_list_type == |
302 bookmarks::Get::Params::ID_OR_ID_LIST_ARRAY) { | 302 bookmarks::Get::Params::ID_OR_ID_LIST_ARRAY) { |
303 std::vector<std::string>* ids = params->id_or_id_list_array.get(); | 303 std::vector<std::string>* ids = params->id_or_id_list_array.get(); |
304 size_t count = ids->size(); | 304 size_t count = ids->size(); |
305 EXTENSION_FUNCTION_VALIDATE(count > 0); | 305 EXTENSION_FUNCTION_VALIDATE(count > 0); |
306 for (size_t i = 0; i < count; ++i) { | 306 for (size_t i = 0; i < count; ++i) { |
307 int64 id; | 307 int64 id; |
308 if (!GetBookmarkIdAsInt64(ids->at(i), &id)) | 308 if (!GetBookmarkIdAsInt64(ids->at(i), &id)) |
309 return false; | 309 return false; |
310 const BookmarkNode* node = model->GetNodeByID(id); | 310 const BookmarkNode* node = model->GetNodeByID(id); |
(...skipping 23 matching lines...) Expand all Loading... |
334 bool GetBookmarkChildrenFunction::RunImpl() { | 334 bool GetBookmarkChildrenFunction::RunImpl() { |
335 scoped_ptr<bookmarks::GetChildren::Params> params( | 335 scoped_ptr<bookmarks::GetChildren::Params> params( |
336 bookmarks::GetChildren::Params::Create(*args_)); | 336 bookmarks::GetChildren::Params::Create(*args_)); |
337 EXTENSION_FUNCTION_VALIDATE(params.get()); | 337 EXTENSION_FUNCTION_VALIDATE(params.get()); |
338 | 338 |
339 int64 id; | 339 int64 id; |
340 if (!GetBookmarkIdAsInt64(params->id, &id)) | 340 if (!GetBookmarkIdAsInt64(params->id, &id)) |
341 return false; | 341 return false; |
342 | 342 |
343 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 343 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
344 const BookmarkNode* node = profile()->GetBookmarkModel()->GetNodeByID(id); | 344 const BookmarkNode* node = |
| 345 BookmarkModelFactory::GetForProfile(profile())->GetNodeByID(id); |
345 if (!node) { | 346 if (!node) { |
346 error_ = keys::kNoNodeError; | 347 error_ = keys::kNoNodeError; |
347 return false; | 348 return false; |
348 } | 349 } |
349 int child_count = node->child_count(); | 350 int child_count = node->child_count(); |
350 for (int i = 0; i < child_count; ++i) { | 351 for (int i = 0; i < child_count; ++i) { |
351 const BookmarkNode* child = node->GetChild(i); | 352 const BookmarkNode* child = node->GetChild(i); |
352 bookmark_extension_helpers::AddNode(child, &nodes, false); | 353 bookmark_extension_helpers::AddNode(child, &nodes, false); |
353 } | 354 } |
354 | 355 |
355 results_ = bookmarks::GetChildren::Results::Create(nodes); | 356 results_ = bookmarks::GetChildren::Results::Create(nodes); |
356 return true; | 357 return true; |
357 } | 358 } |
358 | 359 |
359 bool GetBookmarkRecentFunction::RunImpl() { | 360 bool GetBookmarkRecentFunction::RunImpl() { |
360 scoped_ptr<bookmarks::GetRecent::Params> params( | 361 scoped_ptr<bookmarks::GetRecent::Params> params( |
361 bookmarks::GetRecent::Params::Create(*args_)); | 362 bookmarks::GetRecent::Params::Create(*args_)); |
362 EXTENSION_FUNCTION_VALIDATE(params.get()); | 363 EXTENSION_FUNCTION_VALIDATE(params.get()); |
363 if (params->number_of_items < 1) | 364 if (params->number_of_items < 1) |
364 return false; | 365 return false; |
365 | 366 |
366 std::vector<const BookmarkNode*> nodes; | 367 std::vector<const BookmarkNode*> nodes; |
367 bookmark_utils::GetMostRecentlyAddedEntries(profile()->GetBookmarkModel(), | 368 bookmark_utils::GetMostRecentlyAddedEntries( |
368 params->number_of_items, | 369 BookmarkModelFactory::GetForProfile(profile()), |
369 &nodes); | 370 params->number_of_items, |
| 371 &nodes); |
370 | 372 |
371 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; | 373 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; |
372 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); | 374 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); |
373 for (; i != nodes.end(); ++i) { | 375 for (; i != nodes.end(); ++i) { |
374 const BookmarkNode* node = *i; | 376 const BookmarkNode* node = *i; |
375 bookmark_extension_helpers::AddNode(node, &tree_nodes, false); | 377 bookmark_extension_helpers::AddNode(node, &tree_nodes, false); |
376 } | 378 } |
377 | 379 |
378 results_ = bookmarks::GetRecent::Results::Create(tree_nodes); | 380 results_ = bookmarks::GetRecent::Results::Create(tree_nodes); |
379 return true; | 381 return true; |
380 } | 382 } |
381 | 383 |
382 bool GetBookmarkTreeFunction::RunImpl() { | 384 bool GetBookmarkTreeFunction::RunImpl() { |
383 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 385 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
384 const BookmarkNode* node = profile()->GetBookmarkModel()->root_node(); | 386 const BookmarkNode* node = |
| 387 BookmarkModelFactory::GetForProfile(profile())->root_node(); |
385 bookmark_extension_helpers::AddNode(node, &nodes, true); | 388 bookmark_extension_helpers::AddNode(node, &nodes, true); |
386 results_ = bookmarks::GetTree::Results::Create(nodes); | 389 results_ = bookmarks::GetTree::Results::Create(nodes); |
387 return true; | 390 return true; |
388 } | 391 } |
389 | 392 |
390 bool GetBookmarkSubTreeFunction::RunImpl() { | 393 bool GetBookmarkSubTreeFunction::RunImpl() { |
391 scoped_ptr<bookmarks::GetSubTree::Params> params( | 394 scoped_ptr<bookmarks::GetSubTree::Params> params( |
392 bookmarks::GetSubTree::Params::Create(*args_)); | 395 bookmarks::GetSubTree::Params::Create(*args_)); |
393 EXTENSION_FUNCTION_VALIDATE(params.get()); | 396 EXTENSION_FUNCTION_VALIDATE(params.get()); |
394 | 397 |
395 int64 id; | 398 int64 id; |
396 if (!GetBookmarkIdAsInt64(params->id, &id)) | 399 if (!GetBookmarkIdAsInt64(params->id, &id)) |
397 return false; | 400 return false; |
398 | 401 |
399 const BookmarkNode* node = profile()->GetBookmarkModel()->GetNodeByID(id); | 402 const BookmarkNode* node = |
| 403 BookmarkModelFactory::GetForProfile(profile())->GetNodeByID(id); |
400 if (!node) { | 404 if (!node) { |
401 error_ = keys::kNoNodeError; | 405 error_ = keys::kNoNodeError; |
402 return false; | 406 return false; |
403 } | 407 } |
404 | 408 |
405 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 409 std::vector<linked_ptr<BookmarkTreeNode> > nodes; |
406 bookmark_extension_helpers::AddNode(node, &nodes, true); | 410 bookmark_extension_helpers::AddNode(node, &nodes, true); |
407 results_ = bookmarks::GetSubTree::Results::Create(nodes); | 411 results_ = bookmarks::GetSubTree::Results::Create(nodes); |
408 return true; | 412 return true; |
409 } | 413 } |
410 | 414 |
411 bool SearchBookmarksFunction::RunImpl() { | 415 bool SearchBookmarksFunction::RunImpl() { |
412 scoped_ptr<bookmarks::Search::Params> params( | 416 scoped_ptr<bookmarks::Search::Params> params( |
413 bookmarks::Search::Params::Create(*args_)); | 417 bookmarks::Search::Params::Create(*args_)); |
414 EXTENSION_FUNCTION_VALIDATE(params.get()); | 418 EXTENSION_FUNCTION_VALIDATE(params.get()); |
415 | 419 |
416 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 420 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
417 std::vector<const BookmarkNode*> nodes; | 421 std::vector<const BookmarkNode*> nodes; |
418 bookmark_utils::GetBookmarksContainingText(profile()->GetBookmarkModel(), | 422 bookmark_utils::GetBookmarksContainingText( |
419 UTF8ToUTF16(params->query), | 423 BookmarkModelFactory::GetForProfile(profile()), |
420 std::numeric_limits<int>::max(), | 424 UTF8ToUTF16(params->query), |
421 lang, | 425 std::numeric_limits<int>::max(), |
422 &nodes); | 426 lang, |
| 427 &nodes); |
423 | 428 |
424 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; | 429 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; |
425 for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin(); | 430 for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin(); |
426 node_iter != nodes.end(); ++node_iter) { | 431 node_iter != nodes.end(); ++node_iter) { |
427 bookmark_extension_helpers::AddNode(*node_iter, &tree_nodes, false); | 432 bookmark_extension_helpers::AddNode(*node_iter, &tree_nodes, false); |
428 } | 433 } |
429 | 434 |
430 results_ = bookmarks::Search::Results::Create(tree_nodes); | 435 results_ = bookmarks::Search::Results::Create(tree_nodes); |
431 return true; | 436 return true; |
432 } | 437 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 } | 477 } |
473 | 478 |
474 bool CreateBookmarkFunction::RunImpl() { | 479 bool CreateBookmarkFunction::RunImpl() { |
475 if (!EditBookmarksEnabled()) | 480 if (!EditBookmarksEnabled()) |
476 return false; | 481 return false; |
477 | 482 |
478 scoped_ptr<bookmarks::Create::Params> params( | 483 scoped_ptr<bookmarks::Create::Params> params( |
479 bookmarks::Create::Params::Create(*args_)); | 484 bookmarks::Create::Params::Create(*args_)); |
480 EXTENSION_FUNCTION_VALIDATE(params.get()); | 485 EXTENSION_FUNCTION_VALIDATE(params.get()); |
481 | 486 |
482 BookmarkModel* model = profile()->GetBookmarkModel(); | 487 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
483 int64 parentId; | 488 int64 parentId; |
484 | 489 |
485 if (!params->bookmark.parent_id.get()) { | 490 if (!params->bookmark.parent_id.get()) { |
486 // Optional, default to "other bookmarks". | 491 // Optional, default to "other bookmarks". |
487 parentId = model->other_node()->id(); | 492 parentId = model->other_node()->id(); |
488 } else { | 493 } else { |
489 if (!GetBookmarkIdAsInt64(*params->bookmark.parent_id, &parentId)) | 494 if (!GetBookmarkIdAsInt64(*params->bookmark.parent_id, &parentId)) |
490 return false; | 495 return false; |
491 } | 496 } |
492 const BookmarkNode* parent = model->GetNodeByID(parentId); | 497 const BookmarkNode* parent = model->GetNodeByID(parentId); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 scoped_ptr<bookmarks::Update::Params> params( | 636 scoped_ptr<bookmarks::Update::Params> params( |
632 bookmarks::Update::Params::Create(*args_)); | 637 bookmarks::Update::Params::Create(*args_)); |
633 EXTENSION_FUNCTION_VALIDATE(params.get()); | 638 EXTENSION_FUNCTION_VALIDATE(params.get()); |
634 | 639 |
635 int64 id; | 640 int64 id; |
636 if (!base::StringToInt64(params->id, &id)) { | 641 if (!base::StringToInt64(params->id, &id)) { |
637 error_ = keys::kInvalidIdError; | 642 error_ = keys::kInvalidIdError; |
638 return false; | 643 return false; |
639 } | 644 } |
640 | 645 |
641 BookmarkModel* model = profile()->GetBookmarkModel(); | 646 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
642 | 647 |
643 // Optional but we need to distinguish non present from an empty title. | 648 // Optional but we need to distinguish non present from an empty title. |
644 string16 title; | 649 string16 title; |
645 bool has_title = false; | 650 bool has_title = false; |
646 if (params->changes.title.get()) { | 651 if (params->changes.title.get()) { |
647 title = UTF8ToUTF16(*params->changes.title); | 652 title = UTF8ToUTF16(*params->changes.title); |
648 has_title = true; | 653 has_title = true; |
649 } | 654 } |
650 | 655 |
651 // Optional. | 656 // Optional. |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
959 int index, | 964 int index, |
960 void* params) { | 965 void* params) { |
961 #if !defined(OS_ANDROID) | 966 #if !defined(OS_ANDROID) |
962 // Android does not have support for the standard exporter. | 967 // Android does not have support for the standard exporter. |
963 // TODO(jgreenwald): remove ifdef once extensions are no longer built on | 968 // TODO(jgreenwald): remove ifdef once extensions are no longer built on |
964 // Android. | 969 // Android. |
965 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); | 970 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); |
966 #endif | 971 #endif |
967 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 972 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
968 } | 973 } |
OLD | NEW |