1// Copyright (c) 2011 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/cookie_tree_node.h" 6 7#include "base/sys_string_conversions.h" 8 9@implementation CocoaCookieTreeNode 10 11- (id)initWithNode:(CookieTreeNode*)node { 12 if ((self = [super init])) { 13 DCHECK(node); 14 treeNode_ = node; 15 [self rebuild]; 16 } 17 return self; 18} 19 20- (void)rebuild { 21 title_.reset([base::SysUTF16ToNSString(treeNode_->GetTitle()) retain]); 22 children_.reset(); 23 // The tree node assumes ownership of the cookie details object 24 details_.reset([[CocoaCookieDetails createFromCookieTreeNode:(treeNode_)] 25 retain]); 26} 27 28- (NSString*)title { 29 return title_.get(); 30} 31 32- (CocoaCookieDetailsType)nodeType { 33 return [details_.get() type]; 34} 35 36- (ui::TreeModelNode*)treeNode { 37 return treeNode_; 38} 39 40- (NSMutableArray*)mutableChildren { 41 if (!children_.get()) { 42 const int childCount = treeNode_->child_count(); 43 children_.reset([[NSMutableArray alloc] initWithCapacity:childCount]); 44 for (int i = 0; i < childCount; ++i) { 45 CookieTreeNode* child = treeNode_->GetChild(i); 46 scoped_nsobject<CocoaCookieTreeNode> childNode( 47 [[CocoaCookieTreeNode alloc] initWithNode:child]); 48 [children_ addObject:childNode.get()]; 49 } 50 } 51 return children_.get(); 52} 53 54- (NSArray*)children { 55 return [self mutableChildren]; 56} 57 58- (BOOL)isLeaf { 59 return [self nodeType] != kCocoaCookieDetailsTypeFolder; 60}; 61 62- (NSString*)description { 63 NSString* format = 64 @"<CocoaCookieTreeNode @ %p (title=%@, nodeType=%d, childCount=%u)"; 65 return [NSString stringWithFormat:format, self, [self title], 66 [self nodeType], [[self children] count]]; 67} 68 69- (CocoaCookieDetails*)details { 70 return details_; 71} 72 73@end 74