1 // Copyright (c) 2010 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 #include "chrome/browser/ui/cocoa/content_settings/cookie_details.h" 6 7 #import "base/i18n/time_formatting.h" 8 #include "base/sys_string_conversions.h" 9 #include "grit/generated_resources.h" 10 #include "chrome/browser/cookies_tree_model.h" 11 #include "ui/base/l10n/l10n_util_mac.h" 12 #include "webkit/appcache/appcache_service.h" 13 14 #pragma mark Cocoa Cookie Details 15 16 @implementation CocoaCookieDetails 17 18 @synthesize canEditExpiration = canEditExpiration_; 19 @synthesize hasExpiration = hasExpiration_; 20 @synthesize type = type_; 21 22 - (BOOL)shouldHideCookieDetailsView { 23 return type_ != kCocoaCookieDetailsTypeFolder && 24 type_ != kCocoaCookieDetailsTypeCookie; 25 } 26 27 - (BOOL)shouldShowLocalStorageTreeDetailsView { 28 return type_ == kCocoaCookieDetailsTypeTreeLocalStorage; 29 } 30 31 - (BOOL)shouldShowLocalStoragePromptDetailsView { 32 return type_ == kCocoaCookieDetailsTypePromptLocalStorage; 33 } 34 35 - (BOOL)shouldShowDatabaseTreeDetailsView { 36 return type_ == kCocoaCookieDetailsTypeTreeDatabase; 37 } 38 39 - (BOOL)shouldShowAppCacheTreeDetailsView { 40 return type_ == kCocoaCookieDetailsTypeTreeAppCache; 41 } 42 43 - (BOOL)shouldShowDatabasePromptDetailsView { 44 return type_ == kCocoaCookieDetailsTypePromptDatabase; 45 } 46 47 - (BOOL)shouldShowAppCachePromptDetailsView { 48 return type_ == kCocoaCookieDetailsTypePromptAppCache; 49 } 50 51 - (BOOL)shouldShowIndexedDBTreeDetailsView { 52 return type_ == kCocoaCookieDetailsTypeTreeIndexedDB; 53 } 54 55 - (NSString*)name { 56 return name_.get(); 57 } 58 59 - (NSString*)content { 60 return content_.get(); 61 } 62 63 - (NSString*)domain { 64 return domain_.get(); 65 } 66 67 - (NSString*)path { 68 return path_.get(); 69 } 70 71 - (NSString*)sendFor { 72 return sendFor_.get(); 73 } 74 75 - (NSString*)created { 76 return created_.get(); 77 } 78 79 - (NSString*)expires { 80 return expires_.get(); 81 } 82 83 - (NSString*)fileSize { 84 return fileSize_.get(); 85 } 86 87 - (NSString*)lastModified { 88 return lastModified_.get(); 89 } 90 91 - (NSString*)lastAccessed { 92 return lastAccessed_.get(); 93 } 94 95 - (NSString*)databaseDescription { 96 return databaseDescription_.get(); 97 } 98 99 - (NSString*)localStorageKey { 100 return localStorageKey_.get(); 101 } 102 103 - (NSString*)localStorageValue { 104 return localStorageValue_.get(); 105 } 106 107 - (NSString*)manifestURL { 108 return manifestURL_.get(); 109 } 110 111 - (id)initAsFolder { 112 if ((self = [super init])) { 113 type_ = kCocoaCookieDetailsTypeFolder; 114 } 115 return self; 116 } 117 118 - (id)initWithCookie:(const net::CookieMonster::CanonicalCookie*)cookie 119 origin:(NSString*)origin 120 canEditExpiration:(BOOL)canEditExpiration { 121 if ((self = [super init])) { 122 type_ = kCocoaCookieDetailsTypeCookie; 123 hasExpiration_ = cookie->DoesExpire(); 124 canEditExpiration_ = canEditExpiration && hasExpiration_; 125 name_.reset([base::SysUTF8ToNSString(cookie->Name()) retain]); 126 content_.reset([base::SysUTF8ToNSString(cookie->Value()) retain]); 127 path_.reset([base::SysUTF8ToNSString(cookie->Path()) retain]); 128 domain_.reset([origin retain]); 129 130 if (cookie->DoesExpire()) { 131 expires_.reset([base::SysUTF16ToNSString( 132 base::TimeFormatFriendlyDateAndTime(cookie->ExpiryDate())) retain]); 133 } else { 134 expires_.reset([l10n_util::GetNSStringWithFixup( 135 IDS_COOKIES_COOKIE_EXPIRES_SESSION) retain]); 136 } 137 138 created_.reset([base::SysUTF16ToNSString( 139 base::TimeFormatFriendlyDateAndTime(cookie->CreationDate())) retain]); 140 141 if (cookie->IsSecure()) { 142 sendFor_.reset([l10n_util::GetNSStringWithFixup( 143 IDS_COOKIES_COOKIE_SENDFOR_SECURE) retain]); 144 } else { 145 sendFor_.reset([l10n_util::GetNSStringWithFixup( 146 IDS_COOKIES_COOKIE_SENDFOR_ANY) retain]); 147 } 148 } 149 return self; 150 } 151 152 - (id)initWithDatabase:(const BrowsingDataDatabaseHelper::DatabaseInfo*) 153 databaseInfo { 154 if ((self = [super init])) { 155 type_ = kCocoaCookieDetailsTypeTreeDatabase; 156 canEditExpiration_ = NO; 157 databaseDescription_.reset([base::SysUTF8ToNSString( 158 databaseInfo->description) retain]); 159 fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(databaseInfo->size, 160 GetByteDisplayUnits(databaseInfo->size), true)) retain]); 161 lastModified_.reset([base::SysUTF16ToNSString( 162 base::TimeFormatFriendlyDateAndTime( 163 databaseInfo->last_modified)) retain]); 164 } 165 return self; 166 } 167 168 - (id)initWithLocalStorage:( 169 const BrowsingDataLocalStorageHelper::LocalStorageInfo*)storageInfo { 170 if ((self = [super init])) { 171 type_ = kCocoaCookieDetailsTypeTreeLocalStorage; 172 canEditExpiration_ = NO; 173 domain_.reset([base::SysUTF8ToNSString(storageInfo->origin) retain]); 174 fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(storageInfo->size, 175 GetByteDisplayUnits(storageInfo->size), true)) retain]); 176 lastModified_.reset([base::SysUTF16ToNSString( 177 base::TimeFormatFriendlyDateAndTime( 178 storageInfo->last_modified)) retain]); 179 } 180 return self; 181 } 182 183 - (id)initWithAppCacheInfo:(const appcache::AppCacheInfo*)appcacheInfo { 184 if ((self = [super init])) { 185 type_ = kCocoaCookieDetailsTypeTreeAppCache; 186 canEditExpiration_ = NO; 187 manifestURL_.reset([base::SysUTF8ToNSString( 188 appcacheInfo->manifest_url.spec()) retain]); 189 fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(appcacheInfo->size, 190 GetByteDisplayUnits(appcacheInfo->size), true)) retain]); 191 created_.reset([base::SysUTF16ToNSString( 192 base::TimeFormatFriendlyDateAndTime( 193 appcacheInfo->creation_time)) retain]); 194 lastAccessed_.reset([base::SysUTF16ToNSString( 195 base::TimeFormatFriendlyDateAndTime( 196 appcacheInfo->last_access_time)) retain]); 197 } 198 return self; 199 } 200 201 - (id)initWithDatabase:(const std::string&)domain 202 databaseName:(const string16&)databaseName 203 databaseDescription:(const string16&)databaseDescription 204 fileSize:(unsigned long)fileSize { 205 if ((self = [super init])) { 206 type_ = kCocoaCookieDetailsTypePromptDatabase; 207 canEditExpiration_ = NO; 208 name_.reset([base::SysUTF16ToNSString(databaseName) retain]); 209 domain_.reset([base::SysUTF8ToNSString(domain) retain]); 210 databaseDescription_.reset( 211 [base::SysUTF16ToNSString(databaseDescription) retain]); 212 fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(fileSize, 213 GetByteDisplayUnits(fileSize), true)) retain]); 214 } 215 return self; 216 } 217 218 - (id)initWithLocalStorage:(const std::string&)domain 219 key:(const string16&)key 220 value:(const string16&)value { 221 if ((self = [super init])) { 222 type_ = kCocoaCookieDetailsTypePromptLocalStorage; 223 canEditExpiration_ = NO; 224 domain_.reset([base::SysUTF8ToNSString(domain) retain]); 225 localStorageKey_.reset([base::SysUTF16ToNSString(key) retain]); 226 localStorageValue_.reset([base::SysUTF16ToNSString(value) retain]); 227 } 228 return self; 229 } 230 231 - (id)initWithAppCacheManifestURL:(const std::string&)manifestURL { 232 if ((self = [super init])) { 233 type_ = kCocoaCookieDetailsTypePromptAppCache; 234 canEditExpiration_ = NO; 235 manifestURL_.reset([base::SysUTF8ToNSString(manifestURL) retain]); 236 } 237 return self; 238 } 239 240 - (id)initWithIndexedDBInfo: 241 (const BrowsingDataIndexedDBHelper::IndexedDBInfo*)indexedDBInfo { 242 if ((self = [super init])) { 243 type_ = kCocoaCookieDetailsTypeTreeIndexedDB; 244 canEditExpiration_ = NO; 245 domain_.reset([base::SysUTF8ToNSString(indexedDBInfo->origin) retain]); 246 fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(indexedDBInfo->size, 247 GetByteDisplayUnits(indexedDBInfo->size), true)) retain]); 248 lastModified_.reset([base::SysUTF16ToNSString( 249 base::TimeFormatFriendlyDateAndTime( 250 indexedDBInfo->last_modified)) retain]); 251 } 252 return self; 253 } 254 255 + (CocoaCookieDetails*)createFromCookieTreeNode:(CookieTreeNode*)treeNode { 256 CookieTreeNode::DetailedInfo info = treeNode->GetDetailedInfo(); 257 CookieTreeNode::DetailedInfo::NodeType nodeType = info.node_type; 258 NSString* origin; 259 switch (nodeType) { 260 case CookieTreeNode::DetailedInfo::TYPE_COOKIE: 261 origin = base::SysWideToNSString(info.origin.c_str()); 262 return [[[CocoaCookieDetails alloc] initWithCookie:info.cookie 263 origin:origin 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]; 277 default: 278 return [[[CocoaCookieDetails alloc] initAsFolder] autorelease]; 279 } 280 } 281 282 @end 283 284 #pragma mark Content Object Adapter 285 286 @implementation CookiePromptContentDetailsAdapter 287 288 - (id)initWithDetails:(CocoaCookieDetails*)details { 289 if ((self = [super init])) { 290 details_.reset([details retain]); 291 } 292 return self; 293 } 294 295 - (CocoaCookieDetails*)details { 296 return details_.get(); 297 } 298 299 @end 300