| 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 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 base::i18n::ReplaceIllegalCharactersInPath(&filename, '_'); | 89 base::i18n::ReplaceIllegalCharactersInPath(&filename, '_'); |
| 90 | 90 |
| 91 base::FilePath default_path; | 91 base::FilePath default_path; |
| 92 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path); | 92 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path); |
| 93 return default_path.Append(filename); | 93 return default_path.Append(filename); |
| 94 } | 94 } |
| 95 | 95 |
| 96 } // namespace | 96 } // namespace |
| 97 | 97 |
| 98 bool BookmarksFunction::RunAsync() { | 98 bool BookmarksFunction::RunAsync() { |
| 99 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 99 BookmarkModel* model = |
| 100 BookmarkModelFactory::GetForBrowserContext(GetProfile()); |
| 100 if (!model->loaded()) { | 101 if (!model->loaded()) { |
| 101 // Bookmarks are not ready yet. We'll wait. | 102 // Bookmarks are not ready yet. We'll wait. |
| 102 model->AddObserver(this); | 103 model->AddObserver(this); |
| 103 AddRef(); // Balanced in Loaded(). | 104 AddRef(); // Balanced in Loaded(). |
| 104 return true; | 105 return true; |
| 105 } | 106 } |
| 106 | 107 |
| 107 RunAndSendResponse(); | 108 RunAndSendResponse(); |
| 108 return true; | 109 return true; |
| 109 } | 110 } |
| 110 | 111 |
| 111 BookmarkModel* BookmarksFunction::GetBookmarkModel() { | 112 BookmarkModel* BookmarksFunction::GetBookmarkModel() { |
| 112 return BookmarkModelFactory::GetForProfile(GetProfile()); | 113 return BookmarkModelFactory::GetForBrowserContext(GetProfile()); |
| 113 } | 114 } |
| 114 | 115 |
| 115 ManagedBookmarkService* BookmarksFunction::GetManagedBookmarkService() { | 116 ManagedBookmarkService* BookmarksFunction::GetManagedBookmarkService() { |
| 116 return ManagedBookmarkServiceFactory::GetForProfile(GetProfile()); | 117 return ManagedBookmarkServiceFactory::GetForProfile(GetProfile()); |
| 117 } | 118 } |
| 118 | 119 |
| 119 bool BookmarksFunction::GetBookmarkIdAsInt64(const std::string& id_string, | 120 bool BookmarksFunction::GetBookmarkIdAsInt64(const std::string& id_string, |
| 120 int64_t* id) { | 121 int64_t* id) { |
| 121 if (base::StringToInt64(id_string, id)) | 122 if (base::StringToInt64(id_string, id)) |
| 122 return true; | 123 return true; |
| 123 | 124 |
| 124 error_ = keys::kInvalidIdError; | 125 error_ = keys::kInvalidIdError; |
| 125 return false; | 126 return false; |
| 126 } | 127 } |
| 127 | 128 |
| 128 const BookmarkNode* BookmarksFunction::GetBookmarkNodeFromId( | 129 const BookmarkNode* BookmarksFunction::GetBookmarkNodeFromId( |
| 129 const std::string& id_string) { | 130 const std::string& id_string) { |
| 130 int64_t id; | 131 int64_t id; |
| 131 if (!GetBookmarkIdAsInt64(id_string, &id)) | 132 if (!GetBookmarkIdAsInt64(id_string, &id)) |
| 132 return NULL; | 133 return NULL; |
| 133 | 134 |
| 134 const BookmarkNode* node = ::bookmarks::GetBookmarkNodeByID( | 135 const BookmarkNode* node = ::bookmarks::GetBookmarkNodeByID( |
| 135 BookmarkModelFactory::GetForProfile(GetProfile()), id); | 136 BookmarkModelFactory::GetForBrowserContext(GetProfile()), id); |
| 136 if (!node) | 137 if (!node) |
| 137 error_ = keys::kNoNodeError; | 138 error_ = keys::kNoNodeError; |
| 138 | 139 |
| 139 return node; | 140 return node; |
| 140 } | 141 } |
| 141 | 142 |
| 142 const BookmarkNode* BookmarksFunction::CreateBookmarkNode( | 143 const BookmarkNode* BookmarksFunction::CreateBookmarkNode( |
| 143 BookmarkModel* model, | 144 BookmarkModel* model, |
| 144 const CreateDetails& details, | 145 const CreateDetails& details, |
| 145 const BookmarkNode::MetaInfoMap* meta_info) { | 146 const BookmarkNode::MetaInfoMap* meta_info) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 content::NotificationService::current()->Notify( | 239 content::NotificationService::current()->Notify( |
| 239 extensions::NOTIFICATION_EXTENSION_BOOKMARKS_API_INVOKED, | 240 extensions::NOTIFICATION_EXTENSION_BOOKMARKS_API_INVOKED, |
| 240 content::Source<const Extension>(extension()), | 241 content::Source<const Extension>(extension()), |
| 241 content::Details<const BookmarksFunction>(this)); | 242 content::Details<const BookmarksFunction>(this)); |
| 242 } | 243 } |
| 243 SendResponse(success); | 244 SendResponse(success); |
| 244 } | 245 } |
| 245 | 246 |
| 246 BookmarkEventRouter::BookmarkEventRouter(Profile* profile) | 247 BookmarkEventRouter::BookmarkEventRouter(Profile* profile) |
| 247 : browser_context_(profile), | 248 : browser_context_(profile), |
| 248 model_(BookmarkModelFactory::GetForProfile(profile)), | 249 model_(BookmarkModelFactory::GetForBrowserContext(profile)), |
| 249 managed_(ManagedBookmarkServiceFactory::GetForProfile(profile)) { | 250 managed_(ManagedBookmarkServiceFactory::GetForProfile(profile)) { |
| 250 model_->AddObserver(this); | 251 model_->AddObserver(this); |
| 251 } | 252 } |
| 252 | 253 |
| 253 BookmarkEventRouter::~BookmarkEventRouter() { | 254 BookmarkEventRouter::~BookmarkEventRouter() { |
| 254 if (model_) { | 255 if (model_) { |
| 255 model_->RemoveObserver(this); | 256 model_->RemoveObserver(this); |
| 256 } | 257 } |
| 257 } | 258 } |
| 258 | 259 |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 | 470 |
| 470 bool BookmarksGetRecentFunction::RunOnReady() { | 471 bool BookmarksGetRecentFunction::RunOnReady() { |
| 471 std::unique_ptr<bookmarks::GetRecent::Params> params( | 472 std::unique_ptr<bookmarks::GetRecent::Params> params( |
| 472 bookmarks::GetRecent::Params::Create(*args_)); | 473 bookmarks::GetRecent::Params::Create(*args_)); |
| 473 EXTENSION_FUNCTION_VALIDATE(params.get()); | 474 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 474 if (params->number_of_items < 1) | 475 if (params->number_of_items < 1) |
| 475 return false; | 476 return false; |
| 476 | 477 |
| 477 std::vector<const BookmarkNode*> nodes; | 478 std::vector<const BookmarkNode*> nodes; |
| 478 ::bookmarks::GetMostRecentlyAddedEntries( | 479 ::bookmarks::GetMostRecentlyAddedEntries( |
| 479 BookmarkModelFactory::GetForProfile(GetProfile()), | 480 BookmarkModelFactory::GetForBrowserContext(GetProfile()), |
| 480 params->number_of_items, | 481 params->number_of_items, &nodes); |
| 481 &nodes); | |
| 482 | 482 |
| 483 std::vector<BookmarkTreeNode> tree_nodes; | 483 std::vector<BookmarkTreeNode> tree_nodes; |
| 484 for (const BookmarkNode* node : nodes) { | 484 for (const BookmarkNode* node : nodes) { |
| 485 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, | 485 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, |
| 486 &tree_nodes, false); | 486 &tree_nodes, false); |
| 487 } | 487 } |
| 488 | 488 |
| 489 results_ = bookmarks::GetRecent::Results::Create(tree_nodes); | 489 results_ = bookmarks::GetRecent::Results::Create(tree_nodes); |
| 490 return true; | 490 return true; |
| 491 } | 491 } |
| 492 | 492 |
| 493 bool BookmarksGetTreeFunction::RunOnReady() { | 493 bool BookmarksGetTreeFunction::RunOnReady() { |
| 494 std::vector<BookmarkTreeNode> nodes; | 494 std::vector<BookmarkTreeNode> nodes; |
| 495 const BookmarkNode* node = | 495 const BookmarkNode* node = |
| 496 BookmarkModelFactory::GetForProfile(GetProfile())->root_node(); | 496 BookmarkModelFactory::GetForBrowserContext(GetProfile())->root_node(); |
| 497 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, | 497 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, |
| 498 true); | 498 true); |
| 499 results_ = bookmarks::GetTree::Results::Create(nodes); | 499 results_ = bookmarks::GetTree::Results::Create(nodes); |
| 500 return true; | 500 return true; |
| 501 } | 501 } |
| 502 | 502 |
| 503 bool BookmarksGetSubTreeFunction::RunOnReady() { | 503 bool BookmarksGetSubTreeFunction::RunOnReady() { |
| 504 std::unique_ptr<bookmarks::GetSubTree::Params> params( | 504 std::unique_ptr<bookmarks::GetSubTree::Params> params( |
| 505 bookmarks::GetSubTree::Params::Create(*args_)); | 505 bookmarks::GetSubTree::Params::Create(*args_)); |
| 506 EXTENSION_FUNCTION_VALIDATE(params.get()); | 506 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 520 std::unique_ptr<bookmarks::Search::Params> params( | 520 std::unique_ptr<bookmarks::Search::Params> params( |
| 521 bookmarks::Search::Params::Create(*args_)); | 521 bookmarks::Search::Params::Create(*args_)); |
| 522 EXTENSION_FUNCTION_VALIDATE(params.get()); | 522 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 523 | 523 |
| 524 std::vector<const BookmarkNode*> nodes; | 524 std::vector<const BookmarkNode*> nodes; |
| 525 if (params->query.as_string) { | 525 if (params->query.as_string) { |
| 526 ::bookmarks::QueryFields query; | 526 ::bookmarks::QueryFields query; |
| 527 query.word_phrase_query.reset( | 527 query.word_phrase_query.reset( |
| 528 new base::string16(base::UTF8ToUTF16(*params->query.as_string))); | 528 new base::string16(base::UTF8ToUTF16(*params->query.as_string))); |
| 529 ::bookmarks::GetBookmarksMatchingProperties( | 529 ::bookmarks::GetBookmarksMatchingProperties( |
| 530 BookmarkModelFactory::GetForProfile(GetProfile()), | 530 BookmarkModelFactory::GetForBrowserContext(GetProfile()), query, |
| 531 query, | 531 std::numeric_limits<int>::max(), &nodes); |
| 532 std::numeric_limits<int>::max(), | |
| 533 &nodes); | |
| 534 } else { | 532 } else { |
| 535 DCHECK(params->query.as_object); | 533 DCHECK(params->query.as_object); |
| 536 const bookmarks::Search::Params::Query::Object& object = | 534 const bookmarks::Search::Params::Query::Object& object = |
| 537 *params->query.as_object; | 535 *params->query.as_object; |
| 538 ::bookmarks::QueryFields query; | 536 ::bookmarks::QueryFields query; |
| 539 if (object.query) { | 537 if (object.query) { |
| 540 query.word_phrase_query.reset( | 538 query.word_phrase_query.reset( |
| 541 new base::string16(base::UTF8ToUTF16(*object.query))); | 539 new base::string16(base::UTF8ToUTF16(*object.query))); |
| 542 } | 540 } |
| 543 if (object.url) | 541 if (object.url) |
| 544 query.url.reset(new base::string16(base::UTF8ToUTF16(*object.url))); | 542 query.url.reset(new base::string16(base::UTF8ToUTF16(*object.url))); |
| 545 if (object.title) | 543 if (object.title) |
| 546 query.title.reset(new base::string16(base::UTF8ToUTF16(*object.title))); | 544 query.title.reset(new base::string16(base::UTF8ToUTF16(*object.title))); |
| 547 ::bookmarks::GetBookmarksMatchingProperties( | 545 ::bookmarks::GetBookmarksMatchingProperties( |
| 548 BookmarkModelFactory::GetForProfile(GetProfile()), | 546 BookmarkModelFactory::GetForBrowserContext(GetProfile()), query, |
| 549 query, | 547 std::numeric_limits<int>::max(), &nodes); |
| 550 std::numeric_limits<int>::max(), | |
| 551 &nodes); | |
| 552 } | 548 } |
| 553 | 549 |
| 554 std::vector<BookmarkTreeNode> tree_nodes; | 550 std::vector<BookmarkTreeNode> tree_nodes; |
| 555 ManagedBookmarkService* managed = GetManagedBookmarkService(); | 551 ManagedBookmarkService* managed = GetManagedBookmarkService(); |
| 556 for (const BookmarkNode* node : nodes) | 552 for (const BookmarkNode* node : nodes) |
| 557 bookmark_api_helpers::AddNode(managed, node, &tree_nodes, false); | 553 bookmark_api_helpers::AddNode(managed, node, &tree_nodes, false); |
| 558 | 554 |
| 559 results_ = bookmarks::Search::Results::Create(tree_nodes); | 555 results_ = bookmarks::Search::Results::Create(tree_nodes); |
| 560 return true; | 556 return true; |
| 561 } | 557 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 } | 596 } |
| 601 | 597 |
| 602 bool BookmarksCreateFunction::RunOnReady() { | 598 bool BookmarksCreateFunction::RunOnReady() { |
| 603 if (!EditBookmarksEnabled()) | 599 if (!EditBookmarksEnabled()) |
| 604 return false; | 600 return false; |
| 605 | 601 |
| 606 std::unique_ptr<bookmarks::Create::Params> params( | 602 std::unique_ptr<bookmarks::Create::Params> params( |
| 607 bookmarks::Create::Params::Create(*args_)); | 603 bookmarks::Create::Params::Create(*args_)); |
| 608 EXTENSION_FUNCTION_VALIDATE(params.get()); | 604 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 609 | 605 |
| 610 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 606 BookmarkModel* model = |
| 607 BookmarkModelFactory::GetForBrowserContext(GetProfile()); |
| 611 const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL); | 608 const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL); |
| 612 if (!node) | 609 if (!node) |
| 613 return false; | 610 return false; |
| 614 | 611 |
| 615 BookmarkTreeNode ret = bookmark_api_helpers::GetBookmarkTreeNode( | 612 BookmarkTreeNode ret = bookmark_api_helpers::GetBookmarkTreeNode( |
| 616 GetManagedBookmarkService(), node, false, false); | 613 GetManagedBookmarkService(), node, false, false); |
| 617 results_ = bookmarks::Create::Results::Create(ret); | 614 results_ = bookmarks::Create::Results::Create(ret); |
| 618 | 615 |
| 619 return true; | 616 return true; |
| 620 } | 617 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 632 return false; | 629 return false; |
| 633 | 630 |
| 634 std::unique_ptr<bookmarks::Move::Params> params( | 631 std::unique_ptr<bookmarks::Move::Params> params( |
| 635 bookmarks::Move::Params::Create(*args_)); | 632 bookmarks::Move::Params::Create(*args_)); |
| 636 EXTENSION_FUNCTION_VALIDATE(params.get()); | 633 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 637 | 634 |
| 638 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 635 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
| 639 if (!node) | 636 if (!node) |
| 640 return false; | 637 return false; |
| 641 | 638 |
| 642 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 639 BookmarkModel* model = |
| 640 BookmarkModelFactory::GetForBrowserContext(GetProfile()); |
| 643 if (model->is_permanent_node(node)) { | 641 if (model->is_permanent_node(node)) { |
| 644 error_ = keys::kModifySpecialError; | 642 error_ = keys::kModifySpecialError; |
| 645 return false; | 643 return false; |
| 646 } | 644 } |
| 647 | 645 |
| 648 const BookmarkNode* parent = NULL; | 646 const BookmarkNode* parent = NULL; |
| 649 if (!params->destination.parent_id.get()) { | 647 if (!params->destination.parent_id.get()) { |
| 650 // Optional, defaults to current parent. | 648 // Optional, defaults to current parent. |
| 651 parent = node->parent(); | 649 parent = node->parent(); |
| 652 } else { | 650 } else { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 GURL url(url_string); | 708 GURL url(url_string); |
| 711 if (!url_string.empty() && !url.is_valid()) { | 709 if (!url_string.empty() && !url.is_valid()) { |
| 712 error_ = keys::kInvalidUrlError; | 710 error_ = keys::kInvalidUrlError; |
| 713 return false; | 711 return false; |
| 714 } | 712 } |
| 715 | 713 |
| 716 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 714 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
| 717 if (!CanBeModified(node)) | 715 if (!CanBeModified(node)) |
| 718 return false; | 716 return false; |
| 719 | 717 |
| 720 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 718 BookmarkModel* model = |
| 719 BookmarkModelFactory::GetForBrowserContext(GetProfile()); |
| 721 if (model->is_permanent_node(node)) { | 720 if (model->is_permanent_node(node)) { |
| 722 error_ = keys::kModifySpecialError; | 721 error_ = keys::kModifySpecialError; |
| 723 return false; | 722 return false; |
| 724 } | 723 } |
| 725 if (has_title) | 724 if (has_title) |
| 726 model->SetTitle(node, title); | 725 model->SetTitle(node, title); |
| 727 if (!url.is_empty()) | 726 if (!url.is_empty()) |
| 728 model->SetURL(node, url); | 727 model->SetURL(node, url); |
| 729 | 728 |
| 730 BookmarkTreeNode tree_node = bookmark_api_helpers::GetBookmarkTreeNode( | 729 BookmarkTreeNode tree_node = bookmark_api_helpers::GetBookmarkTreeNode( |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 839 } | 838 } |
| 840 | 839 |
| 841 void BookmarksExportFunction::FileSelected(const base::FilePath& path, | 840 void BookmarksExportFunction::FileSelected(const base::FilePath& path, |
| 842 int index, | 841 int index, |
| 843 void* params) { | 842 void* params) { |
| 844 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); | 843 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); |
| 845 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 844 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
| 846 } | 845 } |
| 847 | 846 |
| 848 } // namespace extensions | 847 } // namespace extensions |
| OLD | NEW |