| 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/extensions/api/bookmarks/bookmarks_api.h" | 5 #include "chrome/browser/extensions/api/bookmarks/bookmarks_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 | 289 |
| 290 DispatchEvent( | 290 DispatchEvent( |
| 291 events::BOOKMARKS_ON_MOVED, bookmarks::OnMoved::kEventName, | 291 events::BOOKMARKS_ON_MOVED, bookmarks::OnMoved::kEventName, |
| 292 bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info)); | 292 bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info)); |
| 293 } | 293 } |
| 294 | 294 |
| 295 void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, | 295 void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, |
| 296 const BookmarkNode* parent, | 296 const BookmarkNode* parent, |
| 297 int index) { | 297 int index) { |
| 298 const BookmarkNode* node = parent->GetChild(index); | 298 const BookmarkNode* node = parent->GetChild(index); |
| 299 scoped_ptr<BookmarkTreeNode> tree_node( | 299 BookmarkTreeNode tree_node = |
| 300 bookmark_api_helpers::GetBookmarkTreeNode(managed_, node, false, false)); | 300 bookmark_api_helpers::GetBookmarkTreeNode(managed_, node, false, false); |
| 301 DispatchEvent(events::BOOKMARKS_ON_CREATED, bookmarks::OnCreated::kEventName, | 301 DispatchEvent( |
| 302 bookmarks::OnCreated::Create(base::Int64ToString(node->id()), | 302 events::BOOKMARKS_ON_CREATED, bookmarks::OnCreated::kEventName, |
| 303 *tree_node)); | 303 bookmarks::OnCreated::Create(base::Int64ToString(node->id()), tree_node)); |
| 304 } | 304 } |
| 305 | 305 |
| 306 void BookmarkEventRouter::BookmarkNodeRemoved( | 306 void BookmarkEventRouter::BookmarkNodeRemoved( |
| 307 BookmarkModel* model, | 307 BookmarkModel* model, |
| 308 const BookmarkNode* parent, | 308 const BookmarkNode* parent, |
| 309 int index, | 309 int index, |
| 310 const BookmarkNode* node, | 310 const BookmarkNode* node, |
| 311 const std::set<GURL>& removed_urls) { | 311 const std::set<GURL>& removed_urls) { |
| 312 bookmarks::OnRemoved::RemoveInfo remove_info; | 312 bookmarks::OnRemoved::RemoveInfo remove_info; |
| 313 remove_info.parent_id = base::Int64ToString(parent->id()); | 313 remove_info.parent_id = base::Int64ToString(parent->id()); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 bookmark_event_router_.reset( | 413 bookmark_event_router_.reset( |
| 414 new BookmarkEventRouter(Profile::FromBrowserContext(browser_context_))); | 414 new BookmarkEventRouter(Profile::FromBrowserContext(browser_context_))); |
| 415 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 415 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 416 } | 416 } |
| 417 | 417 |
| 418 bool BookmarksGetFunction::RunOnReady() { | 418 bool BookmarksGetFunction::RunOnReady() { |
| 419 scoped_ptr<bookmarks::Get::Params> params( | 419 scoped_ptr<bookmarks::Get::Params> params( |
| 420 bookmarks::Get::Params::Create(*args_)); | 420 bookmarks::Get::Params::Create(*args_)); |
| 421 EXTENSION_FUNCTION_VALIDATE(params.get()); | 421 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 422 | 422 |
| 423 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 423 std::vector<BookmarkTreeNode> nodes; |
| 424 ManagedBookmarkService* managed = GetManagedBookmarkService(); | 424 ManagedBookmarkService* managed = GetManagedBookmarkService(); |
| 425 if (params->id_or_id_list.as_strings) { | 425 if (params->id_or_id_list.as_strings) { |
| 426 std::vector<std::string>& ids = *params->id_or_id_list.as_strings; | 426 std::vector<std::string>& ids = *params->id_or_id_list.as_strings; |
| 427 size_t count = ids.size(); | 427 size_t count = ids.size(); |
| 428 EXTENSION_FUNCTION_VALIDATE(count > 0); | 428 EXTENSION_FUNCTION_VALIDATE(count > 0); |
| 429 for (size_t i = 0; i < count; ++i) { | 429 for (size_t i = 0; i < count; ++i) { |
| 430 const BookmarkNode* node = GetBookmarkNodeFromId(ids[i]); | 430 const BookmarkNode* node = GetBookmarkNodeFromId(ids[i]); |
| 431 if (!node) | 431 if (!node) |
| 432 return false; | 432 return false; |
| 433 bookmark_api_helpers::AddNode(managed, node, &nodes, false); | 433 bookmark_api_helpers::AddNode(managed, node, &nodes, false); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 446 | 446 |
| 447 bool BookmarksGetChildrenFunction::RunOnReady() { | 447 bool BookmarksGetChildrenFunction::RunOnReady() { |
| 448 scoped_ptr<bookmarks::GetChildren::Params> params( | 448 scoped_ptr<bookmarks::GetChildren::Params> params( |
| 449 bookmarks::GetChildren::Params::Create(*args_)); | 449 bookmarks::GetChildren::Params::Create(*args_)); |
| 450 EXTENSION_FUNCTION_VALIDATE(params.get()); | 450 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 451 | 451 |
| 452 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 452 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
| 453 if (!node) | 453 if (!node) |
| 454 return false; | 454 return false; |
| 455 | 455 |
| 456 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 456 std::vector<BookmarkTreeNode> nodes; |
| 457 int child_count = node->child_count(); | 457 int child_count = node->child_count(); |
| 458 for (int i = 0; i < child_count; ++i) { | 458 for (int i = 0; i < child_count; ++i) { |
| 459 const BookmarkNode* child = node->GetChild(i); | 459 const BookmarkNode* child = node->GetChild(i); |
| 460 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), child, &nodes, | 460 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), child, &nodes, |
| 461 false); | 461 false); |
| 462 } | 462 } |
| 463 | 463 |
| 464 results_ = bookmarks::GetChildren::Results::Create(nodes); | 464 results_ = bookmarks::GetChildren::Results::Create(nodes); |
| 465 return true; | 465 return true; |
| 466 } | 466 } |
| 467 | 467 |
| 468 bool BookmarksGetRecentFunction::RunOnReady() { | 468 bool BookmarksGetRecentFunction::RunOnReady() { |
| 469 scoped_ptr<bookmarks::GetRecent::Params> params( | 469 scoped_ptr<bookmarks::GetRecent::Params> params( |
| 470 bookmarks::GetRecent::Params::Create(*args_)); | 470 bookmarks::GetRecent::Params::Create(*args_)); |
| 471 EXTENSION_FUNCTION_VALIDATE(params.get()); | 471 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 472 if (params->number_of_items < 1) | 472 if (params->number_of_items < 1) |
| 473 return false; | 473 return false; |
| 474 | 474 |
| 475 std::vector<const BookmarkNode*> nodes; | 475 std::vector<const BookmarkNode*> nodes; |
| 476 ::bookmarks::GetMostRecentlyAddedEntries( | 476 ::bookmarks::GetMostRecentlyAddedEntries( |
| 477 BookmarkModelFactory::GetForProfile(GetProfile()), | 477 BookmarkModelFactory::GetForProfile(GetProfile()), |
| 478 params->number_of_items, | 478 params->number_of_items, |
| 479 &nodes); | 479 &nodes); |
| 480 | 480 |
| 481 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; | 481 std::vector<BookmarkTreeNode> tree_nodes; |
| 482 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); | 482 for (const BookmarkNode* node : nodes) { |
| 483 for (; i != nodes.end(); ++i) { | |
| 484 const BookmarkNode* node = *i; | |
| 485 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, | 483 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, |
| 486 &tree_nodes, false); | 484 &tree_nodes, false); |
| 487 } | 485 } |
| 488 | 486 |
| 489 results_ = bookmarks::GetRecent::Results::Create(tree_nodes); | 487 results_ = bookmarks::GetRecent::Results::Create(tree_nodes); |
| 490 return true; | 488 return true; |
| 491 } | 489 } |
| 492 | 490 |
| 493 bool BookmarksGetTreeFunction::RunOnReady() { | 491 bool BookmarksGetTreeFunction::RunOnReady() { |
| 494 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 492 std::vector<BookmarkTreeNode> nodes; |
| 495 const BookmarkNode* node = | 493 const BookmarkNode* node = |
| 496 BookmarkModelFactory::GetForProfile(GetProfile())->root_node(); | 494 BookmarkModelFactory::GetForProfile(GetProfile())->root_node(); |
| 497 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, | 495 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, |
| 498 true); | 496 true); |
| 499 results_ = bookmarks::GetTree::Results::Create(nodes); | 497 results_ = bookmarks::GetTree::Results::Create(nodes); |
| 500 return true; | 498 return true; |
| 501 } | 499 } |
| 502 | 500 |
| 503 bool BookmarksGetSubTreeFunction::RunOnReady() { | 501 bool BookmarksGetSubTreeFunction::RunOnReady() { |
| 504 scoped_ptr<bookmarks::GetSubTree::Params> params( | 502 scoped_ptr<bookmarks::GetSubTree::Params> params( |
| 505 bookmarks::GetSubTree::Params::Create(*args_)); | 503 bookmarks::GetSubTree::Params::Create(*args_)); |
| 506 EXTENSION_FUNCTION_VALIDATE(params.get()); | 504 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 507 | 505 |
| 508 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 506 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
| 509 if (!node) | 507 if (!node) |
| 510 return false; | 508 return false; |
| 511 | 509 |
| 512 std::vector<linked_ptr<BookmarkTreeNode> > nodes; | 510 std::vector<BookmarkTreeNode> nodes; |
| 513 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, | 511 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, |
| 514 true); | 512 true); |
| 515 results_ = bookmarks::GetSubTree::Results::Create(nodes); | 513 results_ = bookmarks::GetSubTree::Results::Create(nodes); |
| 516 return true; | 514 return true; |
| 517 } | 515 } |
| 518 | 516 |
| 519 bool BookmarksSearchFunction::RunOnReady() { | 517 bool BookmarksSearchFunction::RunOnReady() { |
| 520 scoped_ptr<bookmarks::Search::Params> params( | 518 scoped_ptr<bookmarks::Search::Params> params( |
| 521 bookmarks::Search::Params::Create(*args_)); | 519 bookmarks::Search::Params::Create(*args_)); |
| 522 EXTENSION_FUNCTION_VALIDATE(params.get()); | 520 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 548 if (object.title) | 546 if (object.title) |
| 549 query.title.reset(new base::string16(base::UTF8ToUTF16(*object.title))); | 547 query.title.reset(new base::string16(base::UTF8ToUTF16(*object.title))); |
| 550 ::bookmarks::GetBookmarksMatchingProperties( | 548 ::bookmarks::GetBookmarksMatchingProperties( |
| 551 BookmarkModelFactory::GetForProfile(GetProfile()), | 549 BookmarkModelFactory::GetForProfile(GetProfile()), |
| 552 query, | 550 query, |
| 553 std::numeric_limits<int>::max(), | 551 std::numeric_limits<int>::max(), |
| 554 lang, | 552 lang, |
| 555 &nodes); | 553 &nodes); |
| 556 } | 554 } |
| 557 | 555 |
| 558 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; | 556 std::vector<BookmarkTreeNode> tree_nodes; |
| 559 ManagedBookmarkService* managed = GetManagedBookmarkService(); | 557 ManagedBookmarkService* managed = GetManagedBookmarkService(); |
| 560 for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin(); | 558 for (const BookmarkNode* node : nodes) |
| 561 node_iter != nodes.end(); ++node_iter) { | 559 bookmark_api_helpers::AddNode(managed, node, &tree_nodes, false); |
| 562 bookmark_api_helpers::AddNode(managed, *node_iter, &tree_nodes, false); | |
| 563 } | |
| 564 | 560 |
| 565 results_ = bookmarks::Search::Results::Create(tree_nodes); | 561 results_ = bookmarks::Search::Results::Create(tree_nodes); |
| 566 return true; | 562 return true; |
| 567 } | 563 } |
| 568 | 564 |
| 569 // static | 565 // static |
| 570 bool BookmarksRemoveFunction::ExtractIds(const base::ListValue* args, | 566 bool BookmarksRemoveFunction::ExtractIds(const base::ListValue* args, |
| 571 std::list<int64_t>* ids, | 567 std::list<int64_t>* ids, |
| 572 bool* invalid_id) { | 568 bool* invalid_id) { |
| 573 std::string id_string; | 569 std::string id_string; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 | 607 |
| 612 scoped_ptr<bookmarks::Create::Params> params( | 608 scoped_ptr<bookmarks::Create::Params> params( |
| 613 bookmarks::Create::Params::Create(*args_)); | 609 bookmarks::Create::Params::Create(*args_)); |
| 614 EXTENSION_FUNCTION_VALIDATE(params.get()); | 610 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 615 | 611 |
| 616 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 612 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
| 617 const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL); | 613 const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL); |
| 618 if (!node) | 614 if (!node) |
| 619 return false; | 615 return false; |
| 620 | 616 |
| 621 scoped_ptr<BookmarkTreeNode> ret(bookmark_api_helpers::GetBookmarkTreeNode( | 617 BookmarkTreeNode ret = bookmark_api_helpers::GetBookmarkTreeNode( |
| 622 GetManagedBookmarkService(), node, false, false)); | 618 GetManagedBookmarkService(), node, false, false); |
| 623 results_ = bookmarks::Create::Results::Create(*ret); | 619 results_ = bookmarks::Create::Results::Create(ret); |
| 624 | 620 |
| 625 return true; | 621 return true; |
| 626 } | 622 } |
| 627 | 623 |
| 628 // static | 624 // static |
| 629 bool BookmarksMoveFunction::ExtractIds(const base::ListValue* args, | 625 bool BookmarksMoveFunction::ExtractIds(const base::ListValue* args, |
| 630 std::list<int64_t>* ids, | 626 std::list<int64_t>* ids, |
| 631 bool* invalid_id) { | 627 bool* invalid_id) { |
| 632 // For now, Move accepts ID parameters in the same way as an Update. | 628 // For now, Move accepts ID parameters in the same way as an Update. |
| 633 return BookmarksUpdateFunction::ExtractIds(args, ids, invalid_id); | 629 return BookmarksUpdateFunction::ExtractIds(args, ids, invalid_id); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 if (index > parent->child_count() || index < 0) { | 667 if (index > parent->child_count() || index < 0) { |
| 672 error_ = keys::kInvalidIndexError; | 668 error_ = keys::kInvalidIndexError; |
| 673 return false; | 669 return false; |
| 674 } | 670 } |
| 675 } else { | 671 } else { |
| 676 index = parent->child_count(); | 672 index = parent->child_count(); |
| 677 } | 673 } |
| 678 | 674 |
| 679 model->Move(node, parent, index); | 675 model->Move(node, parent, index); |
| 680 | 676 |
| 681 scoped_ptr<BookmarkTreeNode> tree_node( | 677 BookmarkTreeNode tree_node = bookmark_api_helpers::GetBookmarkTreeNode( |
| 682 bookmark_api_helpers::GetBookmarkTreeNode(GetManagedBookmarkService(), | 678 GetManagedBookmarkService(), node, false, false); |
| 683 node, false, false)); | 679 results_ = bookmarks::Move::Results::Create(tree_node); |
| 684 results_ = bookmarks::Move::Results::Create(*tree_node); | |
| 685 | 680 |
| 686 return true; | 681 return true; |
| 687 } | 682 } |
| 688 | 683 |
| 689 // static | 684 // static |
| 690 bool BookmarksUpdateFunction::ExtractIds(const base::ListValue* args, | 685 bool BookmarksUpdateFunction::ExtractIds(const base::ListValue* args, |
| 691 std::list<int64_t>* ids, | 686 std::list<int64_t>* ids, |
| 692 bool* invalid_id) { | 687 bool* invalid_id) { |
| 693 // For now, Update accepts ID parameters in the same way as an Remove. | 688 // For now, Update accepts ID parameters in the same way as an Remove. |
| 694 return BookmarksRemoveFunction::ExtractIds(args, ids, invalid_id); | 689 return BookmarksRemoveFunction::ExtractIds(args, ids, invalid_id); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 722 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
| 728 if (model->is_permanent_node(node)) { | 723 if (model->is_permanent_node(node)) { |
| 729 error_ = keys::kModifySpecialError; | 724 error_ = keys::kModifySpecialError; |
| 730 return false; | 725 return false; |
| 731 } | 726 } |
| 732 if (has_title) | 727 if (has_title) |
| 733 model->SetTitle(node, title); | 728 model->SetTitle(node, title); |
| 734 if (!url.is_empty()) | 729 if (!url.is_empty()) |
| 735 model->SetURL(node, url); | 730 model->SetURL(node, url); |
| 736 | 731 |
| 737 scoped_ptr<BookmarkTreeNode> tree_node( | 732 BookmarkTreeNode tree_node = bookmark_api_helpers::GetBookmarkTreeNode( |
| 738 bookmark_api_helpers::GetBookmarkTreeNode(GetManagedBookmarkService(), | 733 GetManagedBookmarkService(), node, false, false); |
| 739 node, false, false)); | 734 results_ = bookmarks::Update::Results::Create(tree_node); |
| 740 results_ = bookmarks::Update::Results::Create(*tree_node); | |
| 741 return true; | 735 return true; |
| 742 } | 736 } |
| 743 | 737 |
| 744 BookmarksIOFunction::BookmarksIOFunction() {} | 738 BookmarksIOFunction::BookmarksIOFunction() {} |
| 745 | 739 |
| 746 BookmarksIOFunction::~BookmarksIOFunction() { | 740 BookmarksIOFunction::~BookmarksIOFunction() { |
| 747 // There may be pending file dialogs, we need to tell them that we've gone | 741 // There may be pending file dialogs, we need to tell them that we've gone |
| 748 // away so they don't try and call back to us. | 742 // away so they don't try and call back to us. |
| 749 if (select_file_dialog_.get()) | 743 if (select_file_dialog_.get()) |
| 750 select_file_dialog_->ListenerDestroyed(); | 744 select_file_dialog_->ListenerDestroyed(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 } | 841 } |
| 848 | 842 |
| 849 void BookmarksExportFunction::FileSelected(const base::FilePath& path, | 843 void BookmarksExportFunction::FileSelected(const base::FilePath& path, |
| 850 int index, | 844 int index, |
| 851 void* params) { | 845 void* params) { |
| 852 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); | 846 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); |
| 853 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 847 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
| 854 } | 848 } |
| 855 | 849 |
| 856 } // namespace extensions | 850 } // namespace extensions |
| OLD | NEW |