| 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/bookmark_manager_private/bookmark_manage
r_private_api.h" | 5 #include "chrome/browser/extensions/api/bookmark_manager_private/bookmark_manage
r_private_api.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/linked_ptr.h" | 10 #include "base/memory/linked_ptr.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_stats.h" | 16 #include "chrome/browser/bookmarks/bookmark_stats.h" |
| 17 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" |
| 17 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h" | 18 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h" |
| 18 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_helpers.h" | 19 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_helpers.h" |
| 19 #include "chrome/browser/extensions/extension_web_ui.h" | 20 #include "chrome/browser/extensions/extension_web_ui.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h" | 22 #include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h" |
| 22 #include "chrome/browser/undo/bookmark_undo_service.h" | 23 #include "chrome/browser/undo/bookmark_undo_service.h" |
| 23 #include "chrome/browser/undo/bookmark_undo_service_factory.h" | 24 #include "chrome/browser/undo/bookmark_undo_service_factory.h" |
| 24 #include "chrome/common/extensions/api/bookmark_manager_private.h" | 25 #include "chrome/common/extensions/api/bookmark_manager_private.h" |
| 25 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 26 #include "components/bookmarks/browser/bookmark_model.h" | 27 #include "components/bookmarks/browser/bookmark_model.h" |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 return &bookmark_drag_data_; | 345 return &bookmark_drag_data_; |
| 345 return NULL; | 346 return NULL; |
| 346 } | 347 } |
| 347 | 348 |
| 348 void BookmarkManagerPrivateDragEventRouter::ClearBookmarkNodeData() { | 349 void BookmarkManagerPrivateDragEventRouter::ClearBookmarkNodeData() { |
| 349 bookmark_drag_data_.Clear(); | 350 bookmark_drag_data_.Clear(); |
| 350 } | 351 } |
| 351 | 352 |
| 352 bool ClipboardBookmarkManagerFunction::CopyOrCut(bool cut, | 353 bool ClipboardBookmarkManagerFunction::CopyOrCut(bool cut, |
| 353 const std::vector<std::string>& id_list) { | 354 const std::vector<std::string>& id_list) { |
| 354 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 355 ChromeBookmarkClient* client = GetChromeBookmarkClient(); |
| 355 std::vector<const BookmarkNode*> nodes; | 356 std::vector<const BookmarkNode*> nodes; |
| 356 EXTENSION_FUNCTION_VALIDATE(GetNodesFromVector(model, id_list, &nodes)); | 357 EXTENSION_FUNCTION_VALIDATE( |
| 357 bookmark_utils::CopyToClipboard(model, nodes, cut); | 358 GetNodesFromVector(client->model(), id_list, &nodes)); |
| 359 if (cut && client->HasManagedNodes(nodes)) { |
| 360 error_ = bookmark_keys::kModifyManagedError; |
| 361 return false; |
| 362 } |
| 363 bookmark_utils::CopyToClipboard(client->model(), nodes, cut); |
| 358 return true; | 364 return true; |
| 359 } | 365 } |
| 360 | 366 |
| 361 bool BookmarkManagerPrivateCopyFunction::RunOnReady() { | 367 bool BookmarkManagerPrivateCopyFunction::RunOnReady() { |
| 362 scoped_ptr<Copy::Params> params(Copy::Params::Create(*args_)); | 368 scoped_ptr<Copy::Params> params(Copy::Params::Create(*args_)); |
| 363 EXTENSION_FUNCTION_VALIDATE(params); | 369 EXTENSION_FUNCTION_VALIDATE(params); |
| 364 return CopyOrCut(false, params->id_list); | 370 return CopyOrCut(false, params->id_list); |
| 365 } | 371 } |
| 366 | 372 |
| 367 bool BookmarkManagerPrivateCutFunction::RunOnReady() { | 373 bool BookmarkManagerPrivateCutFunction::RunOnReady() { |
| 368 if (!EditBookmarksEnabled()) | 374 if (!EditBookmarksEnabled()) |
| 369 return false; | 375 return false; |
| 370 | 376 |
| 371 scoped_ptr<Cut::Params> params(Cut::Params::Create(*args_)); | 377 scoped_ptr<Cut::Params> params(Cut::Params::Create(*args_)); |
| 372 EXTENSION_FUNCTION_VALIDATE(params); | 378 EXTENSION_FUNCTION_VALIDATE(params); |
| 373 return CopyOrCut(true, params->id_list); | 379 return CopyOrCut(true, params->id_list); |
| 374 } | 380 } |
| 375 | 381 |
| 376 bool BookmarkManagerPrivatePasteFunction::RunOnReady() { | 382 bool BookmarkManagerPrivatePasteFunction::RunOnReady() { |
| 377 if (!EditBookmarksEnabled()) | 383 if (!EditBookmarksEnabled()) |
| 378 return false; | 384 return false; |
| 379 | 385 |
| 380 scoped_ptr<Paste::Params> params(Paste::Params::Create(*args_)); | 386 scoped_ptr<Paste::Params> params(Paste::Params::Create(*args_)); |
| 381 EXTENSION_FUNCTION_VALIDATE(params); | 387 EXTENSION_FUNCTION_VALIDATE(params); |
| 382 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 388 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
| 383 const BookmarkNode* parent_node = GetNodeFromString(model, params->parent_id); | 389 const BookmarkNode* parent_node = GetNodeFromString(model, params->parent_id); |
| 384 if (!parent_node) { | 390 if (!CanBeModified(parent_node)) |
| 385 error_ = bookmark_keys::kNoParentError; | |
| 386 return false; | 391 return false; |
| 387 } | |
| 388 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); | 392 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); |
| 389 if (!can_paste) | 393 if (!can_paste) |
| 390 return false; | 394 return false; |
| 391 | 395 |
| 392 // We want to use the highest index of the selected nodes as a destination. | 396 // We want to use the highest index of the selected nodes as a destination. |
| 393 std::vector<const BookmarkNode*> nodes; | 397 std::vector<const BookmarkNode*> nodes; |
| 394 // No need to test return value, if we got an empty list, we insert at end. | 398 // No need to test return value, if we got an empty list, we insert at end. |
| 395 if (params->selected_id_list) | 399 if (params->selected_id_list) |
| 396 GetNodesFromVector(model, *params->selected_id_list, &nodes); | 400 GetNodesFromVector(model, *params->selected_id_list, &nodes); |
| 397 int highest_index = -1; // -1 means insert at end of list. | 401 int highest_index = -1; // -1 means insert at end of list. |
| 398 for (size_t i = 0; i < nodes.size(); ++i) { | 402 for (size_t i = 0; i < nodes.size(); ++i) { |
| 399 // + 1 so that we insert after the selection. | 403 // + 1 so that we insert after the selection. |
| 400 int index = parent_node->GetIndexOf(nodes[i]) + 1; | 404 int index = parent_node->GetIndexOf(nodes[i]) + 1; |
| 401 if (index > highest_index) | 405 if (index > highest_index) |
| 402 highest_index = index; | 406 highest_index = index; |
| 403 } | 407 } |
| 404 | 408 |
| 405 bookmark_utils::PasteFromClipboard(model, parent_node, highest_index); | 409 bookmark_utils::PasteFromClipboard(model, parent_node, highest_index); |
| 406 return true; | 410 return true; |
| 407 } | 411 } |
| 408 | 412 |
| 409 bool BookmarkManagerPrivateCanPasteFunction::RunOnReady() { | 413 bool BookmarkManagerPrivateCanPasteFunction::RunOnReady() { |
| 410 if (!EditBookmarksEnabled()) | 414 if (!EditBookmarksEnabled()) |
| 411 return false; | 415 return false; |
| 412 | 416 |
| 413 scoped_ptr<CanPaste::Params> params(CanPaste::Params::Create(*args_)); | 417 scoped_ptr<CanPaste::Params> params(CanPaste::Params::Create(*args_)); |
| 414 EXTENSION_FUNCTION_VALIDATE(params); | 418 EXTENSION_FUNCTION_VALIDATE(params); |
| 415 | 419 |
| 416 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 420 ChromeBookmarkClient* client = GetChromeBookmarkClient(); |
| 417 const BookmarkNode* parent_node = GetNodeFromString(model, params->parent_id); | 421 const BookmarkNode* parent_node = |
| 422 GetNodeFromString(client->model(), params->parent_id); |
| 418 if (!parent_node) { | 423 if (!parent_node) { |
| 419 error_ = bookmark_keys::kNoParentError; | 424 error_ = bookmark_keys::kNoParentError; |
| 420 return false; | 425 return false; |
| 421 } | 426 } |
| 422 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); | 427 bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node) && |
| 428 !client->IsAManagedNode(parent_node); |
| 423 SetResult(new base::FundamentalValue(can_paste)); | 429 SetResult(new base::FundamentalValue(can_paste)); |
| 424 return true; | 430 return true; |
| 425 } | 431 } |
| 426 | 432 |
| 427 bool BookmarkManagerPrivateSortChildrenFunction::RunOnReady() { | 433 bool BookmarkManagerPrivateSortChildrenFunction::RunOnReady() { |
| 428 if (!EditBookmarksEnabled()) | 434 if (!EditBookmarksEnabled()) |
| 429 return false; | 435 return false; |
| 430 | 436 |
| 431 scoped_ptr<SortChildren::Params> params(SortChildren::Params::Create(*args_)); | 437 scoped_ptr<SortChildren::Params> params(SortChildren::Params::Create(*args_)); |
| 432 EXTENSION_FUNCTION_VALIDATE(params); | 438 EXTENSION_FUNCTION_VALIDATE(params); |
| 433 | 439 |
| 434 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 440 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
| 435 const BookmarkNode* parent_node = GetNodeFromString(model, params->parent_id); | 441 const BookmarkNode* parent_node = GetNodeFromString(model, params->parent_id); |
| 436 if (!parent_node) { | 442 if (!CanBeModified(parent_node)) |
| 437 error_ = bookmark_keys::kNoParentError; | |
| 438 return false; | 443 return false; |
| 439 } | |
| 440 model->SortChildren(parent_node); | 444 model->SortChildren(parent_node); |
| 441 return true; | 445 return true; |
| 442 } | 446 } |
| 443 | 447 |
| 444 bool BookmarkManagerPrivateGetStringsFunction::RunAsync() { | 448 bool BookmarkManagerPrivateGetStringsFunction::RunAsync() { |
| 445 base::DictionaryValue* localized_strings = new base::DictionaryValue(); | 449 base::DictionaryValue* localized_strings = new base::DictionaryValue(); |
| 446 | 450 |
| 447 localized_strings->SetString("title", | 451 localized_strings->SetString("title", |
| 448 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_TITLE)); | 452 l10n_util::GetStringUTF16(IDS_BOOKMARK_MANAGER_TITLE)); |
| 449 localized_strings->SetString("search_button", | 453 localized_strings->SetString("search_button", |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 bool BookmarkManagerPrivateDropFunction::RunOnReady() { | 561 bool BookmarkManagerPrivateDropFunction::RunOnReady() { |
| 558 if (!EditBookmarksEnabled()) | 562 if (!EditBookmarksEnabled()) |
| 559 return false; | 563 return false; |
| 560 | 564 |
| 561 scoped_ptr<Drop::Params> params(Drop::Params::Create(*args_)); | 565 scoped_ptr<Drop::Params> params(Drop::Params::Create(*args_)); |
| 562 EXTENSION_FUNCTION_VALIDATE(params); | 566 EXTENSION_FUNCTION_VALIDATE(params); |
| 563 | 567 |
| 564 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 568 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); |
| 565 | 569 |
| 566 const BookmarkNode* drop_parent = GetNodeFromString(model, params->parent_id); | 570 const BookmarkNode* drop_parent = GetNodeFromString(model, params->parent_id); |
| 567 if (!drop_parent) { | 571 if (!CanBeModified(drop_parent)) |
| 568 error_ = bookmark_keys::kNoParentError; | |
| 569 return false; | 572 return false; |
| 570 } | |
| 571 | 573 |
| 572 int drop_index; | 574 int drop_index; |
| 573 if (params->index) | 575 if (params->index) |
| 574 drop_index = *params->index; | 576 drop_index = *params->index; |
| 575 else | 577 else |
| 576 drop_index = drop_parent->child_count(); | 578 drop_index = drop_parent->child_count(); |
| 577 | 579 |
| 578 WebContents* web_contents = | 580 WebContents* web_contents = |
| 579 WebContents::FromRenderViewHost(render_view_host_); | 581 WebContents::FromRenderViewHost(render_view_host_); |
| 580 if (GetViewType(web_contents) == VIEW_TYPE_TAB_CONTENTS) { | 582 if (GetViewType(web_contents) == VIEW_TYPE_TAB_CONTENTS) { |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 bool BookmarkManagerPrivateCanOpenNewWindowsFunction::RunOnReady() { | 725 bool BookmarkManagerPrivateCanOpenNewWindowsFunction::RunOnReady() { |
| 724 bool can_open_new_windows = true; | 726 bool can_open_new_windows = true; |
| 725 SetResult(new base::FundamentalValue(can_open_new_windows)); | 727 SetResult(new base::FundamentalValue(can_open_new_windows)); |
| 726 return true; | 728 return true; |
| 727 } | 729 } |
| 728 | 730 |
| 729 bool BookmarkManagerPrivateRemoveTreesFunction::RunOnReady() { | 731 bool BookmarkManagerPrivateRemoveTreesFunction::RunOnReady() { |
| 730 scoped_ptr<RemoveTrees::Params> params(RemoveTrees::Params::Create(*args_)); | 732 scoped_ptr<RemoveTrees::Params> params(RemoveTrees::Params::Create(*args_)); |
| 731 EXTENSION_FUNCTION_VALIDATE(params); | 733 EXTENSION_FUNCTION_VALIDATE(params); |
| 732 | 734 |
| 733 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); | 735 ChromeBookmarkClient* client = GetChromeBookmarkClient(); |
| 734 #if !defined(OS_ANDROID) | 736 #if !defined(OS_ANDROID) |
| 735 bookmarks::ScopedGroupBookmarkActions group_deletes(model); | 737 bookmarks::ScopedGroupBookmarkActions group_deletes(client->model()); |
| 736 #endif | 738 #endif |
| 737 int64 id; | 739 int64 id; |
| 738 for (size_t i = 0; i < params->id_list.size(); ++i) { | 740 for (size_t i = 0; i < params->id_list.size(); ++i) { |
| 739 if (!GetBookmarkIdAsInt64(params->id_list[i], &id)) | 741 if (!GetBookmarkIdAsInt64(params->id_list[i], &id)) |
| 740 return false; | 742 return false; |
| 741 if (!bookmark_api_helpers::RemoveNode(model, id, true, &error_)) | 743 if (!bookmark_api_helpers::RemoveNode(client, id, true, &error_)) |
| 742 return false; | 744 return false; |
| 743 } | 745 } |
| 744 | 746 |
| 745 return true; | 747 return true; |
| 746 } | 748 } |
| 747 | 749 |
| 748 bool BookmarkManagerPrivateUndoFunction::RunOnReady() { | 750 bool BookmarkManagerPrivateUndoFunction::RunOnReady() { |
| 749 #if !defined(OS_ANDROID) | 751 #if !defined(OS_ANDROID) |
| 750 BookmarkUndoServiceFactory::GetForProfile(GetProfile())->undo_manager()-> | 752 BookmarkUndoServiceFactory::GetForProfile(GetProfile())->undo_manager()-> |
| 751 Undo(); | 753 Undo(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 result.enabled = undo_manager->redo_count() > 0; | 789 result.enabled = undo_manager->redo_count() > 0; |
| 788 result.label = base::UTF16ToUTF8(undo_manager->GetRedoLabel()); | 790 result.label = base::UTF16ToUTF8(undo_manager->GetRedoLabel()); |
| 789 | 791 |
| 790 results_ = RedoInfo::Results::Create(result); | 792 results_ = RedoInfo::Results::Create(result); |
| 791 #endif // !defined(OS_ANDROID) | 793 #endif // !defined(OS_ANDROID) |
| 792 | 794 |
| 793 return true; | 795 return true; |
| 794 } | 796 } |
| 795 | 797 |
| 796 } // namespace extensions | 798 } // namespace extensions |
| OLD | NEW |