summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/cookie_tree_node.mm
blob: 376df82b1d77bfc0bf7af8e823956494db57b13a (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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 "chrome/browser/browsing_data_local_storage_helper.h"
#include "grit/generated_resources.h"

@implementation CocoaCookieTreeNode

- (id)initWithNode:(CookieTreeNode*)node {
  if ((self = [super init])) {
    DCHECK(node);
    treeNode_ = node;
    [self rebuild];
  }
  return self;
}

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

  CookieTreeNode::DetailedInfo info = treeNode_->GetDetailedInfo();
  CookieTreeNode::DetailedInfo::NodeType nodeType = info.node_type;
  if (nodeType == CookieTreeNode::DetailedInfo::TYPE_COOKIE) {
    nodeType_ = kCocoaCookieTreeNodeTypeCookie;
    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]);
    }
  } else if (nodeType == CookieTreeNode::DetailedInfo::TYPE_DATABASE) {
    const BrowsingDataDatabaseHelper::DatabaseInfo* databaseInfo =
        info.database_info;
    nodeType_ = kCocoaCookieTreeNodeTypeDatabaseStorage;
    databaseDescription_.reset([base::SysUTF8ToNSString(
        databaseInfo->description) retain]);
    fileSize_.reset([base::SysWideToNSString(FormatBytes(databaseInfo->size,
        GetByteDisplayUnits(databaseInfo->size), true)) retain]);
    lastModified_.reset([base::SysWideToNSString(
        base::TimeFormatFriendlyDateAndTime(
            databaseInfo->last_modified)) retain]);
  } else if (nodeType == CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE) {
    const BrowsingDataLocalStorageHelper::LocalStorageInfo* storageInfo =
        info.local_storage_info;
    nodeType_ = kCocoaCookieTreeNodeTypeLocalStorage;
    domain_.reset([base::SysUTF8ToNSString(storageInfo->origin) retain]);
    fileSize_.reset([base::SysWideToNSString(FormatBytes(storageInfo->size,
        GetByteDisplayUnits(storageInfo->size), true)) retain]);
    lastModified_.reset([base::SysWideToNSString(
        base::TimeFormatFriendlyDateAndTime(
            storageInfo->last_modified)) retain]);
  }
}

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

- (CocoaCookieTreeNodeType)nodeType {
  return nodeType_;
}

- (TreeModelNode*)treeNode {
  return treeNode_;
}

- (NSMutableArray*)mutableChildren {
  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();
}

- (NSArray*)children {
  return [self mutableChildren];
}

- (BOOL)isLeaf {
  return nodeType_ != kCocoaCookieTreeNodeTypeFolder;
}

- (NSString*)description {
  NSString* format =
      @"<CocoaCookieTreeNode @ %p (title=%@, nodeType=%d, childCount=%u)";
  return [NSString stringWithFormat:format, self, [self title],
      [self nodeType], [[self children] count]];
}

- (BOOL)isFolderOrCookieTreeDetails {
  return [self nodeType] == kCocoaCookieTreeNodeTypeFolder ||
      [self nodeType] == kCocoaCookieTreeNodeTypeCookie;
}

- (BOOL)isDatabaseTreeDetails {
  return [self nodeType] == kCocoaCookieTreeNodeTypeDatabaseStorage;
}

- (BOOL)isLocalStorageTreeDetails {
  return [self nodeType] == kCocoaCookieTreeNodeTypeLocalStorage;
}

- (BOOL)isDatabasePromptDetails {
  return false;
}

- (BOOL)isLocalStoragePromptDetails {
  return false;
}

#pragma mark Cookie Accessors

- (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();
}

#pragma mark Local Storage and Database Accessors

- (NSString*)fileSize {
  return fileSize_.get();
}

- (NSString*)lastModified {
  return lastModified_.get();
}

#pragma mark Database Accessors

- (NSString*)databaseDescription {
  return databaseDescription_.get();
}

#pragma mark Unused Accessors

// This method is never called for the cookie tree, it is only
// only included because the Cocoa bindings for the shared view
// used to display browser data details always expects the method
// even though it is only used in the cookie prompt window.
- (id)localStorageKey {
  NOTIMPLEMENTED();
  return nil;
}

// This method is never called for the cookie tree, it is only
// only included because the Cocoa bindings for the shared view
// used to display browser data details always expects the method
// even though it is only used in the cookie prompt window.
- (id)localStorageValue {
  NOTIMPLEMENTED();
  return nil;
}

@end