| 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 |
| 9 #include <memory> |
| 8 #include <utility> | 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 12 #include "base/i18n/file_util_icu.h" | 14 #include "base/i18n/file_util_icu.h" |
| 13 #include "base/i18n/time_formatting.h" | 15 #include "base/i18n/time_formatting.h" |
| 14 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 15 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/ptr_util.h" |
| 16 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 17 #include "base/sha1.h" | 19 #include "base/sha1.h" |
| 18 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
| 19 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
| 20 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 24 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 25 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 27 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 252 |
| 251 BookmarkEventRouter::~BookmarkEventRouter() { | 253 BookmarkEventRouter::~BookmarkEventRouter() { |
| 252 if (model_) { | 254 if (model_) { |
| 253 model_->RemoveObserver(this); | 255 model_->RemoveObserver(this); |
| 254 } | 256 } |
| 255 } | 257 } |
| 256 | 258 |
| 257 void BookmarkEventRouter::DispatchEvent( | 259 void BookmarkEventRouter::DispatchEvent( |
| 258 events::HistogramValue histogram_value, | 260 events::HistogramValue histogram_value, |
| 259 const std::string& event_name, | 261 const std::string& event_name, |
| 260 scoped_ptr<base::ListValue> event_args) { | 262 std::unique_ptr<base::ListValue> event_args) { |
| 261 EventRouter* event_router = EventRouter::Get(browser_context_); | 263 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 262 if (event_router) { | 264 if (event_router) { |
| 263 event_router->BroadcastEvent(make_scoped_ptr(new extensions::Event( | 265 event_router->BroadcastEvent(base::WrapUnique(new extensions::Event( |
| 264 histogram_value, event_name, std::move(event_args)))); | 266 histogram_value, event_name, std::move(event_args)))); |
| 265 } | 267 } |
| 266 } | 268 } |
| 267 | 269 |
| 268 void BookmarkEventRouter::BookmarkModelLoaded(BookmarkModel* model, | 270 void BookmarkEventRouter::BookmarkModelLoaded(BookmarkModel* model, |
| 269 bool ids_reassigned) { | 271 bool ids_reassigned) { |
| 270 // TODO(erikkay): Perhaps we should send this event down to the extension | 272 // TODO(erikkay): Perhaps we should send this event down to the extension |
| 271 // so they know when it's safe to use the API? | 273 // so they know when it's safe to use the API? |
| 272 } | 274 } |
| 273 | 275 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 return g_factory.Pointer(); | 411 return g_factory.Pointer(); |
| 410 } | 412 } |
| 411 | 413 |
| 412 void BookmarksAPI::OnListenerAdded(const EventListenerInfo& details) { | 414 void BookmarksAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 413 bookmark_event_router_.reset( | 415 bookmark_event_router_.reset( |
| 414 new BookmarkEventRouter(Profile::FromBrowserContext(browser_context_))); | 416 new BookmarkEventRouter(Profile::FromBrowserContext(browser_context_))); |
| 415 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 417 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 416 } | 418 } |
| 417 | 419 |
| 418 bool BookmarksGetFunction::RunOnReady() { | 420 bool BookmarksGetFunction::RunOnReady() { |
| 419 scoped_ptr<bookmarks::Get::Params> params( | 421 std::unique_ptr<bookmarks::Get::Params> params( |
| 420 bookmarks::Get::Params::Create(*args_)); | 422 bookmarks::Get::Params::Create(*args_)); |
| 421 EXTENSION_FUNCTION_VALIDATE(params.get()); | 423 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 422 | 424 |
| 423 std::vector<BookmarkTreeNode> nodes; | 425 std::vector<BookmarkTreeNode> nodes; |
| 424 ManagedBookmarkService* managed = GetManagedBookmarkService(); | 426 ManagedBookmarkService* managed = GetManagedBookmarkService(); |
| 425 if (params->id_or_id_list.as_strings) { | 427 if (params->id_or_id_list.as_strings) { |
| 426 std::vector<std::string>& ids = *params->id_or_id_list.as_strings; | 428 std::vector<std::string>& ids = *params->id_or_id_list.as_strings; |
| 427 size_t count = ids.size(); | 429 size_t count = ids.size(); |
| 428 EXTENSION_FUNCTION_VALIDATE(count > 0); | 430 EXTENSION_FUNCTION_VALIDATE(count > 0); |
| 429 for (size_t i = 0; i < count; ++i) { | 431 for (size_t i = 0; i < count; ++i) { |
| 430 const BookmarkNode* node = GetBookmarkNodeFromId(ids[i]); | 432 const BookmarkNode* node = GetBookmarkNodeFromId(ids[i]); |
| 431 if (!node) | 433 if (!node) |
| 432 return false; | 434 return false; |
| 433 bookmark_api_helpers::AddNode(managed, node, &nodes, false); | 435 bookmark_api_helpers::AddNode(managed, node, &nodes, false); |
| 434 } | 436 } |
| 435 } else { | 437 } else { |
| 436 const BookmarkNode* node = | 438 const BookmarkNode* node = |
| 437 GetBookmarkNodeFromId(*params->id_or_id_list.as_string); | 439 GetBookmarkNodeFromId(*params->id_or_id_list.as_string); |
| 438 if (!node) | 440 if (!node) |
| 439 return false; | 441 return false; |
| 440 bookmark_api_helpers::AddNode(managed, node, &nodes, false); | 442 bookmark_api_helpers::AddNode(managed, node, &nodes, false); |
| 441 } | 443 } |
| 442 | 444 |
| 443 results_ = bookmarks::Get::Results::Create(nodes); | 445 results_ = bookmarks::Get::Results::Create(nodes); |
| 444 return true; | 446 return true; |
| 445 } | 447 } |
| 446 | 448 |
| 447 bool BookmarksGetChildrenFunction::RunOnReady() { | 449 bool BookmarksGetChildrenFunction::RunOnReady() { |
| 448 scoped_ptr<bookmarks::GetChildren::Params> params( | 450 std::unique_ptr<bookmarks::GetChildren::Params> params( |
| 449 bookmarks::GetChildren::Params::Create(*args_)); | 451 bookmarks::GetChildren::Params::Create(*args_)); |
| 450 EXTENSION_FUNCTION_VALIDATE(params.get()); | 452 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 451 | 453 |
| 452 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 454 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
| 453 if (!node) | 455 if (!node) |
| 454 return false; | 456 return false; |
| 455 | 457 |
| 456 std::vector<BookmarkTreeNode> nodes; | 458 std::vector<BookmarkTreeNode> nodes; |
| 457 int child_count = node->child_count(); | 459 int child_count = node->child_count(); |
| 458 for (int i = 0; i < child_count; ++i) { | 460 for (int i = 0; i < child_count; ++i) { |
| 459 const BookmarkNode* child = node->GetChild(i); | 461 const BookmarkNode* child = node->GetChild(i); |
| 460 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), child, &nodes, | 462 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), child, &nodes, |
| 461 false); | 463 false); |
| 462 } | 464 } |
| 463 | 465 |
| 464 results_ = bookmarks::GetChildren::Results::Create(nodes); | 466 results_ = bookmarks::GetChildren::Results::Create(nodes); |
| 465 return true; | 467 return true; |
| 466 } | 468 } |
| 467 | 469 |
| 468 bool BookmarksGetRecentFunction::RunOnReady() { | 470 bool BookmarksGetRecentFunction::RunOnReady() { |
| 469 scoped_ptr<bookmarks::GetRecent::Params> params( | 471 std::unique_ptr<bookmarks::GetRecent::Params> params( |
| 470 bookmarks::GetRecent::Params::Create(*args_)); | 472 bookmarks::GetRecent::Params::Create(*args_)); |
| 471 EXTENSION_FUNCTION_VALIDATE(params.get()); | 473 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 472 if (params->number_of_items < 1) | 474 if (params->number_of_items < 1) |
| 473 return false; | 475 return false; |
| 474 | 476 |
| 475 std::vector<const BookmarkNode*> nodes; | 477 std::vector<const BookmarkNode*> nodes; |
| 476 ::bookmarks::GetMostRecentlyAddedEntries( | 478 ::bookmarks::GetMostRecentlyAddedEntries( |
| 477 BookmarkModelFactory::GetForProfile(GetProfile()), | 479 BookmarkModelFactory::GetForProfile(GetProfile()), |
| 478 params->number_of_items, | 480 params->number_of_items, |
| 479 &nodes); | 481 &nodes); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 492 std::vector<BookmarkTreeNode> nodes; | 494 std::vector<BookmarkTreeNode> nodes; |
| 493 const BookmarkNode* node = | 495 const BookmarkNode* node = |
| 494 BookmarkModelFactory::GetForProfile(GetProfile())->root_node(); | 496 BookmarkModelFactory::GetForProfile(GetProfile())->root_node(); |
| 495 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, | 497 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, |
| 496 true); | 498 true); |
| 497 results_ = bookmarks::GetTree::Results::Create(nodes); | 499 results_ = bookmarks::GetTree::Results::Create(nodes); |
| 498 return true; | 500 return true; |
| 499 } | 501 } |
| 500 | 502 |
| 501 bool BookmarksGetSubTreeFunction::RunOnReady() { | 503 bool BookmarksGetSubTreeFunction::RunOnReady() { |
| 502 scoped_ptr<bookmarks::GetSubTree::Params> params( | 504 std::unique_ptr<bookmarks::GetSubTree::Params> params( |
| 503 bookmarks::GetSubTree::Params::Create(*args_)); | 505 bookmarks::GetSubTree::Params::Create(*args_)); |
| 504 EXTENSION_FUNCTION_VALIDATE(params.get()); | 506 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 505 | 507 |
| 506 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 508 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
| 507 if (!node) | 509 if (!node) |
| 508 return false; | 510 return false; |
| 509 | 511 |
| 510 std::vector<BookmarkTreeNode> nodes; | 512 std::vector<BookmarkTreeNode> nodes; |
| 511 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, | 513 bookmark_api_helpers::AddNode(GetManagedBookmarkService(), node, &nodes, |
| 512 true); | 514 true); |
| 513 results_ = bookmarks::GetSubTree::Results::Create(nodes); | 515 results_ = bookmarks::GetSubTree::Results::Create(nodes); |
| 514 return true; | 516 return true; |
| 515 } | 517 } |
| 516 | 518 |
| 517 bool BookmarksSearchFunction::RunOnReady() { | 519 bool BookmarksSearchFunction::RunOnReady() { |
| 518 scoped_ptr<bookmarks::Search::Params> params( | 520 std::unique_ptr<bookmarks::Search::Params> params( |
| 519 bookmarks::Search::Params::Create(*args_)); | 521 bookmarks::Search::Params::Create(*args_)); |
| 520 EXTENSION_FUNCTION_VALIDATE(params.get()); | 522 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 521 | 523 |
| 522 std::vector<const BookmarkNode*> nodes; | 524 std::vector<const BookmarkNode*> nodes; |
| 523 if (params->query.as_string) { | 525 if (params->query.as_string) { |
| 524 ::bookmarks::QueryFields query; | 526 ::bookmarks::QueryFields query; |
| 525 query.word_phrase_query.reset( | 527 query.word_phrase_query.reset( |
| 526 new base::string16(base::UTF8ToUTF16(*params->query.as_string))); | 528 new base::string16(base::UTF8ToUTF16(*params->query.as_string))); |
| 527 ::bookmarks::GetBookmarksMatchingProperties( | 529 ::bookmarks::GetBookmarksMatchingProperties( |
| 528 BookmarkModelFactory::GetForProfile(GetProfile()), | 530 BookmarkModelFactory::GetForProfile(GetProfile()), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 ids->push_back(id); | 572 ids->push_back(id); |
| 571 else | 573 else |
| 572 *invalid_id = true; | 574 *invalid_id = true; |
| 573 return true; | 575 return true; |
| 574 } | 576 } |
| 575 | 577 |
| 576 bool BookmarksRemoveFunction::RunOnReady() { | 578 bool BookmarksRemoveFunction::RunOnReady() { |
| 577 if (!EditBookmarksEnabled()) | 579 if (!EditBookmarksEnabled()) |
| 578 return false; | 580 return false; |
| 579 | 581 |
| 580 scoped_ptr<bookmarks::Remove::Params> params( | 582 std::unique_ptr<bookmarks::Remove::Params> params( |
| 581 bookmarks::Remove::Params::Create(*args_)); | 583 bookmarks::Remove::Params::Create(*args_)); |
| 582 EXTENSION_FUNCTION_VALIDATE(params.get()); | 584 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 583 | 585 |
| 584 int64_t id; | 586 int64_t id; |
| 585 if (!GetBookmarkIdAsInt64(params->id, &id)) | 587 if (!GetBookmarkIdAsInt64(params->id, &id)) |
| 586 return false; | 588 return false; |
| 587 | 589 |
| 588 bool recursive = false; | 590 bool recursive = false; |
| 589 if (name() == BookmarksRemoveTreeFunction::function_name()) | 591 if (name() == BookmarksRemoveTreeFunction::function_name()) |
| 590 recursive = true; | 592 recursive = true; |
| 591 | 593 |
| 592 BookmarkModel* model = GetBookmarkModel(); | 594 BookmarkModel* model = GetBookmarkModel(); |
| 593 ManagedBookmarkService* managed = GetManagedBookmarkService(); | 595 ManagedBookmarkService* managed = GetManagedBookmarkService(); |
| 594 if (!bookmark_api_helpers::RemoveNode(model, managed, id, recursive, &error_)) | 596 if (!bookmark_api_helpers::RemoveNode(model, managed, id, recursive, &error_)) |
| 595 return false; | 597 return false; |
| 596 | 598 |
| 597 return true; | 599 return true; |
| 598 } | 600 } |
| 599 | 601 |
| 600 bool BookmarksCreateFunction::RunOnReady() { | 602 bool BookmarksCreateFunction::RunOnReady() { |
| 601 if (!EditBookmarksEnabled()) | 603 if (!EditBookmarksEnabled()) |
| 602 return false; | 604 return false; |
| 603 | 605 |
| 604 scoped_ptr<bookmarks::Create::Params> params( | 606 std::unique_ptr<bookmarks::Create::Params> params( |
| 605 bookmarks::Create::Params::Create(*args_)); | 607 bookmarks::Create::Params::Create(*args_)); |
| 606 EXTENSION_FUNCTION_VALIDATE(params.get()); | 608 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 607 | 609 |
| 608 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 610 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
| 609 const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL); | 611 const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL); |
| 610 if (!node) | 612 if (!node) |
| 611 return false; | 613 return false; |
| 612 | 614 |
| 613 BookmarkTreeNode ret = bookmark_api_helpers::GetBookmarkTreeNode( | 615 BookmarkTreeNode ret = bookmark_api_helpers::GetBookmarkTreeNode( |
| 614 GetManagedBookmarkService(), node, false, false); | 616 GetManagedBookmarkService(), node, false, false); |
| 615 results_ = bookmarks::Create::Results::Create(ret); | 617 results_ = bookmarks::Create::Results::Create(ret); |
| 616 | 618 |
| 617 return true; | 619 return true; |
| 618 } | 620 } |
| 619 | 621 |
| 620 // static | 622 // static |
| 621 bool BookmarksMoveFunction::ExtractIds(const base::ListValue* args, | 623 bool BookmarksMoveFunction::ExtractIds(const base::ListValue* args, |
| 622 std::list<int64_t>* ids, | 624 std::list<int64_t>* ids, |
| 623 bool* invalid_id) { | 625 bool* invalid_id) { |
| 624 // For now, Move accepts ID parameters in the same way as an Update. | 626 // For now, Move accepts ID parameters in the same way as an Update. |
| 625 return BookmarksUpdateFunction::ExtractIds(args, ids, invalid_id); | 627 return BookmarksUpdateFunction::ExtractIds(args, ids, invalid_id); |
| 626 } | 628 } |
| 627 | 629 |
| 628 bool BookmarksMoveFunction::RunOnReady() { | 630 bool BookmarksMoveFunction::RunOnReady() { |
| 629 if (!EditBookmarksEnabled()) | 631 if (!EditBookmarksEnabled()) |
| 630 return false; | 632 return false; |
| 631 | 633 |
| 632 scoped_ptr<bookmarks::Move::Params> params( | 634 std::unique_ptr<bookmarks::Move::Params> params( |
| 633 bookmarks::Move::Params::Create(*args_)); | 635 bookmarks::Move::Params::Create(*args_)); |
| 634 EXTENSION_FUNCTION_VALIDATE(params.get()); | 636 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 635 | 637 |
| 636 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); | 638 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); |
| 637 if (!node) | 639 if (!node) |
| 638 return false; | 640 return false; |
| 639 | 641 |
| 640 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 642 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
| 641 if (model->is_permanent_node(node)) { | 643 if (model->is_permanent_node(node)) { |
| 642 error_ = keys::kModifySpecialError; | 644 error_ = keys::kModifySpecialError; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 std::list<int64_t>* ids, | 684 std::list<int64_t>* ids, |
| 683 bool* invalid_id) { | 685 bool* invalid_id) { |
| 684 // For now, Update accepts ID parameters in the same way as an Remove. | 686 // For now, Update accepts ID parameters in the same way as an Remove. |
| 685 return BookmarksRemoveFunction::ExtractIds(args, ids, invalid_id); | 687 return BookmarksRemoveFunction::ExtractIds(args, ids, invalid_id); |
| 686 } | 688 } |
| 687 | 689 |
| 688 bool BookmarksUpdateFunction::RunOnReady() { | 690 bool BookmarksUpdateFunction::RunOnReady() { |
| 689 if (!EditBookmarksEnabled()) | 691 if (!EditBookmarksEnabled()) |
| 690 return false; | 692 return false; |
| 691 | 693 |
| 692 scoped_ptr<bookmarks::Update::Params> params( | 694 std::unique_ptr<bookmarks::Update::Params> params( |
| 693 bookmarks::Update::Params::Create(*args_)); | 695 bookmarks::Update::Params::Create(*args_)); |
| 694 EXTENSION_FUNCTION_VALIDATE(params.get()); | 696 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 695 | 697 |
| 696 // Optional but we need to distinguish non present from an empty title. | 698 // Optional but we need to distinguish non present from an empty title. |
| 697 base::string16 title; | 699 base::string16 title; |
| 698 bool has_title = false; | 700 bool has_title = false; |
| 699 if (params->changes.title.get()) { | 701 if (params->changes.title.get()) { |
| 700 title = base::UTF8ToUTF16(*params->changes.title); | 702 title = base::UTF8ToUTF16(*params->changes.title); |
| 701 has_title = true; | 703 has_title = true; |
| 702 } | 704 } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 } | 839 } |
| 838 | 840 |
| 839 void BookmarksExportFunction::FileSelected(const base::FilePath& path, | 841 void BookmarksExportFunction::FileSelected(const base::FilePath& path, |
| 840 int index, | 842 int index, |
| 841 void* params) { | 843 void* params) { |
| 842 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); | 844 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); |
| 843 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 845 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
| 844 } | 846 } |
| 845 | 847 |
| 846 } // namespace extensions | 848 } // namespace extensions |
| OLD | NEW |