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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
// 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/content_settings_dialog_controller.h"
#import <Cocoa/Cocoa.h>
#include "app/l10n_util.h"
#include "base/mac_util.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_window.h"
#import "chrome/browser/cocoa/content_exceptions_window_controller.h"
#import "chrome/browser/cocoa/cookies_window_controller.h"
#import "chrome/browser/cocoa/geolocation_exceptions_window_controller.h"
#import "chrome/browser/cocoa/l10n_util.h"
#import "chrome/browser/geolocation/geolocation_content_settings_map.h"
#import "chrome/browser/host_content_settings_map.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "grit/locale_settings.h"
namespace {
// Stores the currently visible content settings dialog, if any.
ContentSettingsDialogController* g_instance = nil;
// Walks views in top-down order, wraps each to their current width, and moves
// the latter ones down to prevent overlaps.
CGFloat VerticallyReflowGroup(NSArray* views) {
views = [views sortedArrayUsingFunction:cocoa_l10n_util::CompareFrameY
context:NULL];
CGFloat localVerticalShift = 0;
for (NSInteger index = [views count] - 1; index >= 0; --index) {
NSView* view = [views objectAtIndex:index];
// Since the tab pane is in a horizontal resizer in IB, it's convenient
// to give all the subviews flexible width so that their sizes are
// autoupdated in IB. However, in chrome, the subviews shouldn't have
// flexible widths as this looks weird.
[view setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
NSSize delta = cocoa_l10n_util::WrapOrSizeToFit(view);
localVerticalShift += delta.height;
if (localVerticalShift) {
NSPoint origin = [view frame].origin;
origin.y -= localVerticalShift;
[view setFrameOrigin:origin];
}
}
return localVerticalShift;
}
} // namespace
@interface ContentSettingsDialogController(Private)
- (id)initWithProfile:(Profile*)profile;
- (void)selectTab:(ContentSettingsType)settingsType;
- (void)showExceptionsForType:(ContentSettingsType)settingsType;
// Callback when preferences are changed. |prefName| is the name of the
// pref that has changed.
- (void)prefChanged:(std::wstring*)prefName;
@end
namespace ContentSettingsDialogControllerInternal {
// A C++ class registered for changes in preferences.
class PrefObserverBridge : public NotificationObserver {
public:
PrefObserverBridge(ContentSettingsDialogController* controller)
: controller_(controller), disabled_(false) {}
virtual ~PrefObserverBridge() {}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (!disabled_ && type == NotificationType::PREF_CHANGED) {
[controller_ prefChanged:Details<std::wstring>(details).ptr()];
}
}
void SetDisabled(bool disabled) {
disabled_ = disabled;
}
private:
ContentSettingsDialogController* controller_; // weak, owns us
bool disabled_; // true if notifications should be ignored.
};
// A C++ utility class to disable notifications for PrefsObserverBridge.
// The intended usage is to create on the stack.
class PrefObserverDisabler {
public:
PrefObserverDisabler(PrefObserverBridge *bridge) : bridge_(bridge) {
bridge_->SetDisabled(true);
}
~PrefObserverDisabler() {
bridge_->SetDisabled(false);
}
private:
PrefObserverBridge *bridge_;
};
} // ContentSettingsDialogControllerInternal
@implementation ContentSettingsDialogController
+ (id)showContentSettingsForType:(ContentSettingsType)settingsType
profile:(Profile*)profile {
profile = profile->GetOriginalProfile();
if (!g_instance)
g_instance = [[self alloc] initWithProfile:profile];
// The code doesn't expect multiple profiles. Check that support for that
// hasn't been added.
DCHECK(g_instance->profile_ == profile);
// Select desired tab.
if (settingsType == CONTENT_SETTINGS_TYPE_DEFAULT) {
// Remember the last visited page from local state.
int value = g_instance->lastSelectedTab_.GetValue();
if (value >= 0 && value < CONTENT_SETTINGS_NUM_TYPES)
settingsType = static_cast<ContentSettingsType>(value);
if (settingsType == CONTENT_SETTINGS_TYPE_DEFAULT)
settingsType = CONTENT_SETTINGS_TYPE_COOKIES;
}
// TODO(thakis): Autosave window pos.
[g_instance selectTab:settingsType];
[g_instance showWindow:nil];
[g_instance closeExceptionsSheet];
return g_instance;
}
- (id)initWithProfile:(Profile*)profile {
DCHECK(profile);
NSString* nibpath =
[mac_util::MainAppBundle() pathForResource:@"ContentSettings"
ofType:@"nib"];
if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
profile_ = profile;
observer_.reset(
new ContentSettingsDialogControllerInternal::PrefObserverBridge(self));
clearSiteDataOnExit_.Init(prefs::kClearSiteDataOnExit,
profile_->GetPrefs(), observer_.get());
// Manually observe notifications for preferences that are grouped in
// the HostContentSettingsMap or GeolocationContentSettingsMap.
PrefService* prefs = profile_->GetPrefs();
prefs->AddPrefObserver(prefs::kBlockThirdPartyCookies, observer_.get());
prefs->AddPrefObserver(prefs::kDefaultContentSettings, observer_.get());
prefs->AddPrefObserver(prefs::kGeolocationDefaultContentSetting,
observer_.get());
// We don't need to observe changes in this value.
lastSelectedTab_.Init(prefs::kContentSettingsWindowLastTabIndex,
profile_->GetPrefs(), NULL);
}
return self;
}
- (void)dealloc {
if (profile_) {
PrefService* prefs = profile_->GetPrefs();
prefs->RemovePrefObserver(prefs::kBlockThirdPartyCookies, observer_.get());
prefs->RemovePrefObserver(prefs::kDefaultContentSettings, observer_.get());
prefs->RemovePrefObserver(prefs::kGeolocationDefaultContentSetting,
observer_.get());
}
[super dealloc];
}
- (void)closeExceptionsSheet {
NSWindow* attachedSheet = [[self window] attachedSheet];
if (attachedSheet) {
[NSApp endSheet:attachedSheet];
}
}
- (void)awakeFromNib {
DCHECK([self window]);
DCHECK_EQ(self, [[self window] delegate]);
// Adapt views to potentially long localized strings.
CGFloat windowDelta = 0;
for (NSTabViewItem* tab in [tabView_ tabViewItems]) {
windowDelta = MAX(windowDelta,
VerticallyReflowGroup([[tab view] subviews]));
}
NSRect frame = [[self window] frame];
frame.origin.y -= windowDelta;
frame.size.height += windowDelta;
[[self window] setFrame:frame display:NO];
}
// NSWindowDelegate method.
- (void)windowWillClose:(NSNotification*)notification {
[self autorelease];
g_instance = nil;
}
- (void)selectTab:(ContentSettingsType)settingsType {
[self window]; // Make sure the nib file is loaded.
DCHECK(tabView_);
[tabView_ selectTabViewItemAtIndex:settingsType];
}
// NSTabViewDelegate method.
- (void) tabView:(NSTabView*)tabView
didSelectTabViewItem:(NSTabViewItem*)tabViewItem {
DCHECK_EQ(tabView_, tabView);
NSInteger index = [tabView indexOfTabViewItem:tabViewItem];
DCHECK_GT(index, CONTENT_SETTINGS_TYPE_DEFAULT);
DCHECK_LT(index, CONTENT_SETTINGS_NUM_TYPES);
if (index > CONTENT_SETTINGS_TYPE_DEFAULT &&
index < CONTENT_SETTINGS_NUM_TYPES)
lastSelectedTab_.SetValue(index);
}
// Let esc close the window.
- (void)cancel:(id)sender {
[self close];
}
- (void)setCookieSettingIndex:(NSInteger)value {
ContentSetting setting = CONTENT_SETTING_DEFAULT;
switch (value) {
case kCookieEnabledIndex: setting = CONTENT_SETTING_ALLOW; break;
case kCookieAskIndex: setting = CONTENT_SETTING_ASK; break;
case kCookieDisabledIndex: setting = CONTENT_SETTING_BLOCK; break;
default:
NOTREACHED();
}
ContentSettingsDialogControllerInternal::PrefObserverDisabler
disabler(observer_.get());
profile_->GetHostContentSettingsMap()->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_COOKIES,
setting);
}
- (NSInteger)cookieSettingIndex {
switch (profile_->GetHostContentSettingsMap()->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_COOKIES)) {
case CONTENT_SETTING_ALLOW: return kCookieEnabledIndex;
case CONTENT_SETTING_ASK: return kCookieAskIndex;
case CONTENT_SETTING_BLOCK: return kCookieDisabledIndex;
default:
NOTREACHED();
return kCookieEnabledIndex;
}
}
- (BOOL)blockThirdPartyCookies {
HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap();
return settingsMap->BlockThirdPartyCookies();
}
- (void)setBlockThirdPartyCookies:(BOOL)value {
HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap();
ContentSettingsDialogControllerInternal::PrefObserverDisabler
disabler(observer_.get());
settingsMap->SetBlockThirdPartyCookies(value);
}
- (BOOL)clearSiteDataOnExit {
return clearSiteDataOnExit_.GetValue();
}
- (void)setClearSiteDataOnExit:(BOOL)value {
ContentSettingsDialogControllerInternal::PrefObserverDisabler
disabler(observer_.get());
clearSiteDataOnExit_.SetValue(value);
}
// Shows the cookies controller.
- (IBAction)showCookies:(id)sender {
// The cookie controller will autorelease itself when it's closed.
BrowsingDataDatabaseHelper* databaseHelper =
new BrowsingDataDatabaseHelper(profile_);
BrowsingDataLocalStorageHelper* storageHelper =
new BrowsingDataLocalStorageHelper(profile_);
BrowsingDataAppCacheHelper* appcacheHelper =
new BrowsingDataAppCacheHelper(profile_);
CookiesWindowController* controller =
[[CookiesWindowController alloc] initWithProfile:profile_
databaseHelper:databaseHelper
storageHelper:storageHelper
appcacheHelper:appcacheHelper];
[controller attachSheetTo:[self window]];
}
// Called when the user clicks the "Flash Player storage settings" button.
- (IBAction)openFlashPlayerSettings:(id)sender {
Browser* browser = Browser::Create(profile_);
browser->OpenURL(GURL(l10n_util::GetStringUTF8(IDS_FLASH_STORAGE_URL)),
GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK);
browser->window()->Show();
}
// Called when the user clicks the "Disable individual plug-ins..." button.
- (IBAction)openPluginsPage:(id)sender {
Browser* browser = Browser::Create(profile_);
browser->OpenURL(GURL(chrome::kChromeUIPluginsURL),
GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK);
browser->window()->Show();
}
- (IBAction)showCookieExceptions:(id)sender {
[self showExceptionsForType:CONTENT_SETTINGS_TYPE_COOKIES];
}
- (IBAction)showImagesExceptions:(id)sender {
[self showExceptionsForType:CONTENT_SETTINGS_TYPE_IMAGES];
}
- (IBAction)showJavaScriptExceptions:(id)sender {
[self showExceptionsForType:CONTENT_SETTINGS_TYPE_JAVASCRIPT];
}
- (IBAction)showPluginsExceptions:(id)sender {
[self showExceptionsForType:CONTENT_SETTINGS_TYPE_PLUGINS];
}
- (IBAction)showPopupsExceptions:(id)sender {
[self showExceptionsForType:CONTENT_SETTINGS_TYPE_POPUPS];
}
- (IBAction)showGeolocationExceptions:(id)sender {
GeolocationContentSettingsMap* settingsMap =
profile_->GetGeolocationContentSettingsMap();
[[GeolocationExceptionsWindowController controllerWithSettingsMap:settingsMap]
attachSheetTo:[self window]];
}
- (void)showExceptionsForType:(ContentSettingsType)settingsType {
HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap();
[[ContentExceptionsWindowController controllerForType:settingsType
settingsMap:settingsMap]
attachSheetTo:[self window]];
}
- (void)setImagesEnabledIndex:(NSInteger)value {
ContentSetting setting = value == kContentSettingsEnabledIndex ?
CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
ContentSettingsDialogControllerInternal::PrefObserverDisabler
disabler(observer_.get());
profile_->GetHostContentSettingsMap()->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_IMAGES, setting);
}
- (NSInteger)imagesEnabledIndex {
HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap();
bool enabled =
settingsMap->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_IMAGES) ==
CONTENT_SETTING_ALLOW;
return enabled ? kContentSettingsEnabledIndex : kContentSettingsDisabledIndex;
}
- (void)setJavaScriptEnabledIndex:(NSInteger)value {
ContentSetting setting = value == kContentSettingsEnabledIndex ?
CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
ContentSettingsDialogControllerInternal::PrefObserverDisabler
disabler(observer_.get());
profile_->GetHostContentSettingsMap()->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, setting);
}
- (NSInteger)javaScriptEnabledIndex {
HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap();
bool enabled =
settingsMap->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT) ==
CONTENT_SETTING_ALLOW;
return enabled ? kContentSettingsEnabledIndex : kContentSettingsDisabledIndex;
}
- (void)setPluginsEnabledIndex:(NSInteger)value {
ContentSetting setting = value == kContentSettingsEnabledIndex ?
CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
ContentSettingsDialogControllerInternal::PrefObserverDisabler
disabler(observer_.get());
profile_->GetHostContentSettingsMap()->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, setting);
}
- (NSInteger)pluginsEnabledIndex {
HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap();
bool enabled =
settingsMap->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS) ==
CONTENT_SETTING_ALLOW;
return enabled ? kContentSettingsEnabledIndex : kContentSettingsDisabledIndex;
}
- (void)setPopupsEnabledIndex:(NSInteger)value {
ContentSetting setting = value == kContentSettingsEnabledIndex ?
CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
ContentSettingsDialogControllerInternal::PrefObserverDisabler
disabler(observer_.get());
profile_->GetHostContentSettingsMap()->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_POPUPS, setting);
}
- (NSInteger)popupsEnabledIndex {
HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap();
bool enabled =
settingsMap->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS) ==
CONTENT_SETTING_ALLOW;
return enabled ? kContentSettingsEnabledIndex : kContentSettingsDisabledIndex;
}
- (void)setGeolocationSettingIndex:(NSInteger)value {
ContentSetting setting = CONTENT_SETTING_DEFAULT;
switch (value) {
case kGeolocationEnabledIndex: setting = CONTENT_SETTING_ALLOW; break;
case kGeolocationAskIndex: setting = CONTENT_SETTING_ASK; break;
case kGeolocationDisabledIndex: setting = CONTENT_SETTING_BLOCK; break;
default:
NOTREACHED();
}
ContentSettingsDialogControllerInternal::PrefObserverDisabler
disabler(observer_.get());
profile_->GetGeolocationContentSettingsMap()->SetDefaultContentSetting(
setting);
}
- (NSInteger)geolocationSettingIndex {
ContentSetting setting =
profile_->GetGeolocationContentSettingsMap()->GetDefaultContentSetting();
switch (setting) {
case CONTENT_SETTING_ALLOW: return kGeolocationEnabledIndex;
case CONTENT_SETTING_ASK: return kGeolocationAskIndex;
case CONTENT_SETTING_BLOCK: return kGeolocationDisabledIndex;
default:
NOTREACHED();
return kGeolocationAskIndex;
}
}
// Callback when preferences are changed. |prefName| is the name of the
// pref that has changed and should not be NULL.
- (void)prefChanged:(std::wstring*)prefName {
DCHECK(prefName);
if (!prefName) return;
if (*prefName == prefs::kClearSiteDataOnExit) {
[self willChangeValueForKey:@"clearSiteDataOnExit"];
[self didChangeValueForKey:@"clearSiteDataOnExit"];
}
if (*prefName == prefs::kBlockThirdPartyCookies) {
[self willChangeValueForKey:@"blockThirdPartyCookies"];
[self didChangeValueForKey:@"blockThirdPartyCookies"];
}
if (*prefName == prefs::kDefaultContentSettings) {
// We don't know exactly which setting has changed, so we'll tickle all
// of the properties that apply to kDefaultContentSettings. This will
// keep the UI up-to-date.
[self willChangeValueForKey:@"cookieSettingIndex"];
[self didChangeValueForKey:@"cookieSettingIndex"];
[self willChangeValueForKey:@"imagesEnabledIndex"];
[self didChangeValueForKey:@"imagesEnabledIndex"];
[self willChangeValueForKey:@"javaScriptEnabledIndex"];
[self didChangeValueForKey:@"javaScriptEnabledIndex"];
[self willChangeValueForKey:@"pluginsEnabledIndex"];
[self didChangeValueForKey:@"pluginsEnabledIndex"];
[self willChangeValueForKey:@"popupsEnabledIndex"];
[self didChangeValueForKey:@"popupsEnabledIndex"];
}
if (*prefName == prefs::kGeolocationDefaultContentSetting) {
[self willChangeValueForKey:@"geolocationSettingIndex"];
[self didChangeValueForKey:@"geolocationSettingIndex"];
}
}
@end
|