blob: 693c8e417b89bb0807bb7c8585adda7eb18f9b67 (
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
// 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/collected_cookies_mac.h"
#include <vector>
#include "app/l10n_util_mac.h"
#include "app/resource_bundle.h"
#import "base/mac_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/cocoa/content_settings_dialog_controller.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/notification_service.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/apple/ImageAndTextCell.h"
#include "third_party/skia/include/core/SkBitmap.h"
static const CGFloat kMinCollectedCookiesViewHeight = 116;
#pragma mark Bridge between the constrained window delegate and the sheet
// The delegate used to forward the events from the sheet to the constrained
// window delegate.
@interface CollectedCookiesSheetBridge : NSObject {
CollectedCookiesMac* collectedCookies_; // weak
}
- (id)initWithCollectedCookiesMac:(CollectedCookiesMac*)collectedCookies;
- (void)sheetDidEnd:(NSWindow*)sheet
returnCode:(int)returnCode
contextInfo:(void*)contextInfo;
@end
@implementation CollectedCookiesSheetBridge
- (id)initWithCollectedCookiesMac:(CollectedCookiesMac*)collectedCookies {
if ((self = [super init])) {
collectedCookies_ = collectedCookies;
}
return self;
}
- (void)sheetDidEnd:(NSWindow*)sheet
returnCode:(int)returnCode
contextInfo:(void*)contextInfo {
collectedCookies_->OnSheetDidEnd(sheet);
}
@end
#pragma mark Constrained window delegate
CollectedCookiesMac::CollectedCookiesMac(NSWindow* parent,
TabContents* tab_contents)
: ConstrainedWindowMacDelegateCustomSheet(
[[[CollectedCookiesSheetBridge alloc]
initWithCollectedCookiesMac:this] autorelease],
@selector(sheetDidEnd:returnCode:contextInfo:)),
tab_contents_(tab_contents) {
TabSpecificContentSettings* content_settings =
tab_contents->GetTabSpecificContentSettings();
registrar_.Add(this, NotificationType::COLLECTED_COOKIES_SHOWN,
Source<TabSpecificContentSettings>(content_settings));
sheet_controller_ = [[CollectedCookiesWindowController alloc]
initWithTabContents:tab_contents];
set_sheet([sheet_controller_ window]);
window_ = tab_contents->CreateConstrainedDialog(this);
}
CollectedCookiesMac::~CollectedCookiesMac() {
NSWindow* window = [sheet_controller_ window];
if (window_ && window && is_sheet_open()) {
window_ = NULL;
[NSApp endSheet:window];
}
}
void CollectedCookiesMac::DeleteDelegate() {
delete this;
}
void CollectedCookiesMac::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NotificationType::COLLECTED_COOKIES_SHOWN);
DCHECK_EQ(Source<TabSpecificContentSettings>(source).ptr(),
tab_contents_->GetTabSpecificContentSettings());
window_->CloseConstrainedWindow();
}
void CollectedCookiesMac::OnSheetDidEnd(NSWindow* sheet) {
[sheet orderOut:sheet_controller_];
if (window_)
window_->CloseConstrainedWindow();
}
#pragma mark Window Controller
@implementation CollectedCookiesWindowController
@synthesize allowedCookiesButtonsEnabled =
allowedCookiesButtonsEnabled_;
@synthesize blockedCookiesButtonsEnabled =
blockedCookiesButtonsEnabled_;
@synthesize allowedTreeController = allowedTreeController_;
@synthesize blockedTreeController = blockedTreeController_;
- (id)initWithTabContents:(TabContents*)tabContents {
DCHECK(tabContents);
NSString* nibpath =
[mac_util::MainAppBundle() pathForResource:@"CollectedCookies"
ofType:@"nib"];
if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
tabContents_ = tabContents;
[self loadTreeModelFromTabContents];
}
return self;
}
- (void)windowWillClose:(NSNotification*)notif {
[allowedOutlineView_ setDelegate:nil];
[blockedOutlineView_ setDelegate:nil];
[self autorelease];
}
- (IBAction)closeSheet:(id)sender {
[NSApp endSheet:[self window]];
}
- (void)addException:(ContentSetting)setting
forTreeController:(NSTreeController*)controller {
NSArray* nodes = [controller selectedNodes];
for (NSTreeNode* treeNode in nodes) {
CocoaCookieTreeNode* node = [treeNode representedObject];
CookieTreeNode* cookie = static_cast<CookieTreeNode*>([node treeNode]);
if (cookie->GetDetailedInfo().node_type !=
CookieTreeNode::DetailedInfo::TYPE_ORIGIN) {
continue;
}
CookieTreeOriginNode* origin_node =
static_cast<CookieTreeOriginNode*>(cookie);
origin_node->CreateContentException(
tabContents_->profile()->GetHostContentSettingsMap(),
setting);
}
[[ContentSettingsDialogController
showContentSettingsForType:CONTENT_SETTINGS_TYPE_COOKIES
profile:tabContents_->profile()]
showCookieExceptions:self];
}
- (IBAction)allowOrigin:(id)sender {
[self addException:CONTENT_SETTING_ALLOW
forTreeController:blockedTreeController_];
}
- (IBAction)allowForSessionFromOrigin:(id)sender {
[self addException:CONTENT_SETTING_SESSION_ONLY
forTreeController:blockedTreeController_];
}
- (IBAction)blockOrigin:(id)sender {
[self addException:CONTENT_SETTING_BLOCK
forTreeController:allowedTreeController_];
}
- (CGFloat) splitView:(NSSplitView *)sender
constrainMinCoordinate:(CGFloat)proposedMin
ofSubviewAt:(NSInteger)offset {
return proposedMin + kMinCollectedCookiesViewHeight;
}
- (CGFloat) splitView:(NSSplitView *)sender
constrainMaxCoordinate:(CGFloat)proposedMax
ofSubviewAt:(NSInteger)offset {
return proposedMax - kMinCollectedCookiesViewHeight;
}
- (BOOL)splitView:(NSSplitView *)sender canCollapseSubview:(NSView *)subview {
return YES;
}
- (CocoaCookieTreeNode*)cocoaAllowedTreeModel {
return cocoaAllowedTreeModel_.get();
}
- (void)setCocoaAllowedTreeModel:(CocoaCookieTreeNode*)model {
cocoaAllowedTreeModel_.reset([model retain]);
}
- (CookiesTreeModel*)allowedTreeModel {
return allowedTreeModel_.get();
}
- (CocoaCookieTreeNode*)cocoaBlockedTreeModel {
return cocoaBlockedTreeModel_.get();
}
- (void)setCocoaBlockedTreeModel:(CocoaCookieTreeNode*)model {
cocoaBlockedTreeModel_.reset([model retain]);
}
- (CookiesTreeModel*)blockedTreeModel {
return blockedTreeModel_.get();
}
- (void)outlineView:(NSOutlineView*)outlineView
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn*)tableColumn
item:(id)item {
CocoaCookieTreeNode* node = [item representedObject];
int index;
if (outlineView == allowedOutlineView_)
index = allowedTreeModel_->GetIconIndex([node treeNode]);
else
index = blockedTreeModel_->GetIconIndex([node treeNode]);
NSImage* icon = nil;
if (index >= 0)
icon = [icons_ objectAtIndex:index];
else
icon = [icons_ lastObject];
DCHECK([cell isKindOfClass:[ImageAndTextCell class]]);
[static_cast<ImageAndTextCell*>(cell) setImage:icon];
}
- (void)outlineViewSelectionDidChange:(NSNotification*)notif {
BOOL isAllowedOutlineView;
if ([notif object] == allowedOutlineView_) {
isAllowedOutlineView = YES;
} else if ([notif object] == blockedOutlineView_) {
isAllowedOutlineView = NO;
} else {
NOTREACHED();
return;
}
NSTreeController* controller =
isAllowedOutlineView ? allowedTreeController_ : blockedTreeController_;
NSArray* nodes = [controller selectedNodes];
for (NSTreeNode* treeNode in nodes) {
CocoaCookieTreeNode* node = [treeNode representedObject];
CookieTreeNode* cookie = static_cast<CookieTreeNode*>([node treeNode]);
if (cookie->GetDetailedInfo().node_type !=
CookieTreeNode::DetailedInfo::TYPE_ORIGIN) {
continue;
}
CookieTreeOriginNode* origin_node =
static_cast<CookieTreeOriginNode*>(cookie);
if (origin_node->CanCreateContentException()) {
if (isAllowedOutlineView) {
[self setAllowedCookiesButtonsEnabled:YES];
} else {
[self setBlockedCookiesButtonsEnabled:YES];
}
return;
}
}
if (isAllowedOutlineView) {
[self setAllowedCookiesButtonsEnabled:NO];
} else {
[self setBlockedCookiesButtonsEnabled:NO];
}
}
// Initializes the |allowedTreeModel_| and |blockedTreeModel_|, and builds
// the |cocoaAllowedTreeModel_| and |cocoaBlockedTreeModel_|.
- (void)loadTreeModelFromTabContents {
TabSpecificContentSettings* content_settings =
tabContents_->GetTabSpecificContentSettings();
allowedTreeModel_.reset(content_settings->GetAllowedCookiesTreeModel());
blockedTreeModel_.reset(content_settings->GetBlockedCookiesTreeModel());
// Convert the model's icons from Skia to Cocoa.
std::vector<SkBitmap> skiaIcons;
allowedTreeModel_->GetIcons(&skiaIcons);
icons_.reset([[NSMutableArray alloc] init]);
for (std::vector<SkBitmap>::iterator it = skiaIcons.begin();
it != skiaIcons.end(); ++it) {
[icons_ addObject:gfx::SkBitmapToNSImage(*it)];
}
// Default icon will be the last item in the array.
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
// TODO(rsesek): Rename this resource now that it's in multiple places.
[icons_ addObject:rb.GetNSImageNamed(IDR_BOOKMARK_BAR_FOLDER)];
// Create the Cocoa model.
CookieTreeNode* root =
static_cast<CookieTreeNode*>(allowedTreeModel_->GetRoot());
scoped_nsobject<CocoaCookieTreeNode> model(
[[CocoaCookieTreeNode alloc] initWithNode:root]);
[self setCocoaAllowedTreeModel:model.get()]; // Takes ownership.
root = static_cast<CookieTreeNode*>(blockedTreeModel_->GetRoot());
model.reset(
[[CocoaCookieTreeNode alloc] initWithNode:root]);
[self setCocoaBlockedTreeModel:model.get()]; // Takes ownership.
}
@end
|