32b50d75343f29d86c8b535208dccd53fa5b54dc
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / content_settings / cookie_details.mm
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.
4
5 #include "chrome/browser/ui/cocoa/content_settings/cookie_details.h"
6
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 "content/public/browser/appcache_service.h"
15
16 #pragma mark Cocoa Cookie Details
17
18 @implementation CocoaCookieDetails
19
20 @synthesize canEditExpiration = canEditExpiration_;
21 @synthesize hasExpiration = hasExpiration_;
22 @synthesize type = type_;
23
24 - (BOOL)shouldHideCookieDetailsView {
25   return type_ != kCocoaCookieDetailsTypeFolder &&
26       type_ != kCocoaCookieDetailsTypeCookie;
27 }
28
29 - (BOOL)shouldShowLocalStorageTreeDetailsView {
30   return type_ == kCocoaCookieDetailsTypeTreeLocalStorage;
31 }
32
33 - (BOOL)shouldShowLocalStoragePromptDetailsView {
34   return type_ == kCocoaCookieDetailsTypePromptLocalStorage;
35 }
36
37 - (BOOL)shouldShowDatabaseTreeDetailsView {
38   return type_ == kCocoaCookieDetailsTypeTreeDatabase;
39 }
40
41 - (BOOL)shouldShowAppCacheTreeDetailsView {
42   return type_ == kCocoaCookieDetailsTypeTreeAppCache;
43 }
44
45 - (BOOL)shouldShowDatabasePromptDetailsView {
46   return type_ == kCocoaCookieDetailsTypePromptDatabase;
47 }
48
49 - (BOOL)shouldShowAppCachePromptDetailsView {
50   return type_ == kCocoaCookieDetailsTypePromptAppCache;
51 }
52
53 - (BOOL)shouldShowIndexedDBTreeDetailsView {
54   return type_ == kCocoaCookieDetailsTypeTreeIndexedDB;
55 }
56
57 - (BOOL)shouldShowServiceWorkerTreeDetailsView {
58   return type_ == kCocoaCookieDetailsTypeTreeServiceWorker;
59 }
60
61 - (NSString*)name {
62   return name_.get();
63 }
64
65 - (NSString*)content {
66   return content_.get();
67 }
68
69 - (NSString*)domain {
70   return domain_.get();
71 }
72
73 - (NSString*)path {
74   return path_.get();
75 }
76
77 - (NSString*)sendFor {
78   return sendFor_.get();
79 }
80
81 - (NSString*)created {
82   return created_.get();
83 }
84
85 - (NSString*)expires {
86   return expires_.get();
87 }
88
89 - (NSString*)fileSize {
90   return fileSize_.get();
91 }
92
93 - (NSString*)lastModified {
94   return lastModified_.get();
95 }
96
97 - (NSString*)lastAccessed {
98   return lastAccessed_.get();
99 }
100
101 - (NSString*)databaseDescription {
102   return databaseDescription_.get();
103 }
104
105 - (NSString*)localStorageKey {
106   return localStorageKey_.get();
107 }
108
109 - (NSString*)localStorageValue {
110   return localStorageValue_.get();
111 }
112
113 - (NSString*)manifestURL {
114   return manifestURL_.get();
115 }
116
117 - (NSString*)scopes {
118   return scopes_.get();
119 }
120
121 - (id)initAsFolder {
122   if ((self = [super init])) {
123     type_ = kCocoaCookieDetailsTypeFolder;
124   }
125   return self;
126 }
127
128 - (id)initWithCookie:(const net::CanonicalCookie*)cookie
129    canEditExpiration:(BOOL)canEditExpiration {
130   if ((self = [super init])) {
131     type_ = kCocoaCookieDetailsTypeCookie;
132     hasExpiration_ = cookie->IsPersistent();
133     canEditExpiration_ = canEditExpiration && hasExpiration_;
134     name_.reset([base::SysUTF8ToNSString(cookie->Name()) retain]);
135     content_.reset([base::SysUTF8ToNSString(cookie->Value()) retain]);
136     path_.reset([base::SysUTF8ToNSString(cookie->Path()) retain]);
137     domain_.reset([base::SysUTF8ToNSString(cookie->Domain()) retain]);
138
139     if (cookie->IsPersistent()) {
140       expires_.reset([base::SysUTF16ToNSString(
141           base::TimeFormatFriendlyDateAndTime(cookie->ExpiryDate())) retain]);
142     } else {
143       expires_.reset([l10n_util::GetNSStringWithFixup(
144           IDS_COOKIES_COOKIE_EXPIRES_SESSION) retain]);
145     }
146
147     created_.reset([base::SysUTF16ToNSString(
148         base::TimeFormatFriendlyDateAndTime(cookie->CreationDate())) retain]);
149
150     if (cookie->IsSecure()) {
151       sendFor_.reset([l10n_util::GetNSStringWithFixup(
152           IDS_COOKIES_COOKIE_SENDFOR_SECURE) retain]);
153     } else {
154       sendFor_.reset([l10n_util::GetNSStringWithFixup(
155           IDS_COOKIES_COOKIE_SENDFOR_ANY) retain]);
156     }
157   }
158   return self;
159 }
160
161 - (id)initWithDatabase:(const BrowsingDataDatabaseHelper::DatabaseInfo*)
162     databaseInfo {
163   if ((self = [super init])) {
164     type_ = kCocoaCookieDetailsTypeTreeDatabase;
165     canEditExpiration_ = NO;
166     databaseDescription_.reset([base::SysUTF8ToNSString(
167         databaseInfo->description) retain]);
168     fileSize_.reset([base::SysUTF16ToNSString(
169         ui::FormatBytes(databaseInfo->size)) retain]);
170     lastModified_.reset([base::SysUTF16ToNSString(
171         base::TimeFormatFriendlyDateAndTime(
172             databaseInfo->last_modified)) retain]);
173   }
174   return self;
175 }
176
177 - (id)initWithLocalStorage:(
178     const BrowsingDataLocalStorageHelper::LocalStorageInfo*)storageInfo {
179   if ((self = [super init])) {
180     type_ = kCocoaCookieDetailsTypeTreeLocalStorage;
181     canEditExpiration_ = NO;
182     domain_.reset(
183         [base::SysUTF8ToNSString(storageInfo->origin_url.spec()) retain]);
184     fileSize_.reset(
185         [base::SysUTF16ToNSString(ui::FormatBytes(storageInfo->size)) retain]);
186     lastModified_.reset([base::SysUTF16ToNSString(
187         base::TimeFormatFriendlyDateAndTime(
188             storageInfo->last_modified)) retain]);
189   }
190   return self;
191 }
192
193 - (id)initWithAppCacheInfo:(const content::AppCacheInfo*)appcacheInfo {
194   if ((self = [super init])) {
195     type_ = kCocoaCookieDetailsTypeTreeAppCache;
196     canEditExpiration_ = NO;
197     manifestURL_.reset([base::SysUTF8ToNSString(
198         appcacheInfo->manifest_url.spec()) retain]);
199     fileSize_.reset([base::SysUTF16ToNSString(
200         ui::FormatBytes(appcacheInfo->size)) retain]);
201     created_.reset([base::SysUTF16ToNSString(
202         base::TimeFormatFriendlyDateAndTime(
203             appcacheInfo->creation_time)) retain]);
204     lastAccessed_.reset([base::SysUTF16ToNSString(
205         base::TimeFormatFriendlyDateAndTime(
206             appcacheInfo->last_access_time)) retain]);
207   }
208   return self;
209 }
210
211 - (id)initWithDatabase:(const std::string&)domain
212           databaseName:(const base::string16&)databaseName
213    databaseDescription:(const base::string16&)databaseDescription
214               fileSize:(unsigned long)fileSize {
215   if ((self = [super init])) {
216     type_ = kCocoaCookieDetailsTypePromptDatabase;
217     canEditExpiration_ = NO;
218     name_.reset([base::SysUTF16ToNSString(databaseName) retain]);
219     domain_.reset([base::SysUTF8ToNSString(domain) retain]);
220     databaseDescription_.reset(
221         [base::SysUTF16ToNSString(databaseDescription) retain]);
222     fileSize_.reset(
223         [base::SysUTF16ToNSString(ui::FormatBytes(fileSize)) retain]);
224   }
225   return self;
226 }
227
228 - (id)initWithLocalStorage:(const std::string&)domain
229                        key:(const base::string16&)key
230                      value:(const base::string16&)value {
231   if ((self = [super init])) {
232     type_ = kCocoaCookieDetailsTypePromptLocalStorage;
233     canEditExpiration_ = NO;
234     domain_.reset([base::SysUTF8ToNSString(domain) retain]);
235     localStorageKey_.reset([base::SysUTF16ToNSString(key) retain]);
236     localStorageValue_.reset([base::SysUTF16ToNSString(value) retain]);
237   }
238   return self;
239 }
240
241 - (id)initWithAppCacheManifestURL:(const std::string&)manifestURL {
242   if ((self = [super init])) {
243     type_ = kCocoaCookieDetailsTypePromptAppCache;
244     canEditExpiration_ = NO;
245     manifestURL_.reset([base::SysUTF8ToNSString(manifestURL) retain]);
246   }
247   return self;
248 }
249
250 - (id)initWithIndexedDBInfo:
251     (const content::IndexedDBInfo*)indexedDBInfo {
252   if ((self = [super init])) {
253     type_ = kCocoaCookieDetailsTypeTreeIndexedDB;
254     canEditExpiration_ = NO;
255     domain_.reset([base::SysUTF8ToNSString(
256         indexedDBInfo->origin_.spec()) retain]);
257     fileSize_.reset([base::SysUTF16ToNSString(
258         ui::FormatBytes(indexedDBInfo->size_)) retain]);
259     lastModified_.reset([base::SysUTF16ToNSString(
260         base::TimeFormatFriendlyDateAndTime(
261             indexedDBInfo->last_modified_)) retain]);
262   }
263   return self;
264 }
265
266 - (id)initWithServiceWorkerUsageInfo:
267     (const content::ServiceWorkerUsageInfo*)serviceWorkerInfo {
268   if ((self = [super init])) {
269     type_ = kCocoaCookieDetailsTypeTreeServiceWorker;
270     canEditExpiration_ = NO;
271     domain_.reset([base::SysUTF8ToNSString(
272         serviceWorkerInfo->origin.spec()) retain]);
273
274     NSMutableArray *scopes = [[NSMutableArray alloc]
275                          initWithCapacity:serviceWorkerInfo->scopes.size()];
276     for (std::vector<GURL>::const_iterator it =
277              serviceWorkerInfo->scopes.begin();
278          it != serviceWorkerInfo->scopes.end(); ++it) {
279       [scopes addObject:base::SysUTF8ToNSString(it->spec())];
280     }
281     scopes_.reset([[scopes componentsJoinedByString:@","] retain]);
282   }
283   return self;
284 }
285
286 + (CocoaCookieDetails*)createFromCookieTreeNode:(CookieTreeNode*)treeNode {
287   CookieTreeNode::DetailedInfo info = treeNode->GetDetailedInfo();
288   CookieTreeNode::DetailedInfo::NodeType nodeType = info.node_type;
289   switch (nodeType) {
290     case CookieTreeNode::DetailedInfo::TYPE_COOKIE:
291       return [[[CocoaCookieDetails alloc] initWithCookie:info.cookie
292                                        canEditExpiration:NO] autorelease];
293     case CookieTreeNode::DetailedInfo::TYPE_DATABASE:
294       return [[[CocoaCookieDetails alloc]
295           initWithDatabase:info.database_info] autorelease];
296     case CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE:
297       return [[[CocoaCookieDetails alloc]
298           initWithLocalStorage:info.local_storage_info] autorelease];
299     case CookieTreeNode::DetailedInfo::TYPE_APPCACHE:
300       return [[[CocoaCookieDetails alloc]
301           initWithAppCacheInfo:info.appcache_info] autorelease];
302     case CookieTreeNode::DetailedInfo::TYPE_INDEXED_DB:
303       return [[[CocoaCookieDetails alloc]
304           initWithIndexedDBInfo:info.indexed_db_info] autorelease];
305     case CookieTreeNode::DetailedInfo::TYPE_SERVICE_WORKER:
306       return [[[CocoaCookieDetails alloc]
307           initWithServiceWorkerUsageInfo:info.service_worker_info] autorelease];
308     default:
309       return [[[CocoaCookieDetails alloc] initAsFolder] autorelease];
310   }
311 }
312
313 @end
314
315 #pragma mark Content Object Adapter
316
317 @implementation CookiePromptContentDetailsAdapter
318
319 - (id)initWithDetails:(CocoaCookieDetails*)details {
320   if ((self = [super init])) {
321     details_.reset([details retain]);
322   }
323   return self;
324 }
325
326 - (CocoaCookieDetails*)details {
327   return details_.get();
328 }
329
330 @end