Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: chrome/browser/extensions/api/bookmarks/bookmarks_api.cc

Issue 320473002: Added the "unmodifiable" property to chrome.bookmarks.BookmarkTreeNode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 void BookmarksFunction::BookmarkModelChanged() { 230 void BookmarksFunction::BookmarkModelChanged() {
231 } 231 }
232 232
233 void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model, 233 void BookmarksFunction::BookmarkModelLoaded(BookmarkModel* model,
234 bool ids_reassigned) { 234 bool ids_reassigned) {
235 model->RemoveObserver(this); 235 model->RemoveObserver(this);
236 RunOnReady(); 236 RunOnReady();
237 Release(); // Balanced in RunOnReady(). 237 Release(); // Balanced in RunOnReady().
238 } 238 }
239 239
240 BookmarkEventRouter::BookmarkEventRouter(BrowserContext* context, 240 BookmarkEventRouter::BookmarkEventRouter(Profile* profile)
241 BookmarkModel* model) 241 : browser_context_(profile),
242 : browser_context_(context), model_(model) { 242 model_(BookmarkModelFactory::GetForProfile(profile)),
243 client_(
244 BookmarkModelFactory::GetChromeBookmarkClientForProfile(profile)) {
not at google - send to devlin 2014/06/05 20:58:26 kinda weird that BookmarkModelFactory is a Browser
Joao da Silva 2014/06/05 21:04:16 The main getter is GetForProfile(), so I used this
not at google - send to devlin 2014/06/05 22:21:32 I didn't mean to imply that it was something to do
243 model_->AddObserver(this); 245 model_->AddObserver(this);
244 } 246 }
245 247
246 BookmarkEventRouter::~BookmarkEventRouter() { 248 BookmarkEventRouter::~BookmarkEventRouter() {
247 if (model_) { 249 if (model_) {
248 model_->RemoveObserver(this); 250 model_->RemoveObserver(this);
249 } 251 }
250 } 252 }
251 253
252 void BookmarkEventRouter::DispatchEvent( 254 void BookmarkEventRouter::DispatchEvent(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 DispatchEvent( 286 DispatchEvent(
285 bookmarks::OnMoved::kEventName, 287 bookmarks::OnMoved::kEventName,
286 bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info)); 288 bookmarks::OnMoved::Create(base::Int64ToString(node->id()), move_info));
287 } 289 }
288 290
289 void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, 291 void BookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model,
290 const BookmarkNode* parent, 292 const BookmarkNode* parent,
291 int index) { 293 int index) {
292 const BookmarkNode* node = parent->GetChild(index); 294 const BookmarkNode* node = parent->GetChild(index);
293 scoped_ptr<BookmarkTreeNode> tree_node( 295 scoped_ptr<BookmarkTreeNode> tree_node(
294 bookmark_api_helpers::GetBookmarkTreeNode(node, false, false)); 296 bookmark_api_helpers::GetBookmarkTreeNode(client_, node, false, false));
295 DispatchEvent(bookmarks::OnCreated::kEventName, 297 DispatchEvent(bookmarks::OnCreated::kEventName,
296 bookmarks::OnCreated::Create(base::Int64ToString(node->id()), 298 bookmarks::OnCreated::Create(base::Int64ToString(node->id()),
297 *tree_node)); 299 *tree_node));
298 } 300 }
299 301
300 void BookmarkEventRouter::BookmarkNodeRemoved( 302 void BookmarkEventRouter::BookmarkNodeRemoved(
301 BookmarkModel* model, 303 BookmarkModel* model,
302 const BookmarkNode* parent, 304 const BookmarkNode* parent,
303 int index, 305 int index,
304 const BookmarkNode* node, 306 const BookmarkNode* node,
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 static base::LazyInstance<BrowserContextKeyedAPIFactory<BookmarksAPI> > 394 static base::LazyInstance<BrowserContextKeyedAPIFactory<BookmarksAPI> >
393 g_factory = LAZY_INSTANCE_INITIALIZER; 395 g_factory = LAZY_INSTANCE_INITIALIZER;
394 396
395 // static 397 // static
396 BrowserContextKeyedAPIFactory<BookmarksAPI>* 398 BrowserContextKeyedAPIFactory<BookmarksAPI>*
397 BookmarksAPI::GetFactoryInstance() { 399 BookmarksAPI::GetFactoryInstance() {
398 return g_factory.Pointer(); 400 return g_factory.Pointer();
399 } 401 }
400 402
401 void BookmarksAPI::OnListenerAdded(const EventListenerInfo& details) { 403 void BookmarksAPI::OnListenerAdded(const EventListenerInfo& details) {
402 bookmark_event_router_.reset(new BookmarkEventRouter( 404 bookmark_event_router_.reset(
403 browser_context_, 405 new BookmarkEventRouter(Profile::FromBrowserContext(browser_context_)));
404 BookmarkModelFactory::GetForProfile(
405 Profile::FromBrowserContext(browser_context_))));
406 EventRouter::Get(browser_context_)->UnregisterObserver(this); 406 EventRouter::Get(browser_context_)->UnregisterObserver(this);
407 } 407 }
408 408
409 bool BookmarksGetFunction::RunOnReady() { 409 bool BookmarksGetFunction::RunOnReady() {
410 scoped_ptr<bookmarks::Get::Params> params( 410 scoped_ptr<bookmarks::Get::Params> params(
411 bookmarks::Get::Params::Create(*args_)); 411 bookmarks::Get::Params::Create(*args_));
412 EXTENSION_FUNCTION_VALIDATE(params.get()); 412 EXTENSION_FUNCTION_VALIDATE(params.get());
413 413
414 std::vector<linked_ptr<BookmarkTreeNode> > nodes; 414 std::vector<linked_ptr<BookmarkTreeNode> > nodes;
415 ChromeBookmarkClient* client = GetChromeBookmarkClient();
415 if (params->id_or_id_list.as_strings) { 416 if (params->id_or_id_list.as_strings) {
416 std::vector<std::string>& ids = *params->id_or_id_list.as_strings; 417 std::vector<std::string>& ids = *params->id_or_id_list.as_strings;
417 size_t count = ids.size(); 418 size_t count = ids.size();
418 EXTENSION_FUNCTION_VALIDATE(count > 0); 419 EXTENSION_FUNCTION_VALIDATE(count > 0);
419 for (size_t i = 0; i < count; ++i) { 420 for (size_t i = 0; i < count; ++i) {
420 const BookmarkNode* node = GetBookmarkNodeFromId(ids[i]); 421 const BookmarkNode* node = GetBookmarkNodeFromId(ids[i]);
421 if (!node) 422 if (!node)
422 return false; 423 return false;
423 bookmark_api_helpers::AddNode(node, &nodes, false); 424 bookmark_api_helpers::AddNode(client, node, &nodes, false);
424 } 425 }
425 } else { 426 } else {
426 const BookmarkNode* node = 427 const BookmarkNode* node =
427 GetBookmarkNodeFromId(*params->id_or_id_list.as_string); 428 GetBookmarkNodeFromId(*params->id_or_id_list.as_string);
428 if (!node) 429 if (!node)
429 return false; 430 return false;
430 bookmark_api_helpers::AddNode(node, &nodes, false); 431 bookmark_api_helpers::AddNode(client, node, &nodes, false);
431 } 432 }
432 433
433 results_ = bookmarks::Get::Results::Create(nodes); 434 results_ = bookmarks::Get::Results::Create(nodes);
434 return true; 435 return true;
435 } 436 }
436 437
437 bool BookmarksGetChildrenFunction::RunOnReady() { 438 bool BookmarksGetChildrenFunction::RunOnReady() {
438 scoped_ptr<bookmarks::GetChildren::Params> params( 439 scoped_ptr<bookmarks::GetChildren::Params> params(
439 bookmarks::GetChildren::Params::Create(*args_)); 440 bookmarks::GetChildren::Params::Create(*args_));
440 EXTENSION_FUNCTION_VALIDATE(params.get()); 441 EXTENSION_FUNCTION_VALIDATE(params.get());
441 442
442 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); 443 const BookmarkNode* node = GetBookmarkNodeFromId(params->id);
443 if (!node) 444 if (!node)
444 return false; 445 return false;
445 446
446 std::vector<linked_ptr<BookmarkTreeNode> > nodes; 447 std::vector<linked_ptr<BookmarkTreeNode> > nodes;
447 int child_count = node->child_count(); 448 int child_count = node->child_count();
448 for (int i = 0; i < child_count; ++i) { 449 for (int i = 0; i < child_count; ++i) {
449 const BookmarkNode* child = node->GetChild(i); 450 const BookmarkNode* child = node->GetChild(i);
450 bookmark_api_helpers::AddNode(child, &nodes, false); 451 bookmark_api_helpers::AddNode(
452 GetChromeBookmarkClient(), child, &nodes, false);
451 } 453 }
452 454
453 results_ = bookmarks::GetChildren::Results::Create(nodes); 455 results_ = bookmarks::GetChildren::Results::Create(nodes);
454 return true; 456 return true;
455 } 457 }
456 458
457 bool BookmarksGetRecentFunction::RunOnReady() { 459 bool BookmarksGetRecentFunction::RunOnReady() {
458 scoped_ptr<bookmarks::GetRecent::Params> params( 460 scoped_ptr<bookmarks::GetRecent::Params> params(
459 bookmarks::GetRecent::Params::Create(*args_)); 461 bookmarks::GetRecent::Params::Create(*args_));
460 EXTENSION_FUNCTION_VALIDATE(params.get()); 462 EXTENSION_FUNCTION_VALIDATE(params.get());
461 if (params->number_of_items < 1) 463 if (params->number_of_items < 1)
462 return false; 464 return false;
463 465
464 std::vector<const BookmarkNode*> nodes; 466 std::vector<const BookmarkNode*> nodes;
465 bookmark_utils::GetMostRecentlyAddedEntries( 467 bookmark_utils::GetMostRecentlyAddedEntries(
466 BookmarkModelFactory::GetForProfile(GetProfile()), 468 BookmarkModelFactory::GetForProfile(GetProfile()),
467 params->number_of_items, 469 params->number_of_items,
468 &nodes); 470 &nodes);
469 471
470 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; 472 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes;
471 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); 473 std::vector<const BookmarkNode*>::iterator i = nodes.begin();
472 for (; i != nodes.end(); ++i) { 474 for (; i != nodes.end(); ++i) {
473 const BookmarkNode* node = *i; 475 const BookmarkNode* node = *i;
474 bookmark_api_helpers::AddNode(node, &tree_nodes, false); 476 bookmark_api_helpers::AddNode(
477 GetChromeBookmarkClient(), node, &tree_nodes, false);
475 } 478 }
476 479
477 results_ = bookmarks::GetRecent::Results::Create(tree_nodes); 480 results_ = bookmarks::GetRecent::Results::Create(tree_nodes);
478 return true; 481 return true;
479 } 482 }
480 483
481 bool BookmarksGetTreeFunction::RunOnReady() { 484 bool BookmarksGetTreeFunction::RunOnReady() {
482 std::vector<linked_ptr<BookmarkTreeNode> > nodes; 485 std::vector<linked_ptr<BookmarkTreeNode> > nodes;
483 const BookmarkNode* node = 486 const BookmarkNode* node =
484 BookmarkModelFactory::GetForProfile(GetProfile())->root_node(); 487 BookmarkModelFactory::GetForProfile(GetProfile())->root_node();
485 bookmark_api_helpers::AddNode(node, &nodes, true); 488 bookmark_api_helpers::AddNode(GetChromeBookmarkClient(), node, &nodes, true);
486 results_ = bookmarks::GetTree::Results::Create(nodes); 489 results_ = bookmarks::GetTree::Results::Create(nodes);
487 return true; 490 return true;
488 } 491 }
489 492
490 bool BookmarksGetSubTreeFunction::RunOnReady() { 493 bool BookmarksGetSubTreeFunction::RunOnReady() {
491 scoped_ptr<bookmarks::GetSubTree::Params> params( 494 scoped_ptr<bookmarks::GetSubTree::Params> params(
492 bookmarks::GetSubTree::Params::Create(*args_)); 495 bookmarks::GetSubTree::Params::Create(*args_));
493 EXTENSION_FUNCTION_VALIDATE(params.get()); 496 EXTENSION_FUNCTION_VALIDATE(params.get());
494 497
495 const BookmarkNode* node = GetBookmarkNodeFromId(params->id); 498 const BookmarkNode* node = GetBookmarkNodeFromId(params->id);
496 if (!node) 499 if (!node)
497 return false; 500 return false;
498 501
499 std::vector<linked_ptr<BookmarkTreeNode> > nodes; 502 std::vector<linked_ptr<BookmarkTreeNode> > nodes;
500 bookmark_api_helpers::AddNode(node, &nodes, true); 503 bookmark_api_helpers::AddNode(GetChromeBookmarkClient(), node, &nodes, true);
501 results_ = bookmarks::GetSubTree::Results::Create(nodes); 504 results_ = bookmarks::GetSubTree::Results::Create(nodes);
502 return true; 505 return true;
503 } 506 }
504 507
505 bool BookmarksSearchFunction::RunOnReady() { 508 bool BookmarksSearchFunction::RunOnReady() {
506 scoped_ptr<bookmarks::Search::Params> params( 509 scoped_ptr<bookmarks::Search::Params> params(
507 bookmarks::Search::Params::Create(*args_)); 510 bookmarks::Search::Params::Create(*args_));
508 EXTENSION_FUNCTION_VALIDATE(params.get()); 511 EXTENSION_FUNCTION_VALIDATE(params.get());
509 512
510 PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile()); 513 PrefService* prefs = user_prefs::UserPrefs::Get(GetProfile());
(...skipping 24 matching lines...) Expand all
535 query.title.reset(new base::string16(base::UTF8ToUTF16(*object.title))); 538 query.title.reset(new base::string16(base::UTF8ToUTF16(*object.title)));
536 bookmark_utils::GetBookmarksMatchingProperties( 539 bookmark_utils::GetBookmarksMatchingProperties(
537 BookmarkModelFactory::GetForProfile(GetProfile()), 540 BookmarkModelFactory::GetForProfile(GetProfile()),
538 query, 541 query,
539 std::numeric_limits<int>::max(), 542 std::numeric_limits<int>::max(),
540 lang, 543 lang,
541 &nodes); 544 &nodes);
542 } 545 }
543 546
544 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes; 547 std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes;
548 ChromeBookmarkClient* client = GetChromeBookmarkClient();
545 for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin(); 549 for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin();
546 node_iter != nodes.end(); ++node_iter) { 550 node_iter != nodes.end(); ++node_iter) {
547 bookmark_api_helpers::AddNode(*node_iter, &tree_nodes, false); 551 bookmark_api_helpers::AddNode(client, *node_iter, &tree_nodes, false);
548 } 552 }
549 553
550 results_ = bookmarks::Search::Results::Create(tree_nodes); 554 results_ = bookmarks::Search::Results::Create(tree_nodes);
551 return true; 555 return true;
552 } 556 }
553 557
554 // static 558 // static
555 bool BookmarksRemoveFunction::ExtractIds(const base::ListValue* args, 559 bool BookmarksRemoveFunction::ExtractIds(const base::ListValue* args,
556 std::list<int64>* ids, 560 std::list<int64>* ids,
557 bool* invalid_id) { 561 bool* invalid_id) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 599
596 scoped_ptr<bookmarks::Create::Params> params( 600 scoped_ptr<bookmarks::Create::Params> params(
597 bookmarks::Create::Params::Create(*args_)); 601 bookmarks::Create::Params::Create(*args_));
598 EXTENSION_FUNCTION_VALIDATE(params.get()); 602 EXTENSION_FUNCTION_VALIDATE(params.get());
599 603
600 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile()); 604 BookmarkModel* model = BookmarkModelFactory::GetForProfile(GetProfile());
601 const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL); 605 const BookmarkNode* node = CreateBookmarkNode(model, params->bookmark, NULL);
602 if (!node) 606 if (!node)
603 return false; 607 return false;
604 608
605 scoped_ptr<BookmarkTreeNode> ret( 609 scoped_ptr<BookmarkTreeNode> ret(bookmark_api_helpers::GetBookmarkTreeNode(
606 bookmark_api_helpers::GetBookmarkTreeNode(node, false, false)); 610 GetChromeBookmarkClient(), node, false, false));
607 results_ = bookmarks::Create::Results::Create(*ret); 611 results_ = bookmarks::Create::Results::Create(*ret);
608 612
609 return true; 613 return true;
610 } 614 }
611 615
612 // static 616 // static
613 bool BookmarksMoveFunction::ExtractIds(const base::ListValue* args, 617 bool BookmarksMoveFunction::ExtractIds(const base::ListValue* args,
614 std::list<int64>* ids, 618 std::list<int64>* ids,
615 bool* invalid_id) { 619 bool* invalid_id) {
616 // For now, Move accepts ID parameters in the same way as an Update. 620 // For now, Move accepts ID parameters in the same way as an Update.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 error_ = keys::kInvalidIndexError; 660 error_ = keys::kInvalidIndexError;
657 return false; 661 return false;
658 } 662 }
659 } else { 663 } else {
660 index = parent->child_count(); 664 index = parent->child_count();
661 } 665 }
662 666
663 model->Move(node, parent, index); 667 model->Move(node, parent, index);
664 668
665 scoped_ptr<BookmarkTreeNode> tree_node( 669 scoped_ptr<BookmarkTreeNode> tree_node(
666 bookmark_api_helpers::GetBookmarkTreeNode(node, false, false)); 670 bookmark_api_helpers::GetBookmarkTreeNode(
671 GetChromeBookmarkClient(), node, false, false));
667 results_ = bookmarks::Move::Results::Create(*tree_node); 672 results_ = bookmarks::Move::Results::Create(*tree_node);
668 673
669 return true; 674 return true;
670 } 675 }
671 676
672 // static 677 // static
673 bool BookmarksUpdateFunction::ExtractIds(const base::ListValue* args, 678 bool BookmarksUpdateFunction::ExtractIds(const base::ListValue* args,
674 std::list<int64>* ids, 679 std::list<int64>* ids,
675 bool* invalid_id) { 680 bool* invalid_id) {
676 // For now, Update accepts ID parameters in the same way as an Remove. 681 // For now, Update accepts ID parameters in the same way as an Remove.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 if (model->is_permanent_node(node)) { 716 if (model->is_permanent_node(node)) {
712 error_ = keys::kModifySpecialError; 717 error_ = keys::kModifySpecialError;
713 return false; 718 return false;
714 } 719 }
715 if (has_title) 720 if (has_title)
716 model->SetTitle(node, title); 721 model->SetTitle(node, title);
717 if (!url.is_empty()) 722 if (!url.is_empty())
718 model->SetURL(node, url); 723 model->SetURL(node, url);
719 724
720 scoped_ptr<BookmarkTreeNode> tree_node( 725 scoped_ptr<BookmarkTreeNode> tree_node(
721 bookmark_api_helpers::GetBookmarkTreeNode(node, false, false)); 726 bookmark_api_helpers::GetBookmarkTreeNode(
727 GetChromeBookmarkClient(), node, false, false));
722 results_ = bookmarks::Update::Results::Create(*tree_node); 728 results_ = bookmarks::Update::Results::Create(*tree_node);
723 return true; 729 return true;
724 } 730 }
725 731
726 // Mapper superclass for BookmarkFunctions. 732 // Mapper superclass for BookmarkFunctions.
727 template <typename BucketIdType> 733 template <typename BucketIdType>
728 class BookmarkBucketMapper : public BucketMapper { 734 class BookmarkBucketMapper : public BucketMapper {
729 public: 735 public:
730 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); } 736 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); }
731 protected: 737 protected:
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 #if !defined(OS_ANDROID) 1040 #if !defined(OS_ANDROID)
1035 // Android does not have support for the standard exporter. 1041 // Android does not have support for the standard exporter.
1036 // TODO(jgreenwald): remove ifdef once extensions are no longer built on 1042 // TODO(jgreenwald): remove ifdef once extensions are no longer built on
1037 // Android. 1043 // Android.
1038 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL); 1044 bookmark_html_writer::WriteBookmarks(GetProfile(), path, NULL);
1039 #endif 1045 #endif
1040 Release(); // Balanced in BookmarksIOFunction::SelectFile() 1046 Release(); // Balanced in BookmarksIOFunction::SelectFile()
1041 } 1047 }
1042 1048
1043 } // namespace extensions 1049 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698