OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/content_settings/cookies_tree_controller_bridge
.h" |
| 6 |
| 7 CookiesTreeControllerBridge::CookiesTreeControllerBridge( |
| 8 CookiesTreeModel* model) |
| 9 : model_(model), |
| 10 cocoa_model_([CocoaNodeFromTreeNode(model_->GetRoot()) retain]) { |
| 11 model_->AddObserver(this); |
| 12 } |
| 13 |
| 14 CookiesTreeControllerBridge::~CookiesTreeControllerBridge() { |
| 15 model_->RemoveObserver(this); |
| 16 } |
| 17 |
| 18 // Notification that nodes were added to the specified parent. |
| 19 void CookiesTreeControllerBridge::TreeNodesAdded(ui::TreeModel* model, |
| 20 ui::TreeModelNode* parent, |
| 21 int start, |
| 22 int count) { |
| 23 CocoaCookieTreeNode* cocoa_parent = FindCocoaNode(parent, nil); |
| 24 NSMutableArray* cocoa_children = [cocoa_parent mutableChildren]; |
| 25 |
| 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]; |
| 32 } |
| 33 [cocoa_model_ didChangeValueForKey:@"children"]; |
| 34 } |
| 35 |
| 36 // Notification that nodes were removed from the specified parent. |
| 37 void CookiesTreeControllerBridge::TreeNodesRemoved(ui::TreeModel* model, |
| 38 ui::TreeModelNode* parent, |
| 39 int start, |
| 40 int count) { |
| 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]; |
| 46 } |
| 47 [cocoa_model_ didChangeValueForKey:@"children"]; |
| 48 } |
| 49 |
| 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"]; |
| 57 } |
| 58 |
| 59 CocoaCookieTreeNode* CookiesTreeControllerBridge::CocoaNodeFromTreeNode( |
| 60 ui::TreeModelNode* node) { |
| 61 CookieTreeNode* cookie_node = static_cast<CookieTreeNode*>(node); |
| 62 return [[[CocoaCookieTreeNode alloc] initWithNode:cookie_node] autorelease]; |
| 63 } |
| 64 |
| 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) { |
| 70 if (!start) { |
| 71 start = cocoa_model_.get(); |
| 72 } |
| 73 if ([start treeNode] == target) { |
| 74 return start; |
| 75 } |
| 76 |
| 77 // Enqueue the root node of the search (sub-)tree. |
| 78 std::queue<CocoaCookieTreeNode*> horizon; |
| 79 horizon.push(start); |
| 80 |
| 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(); |
| 85 horizon.pop(); |
| 86 |
| 87 // If this is the target node, report it up. |
| 88 if ([node treeNode] == target) |
| 89 return node; |
| 90 |
| 91 // Add all child nodes to the queue for searching. |
| 92 if (![node isLeaf]) { |
| 93 NSArray* children = [node children]; |
| 94 for (CocoaCookieTreeNode* child in children) { |
| 95 horizon.push(child); |
| 96 } |
| 97 } |
| 98 } |
| 99 |
| 100 return nil; // The node could not be found. |
| 101 } |
OLD | NEW |