1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/content_settings/cookies_tree_controller_bridge.h"
7 CookiesTreeControllerBridge::CookiesTreeControllerBridge(
8 CookiesTreeModel* model)
10 cocoa_model_([CocoaNodeFromTreeNode(model_->GetRoot()) retain]) {
11 model_->AddObserver(this);
14 CookiesTreeControllerBridge::~CookiesTreeControllerBridge() {
15 model_->RemoveObserver(this);
18 // Notification that nodes were added to the specified parent.
19 void CookiesTreeControllerBridge::TreeNodesAdded(ui::TreeModel* model,
20 ui::TreeModelNode* parent,
23 CocoaCookieTreeNode* cocoa_parent = FindCocoaNode(parent, nil);
24 NSMutableArray* cocoa_children = [cocoa_parent mutableChildren];
26 [cocoa_model_ willChangeValueForKey:@"children"];
27 CookieTreeNode* cookie_parent = static_cast<CookieTreeNode*>(parent);
28 for (int i = 0; i < count; ++i) {
29 CookieTreeNode* cookie_child = cookie_parent->GetChild(start + i);
30 CocoaCookieTreeNode* new_child = CocoaNodeFromTreeNode(cookie_child);
31 [cocoa_children addObject:new_child];
33 [cocoa_model_ didChangeValueForKey:@"children"];
36 // Notification that nodes were removed from the specified parent.
37 void CookiesTreeControllerBridge::TreeNodesRemoved(ui::TreeModel* model,
38 ui::TreeModelNode* parent,
41 CocoaCookieTreeNode* cocoa_parent = FindCocoaNode(parent, nil);
42 NSMutableArray* cocoa_children = [cocoa_parent mutableChildren];
43 [cocoa_model_ willChangeValueForKey:@"children"];
44 for (int i = start + count - 1; i >= start; --i) {
45 [cocoa_children removeObjectAtIndex:i];
47 [cocoa_model_ didChangeValueForKey:@"children"];
50 // Notification that the contents of a node has changed.
51 void CookiesTreeControllerBridge::TreeNodeChanged(ui::TreeModel* model,
52 ui::TreeModelNode* node) {
53 [cocoa_model_ willChangeValueForKey:@"children"];
54 CocoaCookieTreeNode* changed_node = FindCocoaNode(node, nil);
55 [changed_node rebuild];
56 [cocoa_model_ didChangeValueForKey:@"children"];
59 CocoaCookieTreeNode* CookiesTreeControllerBridge::CocoaNodeFromTreeNode(
60 ui::TreeModelNode* node) {
61 CookieTreeNode* cookie_node = static_cast<CookieTreeNode*>(node);
62 return [[[CocoaCookieTreeNode alloc] initWithNode:cookie_node] autorelease];
65 // Does breadth-first search on the tree to find |node|. This method is most
66 // commonly used to find origin/folder nodes, which are at the first level off
67 // the root (hence breadth-first search).
68 CocoaCookieTreeNode* CookiesTreeControllerBridge::FindCocoaNode(
69 ui::TreeModelNode* target, CocoaCookieTreeNode* start) {
71 start = cocoa_model_.get();
73 if ([start treeNode] == target) {
77 // Enqueue the root node of the search (sub-)tree.
78 std::queue<CocoaCookieTreeNode*> horizon;
81 // Loop until we've looked at every node or we found the target.
82 while (!horizon.empty()) {
83 // Dequeue the item at the front.
84 CocoaCookieTreeNode* node = horizon.front();
87 // If this is the target node, report it up.
88 if ([node treeNode] == target)
91 // Add all child nodes to the queue for searching.
93 NSArray* children = [node children];
94 for (CocoaCookieTreeNode* child in children) {
100 return nil; // The node could not be found.