Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / content_settings / cookie_details.mm
blob5138cb7567a2ea239559820bd1cb9a84b2e05b8e
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 "chrome/grit/generated_resources.h"
11 #include "content/public/browser/appcache_service.h"
12 #include "net/cookies/canonical_cookie.h"
13 #include "ui/base/l10n/l10n_util_mac.h"
14 #include "ui/base/text/bytes_formatting.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;
57 - (BOOL)shouldShowServiceWorkerTreeDetailsView {
58   return type_ == kCocoaCookieDetailsTypeTreeServiceWorker;
61 - (BOOL)shouldShowCacheStorageTreeDetailsView {
62   return type_ == kCocoaCookieDetailsTypeTreeCacheStorage;
65 - (NSString*)name {
66   return name_.get();
69 - (NSString*)content {
70   return content_.get();
73 - (NSString*)domain {
74   return domain_.get();
77 - (NSString*)path {
78   return path_.get();
81 - (NSString*)sendFor {
82   return sendFor_.get();
85 - (NSString*)created {
86   return created_.get();
89 - (NSString*)expires {
90   return expires_.get();
93 - (NSString*)fileSize {
94   return fileSize_.get();
97 - (NSString*)lastModified {
98   return lastModified_.get();
101 - (NSString*)lastAccessed {
102   return lastAccessed_.get();
105 - (NSString*)databaseDescription {
106   return databaseDescription_.get();
109 - (NSString*)localStorageKey {
110   return localStorageKey_.get();
113 - (NSString*)localStorageValue {
114   return localStorageValue_.get();
117 - (NSString*)manifestURL {
118   return manifestURL_.get();
121 - (NSString*)scopes {
122   return scopes_.get();
125 - (id)initAsFolder {
126   if ((self = [super init])) {
127     type_ = kCocoaCookieDetailsTypeFolder;
128   }
129   return self;
132 - (id)initWithCookie:(const net::CanonicalCookie*)cookie
133    canEditExpiration:(BOOL)canEditExpiration {
134   if ((self = [super init])) {
135     type_ = kCocoaCookieDetailsTypeCookie;
136     hasExpiration_ = cookie->IsPersistent();
137     canEditExpiration_ = canEditExpiration && hasExpiration_;
138     name_.reset([base::SysUTF8ToNSString(cookie->Name()) retain]);
139     content_.reset([base::SysUTF8ToNSString(cookie->Value()) retain]);
140     path_.reset([base::SysUTF8ToNSString(cookie->Path()) retain]);
141     domain_.reset([base::SysUTF8ToNSString(cookie->Domain()) retain]);
143     if (cookie->IsPersistent()) {
144       expires_.reset([base::SysUTF16ToNSString(
145           base::TimeFormatFriendlyDateAndTime(cookie->ExpiryDate())) retain]);
146     } else {
147       expires_.reset([l10n_util::GetNSStringWithFixup(
148           IDS_COOKIES_COOKIE_EXPIRES_SESSION) retain]);
149     }
151     created_.reset([base::SysUTF16ToNSString(
152         base::TimeFormatFriendlyDateAndTime(cookie->CreationDate())) retain]);
154     sendFor_.reset([l10n_util::GetNSStringWithFixup(
155         CookiesTreeModel::GetSendForMessageID(*cookie)) retain]);
156   }
157   return self;
160 - (id)initWithDatabase:(const BrowsingDataDatabaseHelper::DatabaseInfo*)
161     databaseInfo {
162   if ((self = [super init])) {
163     type_ = kCocoaCookieDetailsTypeTreeDatabase;
164     canEditExpiration_ = NO;
165     databaseDescription_.reset([base::SysUTF8ToNSString(
166         databaseInfo->description) retain]);
167     fileSize_.reset([base::SysUTF16ToNSString(
168         ui::FormatBytes(databaseInfo->size)) retain]);
169     lastModified_.reset([base::SysUTF16ToNSString(
170         base::TimeFormatFriendlyDateAndTime(
171             databaseInfo->last_modified)) retain]);
172   }
173   return self;
176 - (id)initWithLocalStorage:(
177     const BrowsingDataLocalStorageHelper::LocalStorageInfo*)storageInfo {
178   if ((self = [super init])) {
179     type_ = kCocoaCookieDetailsTypeTreeLocalStorage;
180     canEditExpiration_ = NO;
181     domain_.reset(
182         [base::SysUTF8ToNSString(storageInfo->origin_url.spec()) retain]);
183     fileSize_.reset(
184         [base::SysUTF16ToNSString(ui::FormatBytes(storageInfo->size)) retain]);
185     lastModified_.reset([base::SysUTF16ToNSString(
186         base::TimeFormatFriendlyDateAndTime(
187             storageInfo->last_modified)) retain]);
188   }
189   return self;
192 - (id)initWithAppCacheInfo:(const content::AppCacheInfo*)appcacheInfo {
193   if ((self = [super init])) {
194     type_ = kCocoaCookieDetailsTypeTreeAppCache;
195     canEditExpiration_ = NO;
196     manifestURL_.reset([base::SysUTF8ToNSString(
197         appcacheInfo->manifest_url.spec()) retain]);
198     fileSize_.reset([base::SysUTF16ToNSString(
199         ui::FormatBytes(appcacheInfo->size)) retain]);
200     created_.reset([base::SysUTF16ToNSString(
201         base::TimeFormatFriendlyDateAndTime(
202             appcacheInfo->creation_time)) retain]);
203     lastAccessed_.reset([base::SysUTF16ToNSString(
204         base::TimeFormatFriendlyDateAndTime(
205             appcacheInfo->last_access_time)) retain]);
206   }
207   return self;
210 - (id)initWithDatabase:(const std::string&)domain
211           databaseName:(const base::string16&)databaseName
212    databaseDescription:(const base::string16&)databaseDescription
213               fileSize:(unsigned long)fileSize {
214   if ((self = [super init])) {
215     type_ = kCocoaCookieDetailsTypePromptDatabase;
216     canEditExpiration_ = NO;
217     name_.reset([base::SysUTF16ToNSString(databaseName) retain]);
218     domain_.reset([base::SysUTF8ToNSString(domain) retain]);
219     databaseDescription_.reset(
220         [base::SysUTF16ToNSString(databaseDescription) retain]);
221     fileSize_.reset(
222         [base::SysUTF16ToNSString(ui::FormatBytes(fileSize)) retain]);
223   }
224   return self;
227 - (id)initWithLocalStorage:(const std::string&)domain
228                        key:(const base::string16&)key
229                      value:(const base::string16&)value {
230   if ((self = [super init])) {
231     type_ = kCocoaCookieDetailsTypePromptLocalStorage;
232     canEditExpiration_ = NO;
233     domain_.reset([base::SysUTF8ToNSString(domain) retain]);
234     localStorageKey_.reset([base::SysUTF16ToNSString(key) retain]);
235     localStorageValue_.reset([base::SysUTF16ToNSString(value) retain]);
236   }
237   return self;
240 - (id)initWithAppCacheManifestURL:(const std::string&)manifestURL {
241   if ((self = [super init])) {
242     type_ = kCocoaCookieDetailsTypePromptAppCache;
243     canEditExpiration_ = NO;
244     manifestURL_.reset([base::SysUTF8ToNSString(manifestURL) retain]);
245   }
246   return self;
249 - (id)initWithIndexedDBInfo:
250     (const content::IndexedDBInfo*)indexedDBInfo {
251   if ((self = [super init])) {
252     type_ = kCocoaCookieDetailsTypeTreeIndexedDB;
253     canEditExpiration_ = NO;
254     domain_.reset(
255         [base::SysUTF8ToNSString(indexedDBInfo->origin.spec()) retain]);
256     fileSize_.reset([base::SysUTF16ToNSString(
257         ui::FormatBytes(indexedDBInfo->size)) retain]);
258     lastModified_.reset([base::SysUTF16ToNSString(
259         base::TimeFormatFriendlyDateAndTime(indexedDBInfo->last_modified))
260         retain]);
261   }
262   return self;
265 - (id)initWithServiceWorkerUsageInfo:
266     (const content::ServiceWorkerUsageInfo*)serviceWorkerInfo {
267   if ((self = [super init])) {
268     type_ = kCocoaCookieDetailsTypeTreeServiceWorker;
269     canEditExpiration_ = NO;
270     domain_.reset([base::SysUTF8ToNSString(
271         serviceWorkerInfo->origin.spec()) retain]);
272     fileSize_.reset([base::SysUTF16ToNSString(
273         ui::FormatBytes(serviceWorkerInfo->total_size_bytes)) retain]);
275     NSMutableArray *scopes = [[NSMutableArray alloc]
276                          initWithCapacity:serviceWorkerInfo->scopes.size()];
277     for (std::vector<GURL>::const_iterator it =
278              serviceWorkerInfo->scopes.begin();
279          it != serviceWorkerInfo->scopes.end(); ++it) {
280       [scopes addObject:base::SysUTF8ToNSString(it->spec())];
281     }
282     scopes_.reset([[scopes componentsJoinedByString:@","] retain]);
283   }
284   return self;
287 - (id)initWithCacheStorageUsageInfo:
288     (const content::CacheStorageUsageInfo*)cacheStorageInfo {
289   if ((self = [super init])) {
290     type_ = kCocoaCookieDetailsTypeTreeCacheStorage;
291     canEditExpiration_ = NO;
292     domain_.reset(
293         [base::SysUTF8ToNSString(cacheStorageInfo->origin.spec()) retain]);
294     fileSize_.reset([base::SysUTF16ToNSString(
295         ui::FormatBytes(cacheStorageInfo->total_size_bytes)) retain]);
296     lastModified_.reset([base::SysUTF16ToNSString(
297         base::TimeFormatFriendlyDateAndTime(cacheStorageInfo->last_modified))
298         retain]);
299   }
300   return self;
303 + (CocoaCookieDetails*)createFromCookieTreeNode:(CookieTreeNode*)treeNode {
304   CookieTreeNode::DetailedInfo info = treeNode->GetDetailedInfo();
305   CookieTreeNode::DetailedInfo::NodeType nodeType = info.node_type;
306   switch (nodeType) {
307     case CookieTreeNode::DetailedInfo::TYPE_COOKIE:
308       return [[[CocoaCookieDetails alloc] initWithCookie:info.cookie
309                                        canEditExpiration:NO] autorelease];
310     case CookieTreeNode::DetailedInfo::TYPE_DATABASE:
311       return [[[CocoaCookieDetails alloc]
312           initWithDatabase:info.database_info] autorelease];
313     case CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE:
314       return [[[CocoaCookieDetails alloc]
315           initWithLocalStorage:info.local_storage_info] autorelease];
316     case CookieTreeNode::DetailedInfo::TYPE_APPCACHE:
317       return [[[CocoaCookieDetails alloc]
318           initWithAppCacheInfo:info.appcache_info] autorelease];
319     case CookieTreeNode::DetailedInfo::TYPE_INDEXED_DB:
320       return [[[CocoaCookieDetails alloc]
321           initWithIndexedDBInfo:info.indexed_db_info] autorelease];
322     case CookieTreeNode::DetailedInfo::TYPE_SERVICE_WORKER:
323       return [[[CocoaCookieDetails alloc]
324           initWithServiceWorkerUsageInfo:info.service_worker_info] autorelease];
325     case CookieTreeNode::DetailedInfo::TYPE_CACHE_STORAGE:
326       return [[[CocoaCookieDetails alloc]
327           initWithCacheStorageUsageInfo:info.cache_storage_info] autorelease];
328     default:
329       return [[[CocoaCookieDetails alloc] initAsFolder] autorelease];
330   }
333 @end
335 #pragma mark Content Object Adapter
337 @implementation CookiePromptContentDetailsAdapter
339 - (id)initWithDetails:(CocoaCookieDetails*)details {
340   if ((self = [super init])) {
341     details_.reset([details retain]);
342   }
343   return self;
346 - (CocoaCookieDetails*)details {
347   return details_.get();
350 @end