1 // Copyright (c) 2012 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 #include "chrome/browser/ui/cocoa/content_settings/cookie_details.h"
7 #import "base/i18n/time_formatting.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/browsing_data/cookies_tree_model.h"
10 #include "grit/generated_resources.h"
11 #include "net/cookies/canonical_cookie.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
13 #include "ui/base/text/bytes_formatting.h"
14 #include "webkit/browser/appcache/appcache_service.h"
16 #pragma mark Cocoa Cookie Details
18 @implementation CocoaCookieDetails
20 @synthesize canEditExpiration = canEditExpiration_;
21 @synthesize hasExpiration = hasExpiration_;
22 @synthesize type = type_;
24 - (BOOL)shouldHideCookieDetailsView {
25 return type_ != kCocoaCookieDetailsTypeFolder &&
26 type_ != kCocoaCookieDetailsTypeCookie;
29 - (BOOL)shouldShowLocalStorageTreeDetailsView {
30 return type_ == kCocoaCookieDetailsTypeTreeLocalStorage;
33 - (BOOL)shouldShowLocalStoragePromptDetailsView {
34 return type_ == kCocoaCookieDetailsTypePromptLocalStorage;
37 - (BOOL)shouldShowDatabaseTreeDetailsView {
38 return type_ == kCocoaCookieDetailsTypeTreeDatabase;
41 - (BOOL)shouldShowAppCacheTreeDetailsView {
42 return type_ == kCocoaCookieDetailsTypeTreeAppCache;
45 - (BOOL)shouldShowDatabasePromptDetailsView {
46 return type_ == kCocoaCookieDetailsTypePromptDatabase;
49 - (BOOL)shouldShowAppCachePromptDetailsView {
50 return type_ == kCocoaCookieDetailsTypePromptAppCache;
53 - (BOOL)shouldShowIndexedDBTreeDetailsView {
54 return type_ == kCocoaCookieDetailsTypeTreeIndexedDB;
61 - (NSString*)content {
62 return content_.get();
73 - (NSString*)sendFor {
74 return sendFor_.get();
77 - (NSString*)created {
78 return created_.get();
81 - (NSString*)expires {
82 return expires_.get();
85 - (NSString*)fileSize {
86 return fileSize_.get();
89 - (NSString*)lastModified {
90 return lastModified_.get();
93 - (NSString*)lastAccessed {
94 return lastAccessed_.get();
97 - (NSString*)databaseDescription {
98 return databaseDescription_.get();
101 - (NSString*)localStorageKey {
102 return localStorageKey_.get();
105 - (NSString*)localStorageValue {
106 return localStorageValue_.get();
109 - (NSString*)manifestURL {
110 return manifestURL_.get();
114 if ((self = [super init])) {
115 type_ = kCocoaCookieDetailsTypeFolder;
120 - (id)initWithCookie:(const net::CanonicalCookie*)cookie
121 canEditExpiration:(BOOL)canEditExpiration {
122 if ((self = [super init])) {
123 type_ = kCocoaCookieDetailsTypeCookie;
124 hasExpiration_ = cookie->IsPersistent();
125 canEditExpiration_ = canEditExpiration && hasExpiration_;
126 name_.reset([base::SysUTF8ToNSString(cookie->Name()) retain]);
127 content_.reset([base::SysUTF8ToNSString(cookie->Value()) retain]);
128 path_.reset([base::SysUTF8ToNSString(cookie->Path()) retain]);
129 domain_.reset([base::SysUTF8ToNSString(cookie->Domain()) retain]);
131 if (cookie->IsPersistent()) {
132 expires_.reset([base::SysUTF16ToNSString(
133 base::TimeFormatFriendlyDateAndTime(cookie->ExpiryDate())) retain]);
135 expires_.reset([l10n_util::GetNSStringWithFixup(
136 IDS_COOKIES_COOKIE_EXPIRES_SESSION) retain]);
139 created_.reset([base::SysUTF16ToNSString(
140 base::TimeFormatFriendlyDateAndTime(cookie->CreationDate())) retain]);
142 if (cookie->IsSecure()) {
143 sendFor_.reset([l10n_util::GetNSStringWithFixup(
144 IDS_COOKIES_COOKIE_SENDFOR_SECURE) retain]);
146 sendFor_.reset([l10n_util::GetNSStringWithFixup(
147 IDS_COOKIES_COOKIE_SENDFOR_ANY) retain]);
153 - (id)initWithDatabase:(const BrowsingDataDatabaseHelper::DatabaseInfo*)
155 if ((self = [super init])) {
156 type_ = kCocoaCookieDetailsTypeTreeDatabase;
157 canEditExpiration_ = NO;
158 databaseDescription_.reset([base::SysUTF8ToNSString(
159 databaseInfo->description) retain]);
160 fileSize_.reset([base::SysUTF16ToNSString(
161 ui::FormatBytes(databaseInfo->size)) retain]);
162 lastModified_.reset([base::SysUTF16ToNSString(
163 base::TimeFormatFriendlyDateAndTime(
164 databaseInfo->last_modified)) retain]);
169 - (id)initWithLocalStorage:(
170 const BrowsingDataLocalStorageHelper::LocalStorageInfo*)storageInfo {
171 if ((self = [super init])) {
172 type_ = kCocoaCookieDetailsTypeTreeLocalStorage;
173 canEditExpiration_ = NO;
175 [base::SysUTF8ToNSString(storageInfo->origin_url.spec()) retain]);
177 [base::SysUTF16ToNSString(ui::FormatBytes(storageInfo->size)) retain]);
178 lastModified_.reset([base::SysUTF16ToNSString(
179 base::TimeFormatFriendlyDateAndTime(
180 storageInfo->last_modified)) retain]);
185 - (id)initWithAppCacheInfo:(const appcache::AppCacheInfo*)appcacheInfo {
186 if ((self = [super init])) {
187 type_ = kCocoaCookieDetailsTypeTreeAppCache;
188 canEditExpiration_ = NO;
189 manifestURL_.reset([base::SysUTF8ToNSString(
190 appcacheInfo->manifest_url.spec()) retain]);
191 fileSize_.reset([base::SysUTF16ToNSString(
192 ui::FormatBytes(appcacheInfo->size)) retain]);
193 created_.reset([base::SysUTF16ToNSString(
194 base::TimeFormatFriendlyDateAndTime(
195 appcacheInfo->creation_time)) retain]);
196 lastAccessed_.reset([base::SysUTF16ToNSString(
197 base::TimeFormatFriendlyDateAndTime(
198 appcacheInfo->last_access_time)) retain]);
203 - (id)initWithDatabase:(const std::string&)domain
204 databaseName:(const base::string16&)databaseName
205 databaseDescription:(const base::string16&)databaseDescription
206 fileSize:(unsigned long)fileSize {
207 if ((self = [super init])) {
208 type_ = kCocoaCookieDetailsTypePromptDatabase;
209 canEditExpiration_ = NO;
210 name_.reset([base::SysUTF16ToNSString(databaseName) retain]);
211 domain_.reset([base::SysUTF8ToNSString(domain) retain]);
212 databaseDescription_.reset(
213 [base::SysUTF16ToNSString(databaseDescription) retain]);
215 [base::SysUTF16ToNSString(ui::FormatBytes(fileSize)) retain]);
220 - (id)initWithLocalStorage:(const std::string&)domain
221 key:(const base::string16&)key
222 value:(const base::string16&)value {
223 if ((self = [super init])) {
224 type_ = kCocoaCookieDetailsTypePromptLocalStorage;
225 canEditExpiration_ = NO;
226 domain_.reset([base::SysUTF8ToNSString(domain) retain]);
227 localStorageKey_.reset([base::SysUTF16ToNSString(key) retain]);
228 localStorageValue_.reset([base::SysUTF16ToNSString(value) retain]);
233 - (id)initWithAppCacheManifestURL:(const std::string&)manifestURL {
234 if ((self = [super init])) {
235 type_ = kCocoaCookieDetailsTypePromptAppCache;
236 canEditExpiration_ = NO;
237 manifestURL_.reset([base::SysUTF8ToNSString(manifestURL) retain]);
242 - (id)initWithIndexedDBInfo:
243 (const content::IndexedDBInfo*)indexedDBInfo {
244 if ((self = [super init])) {
245 type_ = kCocoaCookieDetailsTypeTreeIndexedDB;
246 canEditExpiration_ = NO;
247 domain_.reset([base::SysUTF8ToNSString(
248 indexedDBInfo->origin_.spec()) retain]);
249 fileSize_.reset([base::SysUTF16ToNSString(
250 ui::FormatBytes(indexedDBInfo->size_)) retain]);
251 lastModified_.reset([base::SysUTF16ToNSString(
252 base::TimeFormatFriendlyDateAndTime(
253 indexedDBInfo->last_modified_)) retain]);
258 + (CocoaCookieDetails*)createFromCookieTreeNode:(CookieTreeNode*)treeNode {
259 CookieTreeNode::DetailedInfo info = treeNode->GetDetailedInfo();
260 CookieTreeNode::DetailedInfo::NodeType nodeType = info.node_type;
262 case CookieTreeNode::DetailedInfo::TYPE_COOKIE:
263 return [[[CocoaCookieDetails alloc] initWithCookie:info.cookie
264 canEditExpiration:NO] autorelease];
265 case CookieTreeNode::DetailedInfo::TYPE_DATABASE:
266 return [[[CocoaCookieDetails alloc]
267 initWithDatabase:info.database_info] autorelease];
268 case CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE:
269 return [[[CocoaCookieDetails alloc]
270 initWithLocalStorage:info.local_storage_info] autorelease];
271 case CookieTreeNode::DetailedInfo::TYPE_APPCACHE:
272 return [[[CocoaCookieDetails alloc]
273 initWithAppCacheInfo:info.appcache_info] autorelease];
274 case CookieTreeNode::DetailedInfo::TYPE_INDEXED_DB:
275 return [[[CocoaCookieDetails alloc]
276 initWithIndexedDBInfo:info.indexed_db_info] autorelease];
278 return [[[CocoaCookieDetails alloc] initAsFolder] autorelease];
284 #pragma mark Content Object Adapter
286 @implementation CookiePromptContentDetailsAdapter
288 - (id)initWithDetails:(CocoaCookieDetails*)details {
289 if ((self = [super init])) {
290 details_.reset([details retain]);
295 - (CocoaCookieDetails*)details {
296 return details_.get();