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

Side by Side Diff: chrome/browser/ui/views/bookmarks/bookmark_editor_view.cc

Issue 2379863002: Fix object ownership in ui/base/models. (Closed)
Patch Set: fix Created 4 years, 2 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
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/ui/views/bookmarks/bookmark_editor_view.h" 5 #include "chrome/browser/ui/views/bookmarks/bookmark_editor_view.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
10 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 13 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" 15 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
15 #include "chrome/browser/ui/bookmarks/bookmark_utils_desktop.h" 16 #include "chrome/browser/ui/bookmarks/bookmark_utils_desktop.h"
16 #include "chrome/browser/ui/browser_dialogs.h" 17 #include "chrome/browser/ui/browser_dialogs.h"
17 #include "chrome/grit/generated_resources.h" 18 #include "chrome/grit/generated_resources.h"
18 #include "chrome/grit/locale_settings.h" 19 #include "chrome/grit/locale_settings.h"
19 #include "components/bookmarks/browser/bookmark_model.h" 20 #include "components/bookmarks/browser/bookmark_model.h"
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 UserInputChanged(); 408 UserInputChanged();
408 return; 409 return;
409 } 410 }
410 411
411 new_folder_button_->SetEnabled(true); 412 new_folder_button_->SetEnabled(true);
412 413
413 // Do this first, otherwise when we invoke SetModel with the real one 414 // Do this first, otherwise when we invoke SetModel with the real one
414 // tree_view will try to invoke something on the model we just deleted. 415 // tree_view will try to invoke something on the model we just deleted.
415 tree_view_->SetModel(NULL); 416 tree_view_->SetModel(NULL);
416 417
417 EditorNode* root_node = CreateRootNode(); 418 tree_model_.reset(new EditorTreeModel(CreateRootNode()));
418 tree_model_.reset(new EditorTreeModel(root_node));
419 419
420 tree_view_->SetModel(tree_model_.get()); 420 tree_view_->SetModel(tree_model_.get());
421 tree_view_->SetController(this); 421 tree_view_->SetController(this);
422 422
423 context_menu_runner_.reset(); 423 context_menu_runner_.reset();
424 424
425 if (parent()) 425 if (parent())
426 ExpandAndSelect(); 426 ExpandAndSelect();
427 } 427 }
428 428
(...skipping 22 matching lines...) Expand all
451 if (!parent) { 451 if (!parent) {
452 NOTREACHED(); 452 NOTREACHED();
453 return; 453 return;
454 } 454 }
455 455
456 tree_view_->StartEditing(AddNewFolder(parent)); 456 tree_view_->StartEditing(AddNewFolder(parent));
457 } 457 }
458 458
459 BookmarkEditorView::EditorNode* BookmarkEditorView::AddNewFolder( 459 BookmarkEditorView::EditorNode* BookmarkEditorView::AddNewFolder(
460 EditorNode* parent) { 460 EditorNode* parent) {
461 EditorNode* new_node = new EditorNode( 461 return tree_model_->Add(
462 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME), 0); 462 parent,
463 // |new_node| is now owned by |parent|. 463 base::MakeUnique<EditorNode>(
464 tree_model_->Add(parent, new_node, parent->child_count()); 464 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME), 0),
465 return new_node; 465 parent->child_count());
466 } 466 }
467 467
468 void BookmarkEditorView::ExpandAndSelect() { 468 void BookmarkEditorView::ExpandAndSelect() {
469 BookmarkExpandedStateTracker::Nodes expanded_nodes = 469 BookmarkExpandedStateTracker::Nodes expanded_nodes =
470 bb_model_->expanded_state_tracker()->GetExpandedNodes(); 470 bb_model_->expanded_state_tracker()->GetExpandedNodes();
471 for (BookmarkExpandedStateTracker::Nodes::const_iterator i( 471 for (BookmarkExpandedStateTracker::Nodes::const_iterator i(
472 expanded_nodes.begin()); i != expanded_nodes.end(); ++i) { 472 expanded_nodes.begin()); i != expanded_nodes.end(); ++i) {
473 EditorNode* editor_node = 473 EditorNode* editor_node =
474 FindNodeWithID(tree_model_->GetRoot(), (*i)->id()); 474 FindNodeWithID(tree_model_->GetRoot(), (*i)->id());
475 if (editor_node) 475 if (editor_node)
476 tree_view_->Expand(editor_node); 476 tree_view_->Expand(editor_node);
477 } 477 }
478 478
479 const BookmarkNode* to_select = parent_; 479 const BookmarkNode* to_select = parent_;
480 if (details_.type == EditDetails::EXISTING_NODE) 480 if (details_.type == EditDetails::EXISTING_NODE)
481 to_select = details_.existing_node->parent(); 481 to_select = details_.existing_node->parent();
482 int64_t folder_id_to_select = to_select->id(); 482 int64_t folder_id_to_select = to_select->id();
483 EditorNode* b_node = 483 EditorNode* b_node =
484 FindNodeWithID(tree_model_->GetRoot(), folder_id_to_select); 484 FindNodeWithID(tree_model_->GetRoot(), folder_id_to_select);
485 if (!b_node) 485 if (!b_node)
486 b_node = tree_model_->GetRoot()->GetChild(0); // Bookmark bar node. 486 b_node = tree_model_->GetRoot()->GetChild(0); // Bookmark bar node.
487 487
488 tree_view_->SetSelectedNode(b_node); 488 tree_view_->SetSelectedNode(b_node);
489 } 489 }
490 490
491 BookmarkEditorView::EditorNode* BookmarkEditorView::CreateRootNode() { 491 std::unique_ptr<BookmarkEditorView::EditorNode>
492 EditorNode* root_node = new EditorNode(base::string16(), 0); 492 BookmarkEditorView::CreateRootNode() {
493 std::unique_ptr<EditorNode> root_node =
494 base::MakeUnique<EditorNode>(base::string16(), 0);
493 const BookmarkNode* bb_root_node = bb_model_->root_node(); 495 const BookmarkNode* bb_root_node = bb_model_->root_node();
494 CreateNodes(bb_root_node, root_node); 496 CreateNodes(bb_root_node, root_node.get());
495 DCHECK(root_node->child_count() >= 2 && root_node->child_count() <= 4); 497 DCHECK(root_node->child_count() >= 2 && root_node->child_count() <= 4);
496 DCHECK_EQ(BookmarkNode::BOOKMARK_BAR, bb_root_node->GetChild(0)->type()); 498 DCHECK_EQ(BookmarkNode::BOOKMARK_BAR, bb_root_node->GetChild(0)->type());
497 DCHECK_EQ(BookmarkNode::OTHER_NODE, bb_root_node->GetChild(1)->type()); 499 DCHECK_EQ(BookmarkNode::OTHER_NODE, bb_root_node->GetChild(1)->type());
498 if (root_node->child_count() >= 3) 500 if (root_node->child_count() >= 3)
499 DCHECK_EQ(BookmarkNode::MOBILE, bb_root_node->GetChild(2)->type()); 501 DCHECK_EQ(BookmarkNode::MOBILE, bb_root_node->GetChild(2)->type());
500 return root_node; 502 return root_node;
501 } 503 }
502 504
503 void BookmarkEditorView::CreateNodes(const BookmarkNode* bb_node, 505 void BookmarkEditorView::CreateNodes(const BookmarkNode* bb_node,
504 BookmarkEditorView::EditorNode* b_node) { 506 BookmarkEditorView::EditorNode* b_node) {
505 for (int i = 0; i < bb_node->child_count(); ++i) { 507 for (int i = 0; i < bb_node->child_count(); ++i) {
506 const BookmarkNode* child_bb_node = bb_node->GetChild(i); 508 const BookmarkNode* child_bb_node = bb_node->GetChild(i);
507 if (child_bb_node->IsVisible() && child_bb_node->is_folder() && 509 if (child_bb_node->IsVisible() && child_bb_node->is_folder() &&
508 bb_model_->client()->CanBeEditedByUser(child_bb_node)) { 510 bb_model_->client()->CanBeEditedByUser(child_bb_node)) {
509 EditorNode* new_b_node = new EditorNode(child_bb_node->GetTitle(), 511 EditorNode* new_b_node =
510 child_bb_node->id()); 512 b_node->Add(base::MakeUnique<EditorNode>(child_bb_node->GetTitle(),
511 b_node->Add(new_b_node, b_node->child_count()); 513 child_bb_node->id()),
514 b_node->child_count());
512 CreateNodes(child_bb_node, new_b_node); 515 CreateNodes(child_bb_node, new_b_node);
513 } 516 }
514 } 517 }
515 } 518 }
516 519
517 BookmarkEditorView::EditorNode* BookmarkEditorView::FindNodeWithID( 520 BookmarkEditorView::EditorNode* BookmarkEditorView::FindNodeWithID(
518 BookmarkEditorView::EditorNode* node, 521 BookmarkEditorView::EditorNode* node,
519 int64_t id) { 522 int64_t id) {
520 if (node->value == id) 523 if (node->value == id)
521 return node; 524 return node;
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 Profile* profile, 653 Profile* profile,
651 const BookmarkEditor::EditDetails& details, 654 const BookmarkEditor::EditDetails& details,
652 BookmarkEditor::Configuration configuration) { 655 BookmarkEditor::Configuration configuration) {
653 DCHECK(profile); 656 DCHECK(profile);
654 BookmarkEditorView* editor = new BookmarkEditorView( 657 BookmarkEditorView* editor = new BookmarkEditorView(
655 profile, details.parent_node, details, configuration); 658 profile, details.parent_node, details, configuration);
656 editor->Show(parent_window); 659 editor->Show(parent_window);
657 } 660 }
658 661
659 } // namespace chrome 662 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/bookmarks/bookmark_editor_view.h ('k') | components/bookmarks/browser/bookmark_codec.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698