summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/cookie_tree_node.mm
blob: 53b7bab059c9a5bd15df9f5a50c3576c12506e25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/cocoa/cookie_tree_node.h"

#include "app/l10n_util_mac.h"
#import "base/i18n/time_formatting.h"
#include "base/sys_string_conversions.h"
#include "grit/generated_resources.h"

@implementation CocoaCookieTreeNode

- (id)initWithNode:(CookieTreeNode*)node {
  if ((self = [super init])) {
    DCHECK(node);
    treeNode_ = node;
    isLeaf_ = (node->GetChildCount() == 0);

    [self rebuild];
  }
  return self;
}

- (void)rebuild {
  title_.reset([base::SysWideToNSString(treeNode_->GetTitle()) retain]);
  isCookie_ = NO;

  CookieTreeNode::DetailedInfo info = treeNode_->GetDetailedInfo();
  if (info.node_type == CookieTreeNode::DetailedInfo::TYPE_COOKIE) {
    isCookie_ = YES;
    net::CookieMonster::CanonicalCookie cookie = info.cookie->second;

    name_.reset([base::SysUTF8ToNSString(cookie.Name()) retain]);
    title_.reset([base::SysUTF8ToNSString(cookie.Name()) retain]);
    content_.reset([base::SysUTF8ToNSString(cookie.Value()) retain]);
    path_.reset([base::SysUTF8ToNSString(cookie.Path()) retain]);
    domain_.reset([base::SysWideToNSString(info.origin) retain]);

    if (cookie.DoesExpire()) {
      expires_.reset([base::SysWideToNSString(
          base::TimeFormatFriendlyDateAndTime(cookie.ExpiryDate())) retain]);
    } else {
      expires_.reset([l10n_util::GetNSStringWithFixup(
          IDS_COOKIES_COOKIE_EXPIRES_SESSION) retain]);
    }

    created_.reset([base::SysWideToNSString(
        base::TimeFormatFriendlyDateAndTime(cookie.CreationDate())) retain]);

    if (cookie.IsSecure()) {
      sendFor_.reset([l10n_util::GetNSStringWithFixup(
          IDS_COOKIES_COOKIE_SENDFOR_SECURE) retain]);
    } else {
      sendFor_.reset([l10n_util::GetNSStringWithFixup(
          IDS_COOKIES_COOKIE_SENDFOR_ANY) retain]);
    }
  }
}

- (BOOL)isLeaf {
  return isLeaf_;
}

- (NSString*)title {
  return title_.get();
}

- (NSMutableArray*)children {
  if (!children_.get()) {
    const int childCount = treeNode_->GetChildCount();
    children_.reset([[NSMutableArray alloc] initWithCapacity:childCount]);
    for (int i = 0; i < childCount; ++i) {
      CookieTreeNode* child = treeNode_->GetChild(i);
      scoped_nsobject<CocoaCookieTreeNode> childNode(
          [[CocoaCookieTreeNode alloc] initWithNode:child]);
      [children_ addObject:childNode.get()];
    }
  }
  return children_.get();
}

- (TreeModelNode*)treeNode {
  return treeNode_;
}

#pragma mark Cookie Accessors

- (BOOL)isCookie {
  return isCookie_;
}

- (NSString*)name {
  return name_.get();
}

- (NSString*)content {
  return content_.get();
}

- (NSString*)domain {
  return domain_.get();
}

- (NSString*)path {
  return path_.get();
}

- (NSString*)sendFor {
  return sendFor_.get();
}

- (NSString*)created {
  return created_.get();
}

- (NSString*)expires {
  return expires_.get();
}

@end