| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/cocoa/content_settings_dialog_controller.h" | 5 #import "chrome/browser/cocoa/content_settings_dialog_controller.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/mac_util.h" | 10 #include "base/mac_util.h" |
| 11 #include "chrome/browser/browser.h" | 11 #include "chrome/browser/browser.h" |
| 12 #include "chrome/browser/browser_window.h" | 12 #include "chrome/browser/browser_window.h" |
| 13 #import "chrome/browser/cocoa/content_exceptions_window_controller.h" | 13 #import "chrome/browser/cocoa/content_exceptions_window_controller.h" |
| 14 #import "chrome/browser/cocoa/cookies_window_controller.h" | 14 #import "chrome/browser/cocoa/cookies_window_controller.h" |
| 15 #import "chrome/browser/host_content_settings_map.h" | 15 #import "chrome/browser/host_content_settings_map.h" |
| 16 #include "chrome/browser/pref_service.h" |
| 16 #include "chrome/browser/profile.h" | 17 #include "chrome/browser/profile.h" |
| 18 #include "chrome/common/notification_service.h" |
| 17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 18 #include "grit/locale_settings.h" | 20 #include "grit/locale_settings.h" |
| 19 | 21 |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 // Index of the "enabled" and "disabled" radio group settings in all tabs except | 25 // Index of the "enabled" and "disabled" radio group settings in all tabs except |
| 24 // for the cookies tab. | 26 // for the cookies tab. |
| 25 const NSInteger kEnabledIndex = 0; | 27 const NSInteger kEnabledIndex = 0; |
| 26 const NSInteger kDisabledIndex = 1; | 28 const NSInteger kDisabledIndex = 1; |
| 27 | 29 |
| 28 // Indices of the various cookie settings in the cookie radio group. | 30 // Indices of the various cookie settings in the cookie radio group. |
| 29 const NSInteger kCookieEnabledIndex = 0; | 31 const NSInteger kCookieEnabledIndex = 0; |
| 30 const NSInteger kCookieAskIndex = 1; | 32 const NSInteger kCookieAskIndex = 1; |
| 31 const NSInteger kCookieDisabledIndex = 2; | 33 const NSInteger kCookieDisabledIndex = 2; |
| 32 | 34 |
| 33 // Stores the currently visible content settings dialog, if any. | 35 // Stores the currently visible content settings dialog, if any. |
| 34 ContentSettingsDialogController* g_instance = nil; | 36 ContentSettingsDialogController* g_instance = nil; |
| 35 | 37 |
| 36 } // namespace | 38 } // namespace |
| 37 | 39 |
| 38 | 40 |
| 39 @interface ContentSettingsDialogController(Private) | 41 @interface ContentSettingsDialogController(Private) |
| 40 - (id)initWithProfile:(Profile*)profile; | 42 - (id)initWithProfile:(Profile*)profile; |
| 43 - (void)selectTab:(ContentSettingsType)settingsType; |
| 41 - (void)showExceptionsForType:(ContentSettingsType)settingsType; | 44 - (void)showExceptionsForType:(ContentSettingsType)settingsType; |
| 42 | 45 |
| 43 // Properties that the radio groups and checkboxes are bound to. | 46 // Properties that the radio groups and checkboxes are bound to. |
| 44 @property(assign, nonatomic) NSInteger cookieSettingIndex; | 47 @property(assign, nonatomic) NSInteger cookieSettingIndex; |
| 45 @property(assign, nonatomic) BOOL blockThirdPartyCookies; | 48 @property(assign, nonatomic) BOOL blockThirdPartyCookies; |
| 46 @property(assign, nonatomic) BOOL clearSiteDataOnExit; | 49 @property(assign, nonatomic) BOOL clearSiteDataOnExit; |
| 47 @property(assign, nonatomic) NSInteger imagesEnabledIndex; | 50 @property(assign, nonatomic) NSInteger imagesEnabledIndex; |
| 48 @property(assign, nonatomic) NSInteger javaScriptEnabledIndex; | 51 @property(assign, nonatomic) NSInteger javaScriptEnabledIndex; |
| 49 @property(assign, nonatomic) NSInteger popupsEnabledIndex; | 52 @property(assign, nonatomic) NSInteger popupsEnabledIndex; |
| 50 @property(assign, nonatomic) NSInteger pluginsEnabledIndex; | 53 @property(assign, nonatomic) NSInteger pluginsEnabledIndex; |
| 51 @end | 54 @end |
| 52 | 55 |
| 56 // A C++ class registered for changes in preferences. |
| 57 class PrefObserverBridge : public NotificationObserver { |
| 58 public: |
| 59 PrefObserverBridge(ContentSettingsDialogController* controller) |
| 60 : controller_(controller) {} |
| 61 |
| 62 virtual ~PrefObserverBridge() {} |
| 63 |
| 64 virtual void Observe(NotificationType type, |
| 65 const NotificationSource& source, |
| 66 const NotificationDetails& details) { |
| 67 std::wstring* pref_name = Details<std::wstring>(details).ptr(); |
| 68 if (type == NotificationType::PREF_CHANGED && |
| 69 *pref_name == prefs::kClearSiteDataOnExit) { |
| 70 // Update UI. |
| 71 [controller_ setClearSiteDataOnExit:[controller_ clearSiteDataOnExit]]; |
| 72 } |
| 73 } |
| 74 |
| 75 private: |
| 76 ContentSettingsDialogController* controller_; // weak, owns us |
| 77 }; |
| 78 |
| 53 | 79 |
| 54 @implementation ContentSettingsDialogController | 80 @implementation ContentSettingsDialogController |
| 55 | 81 |
| 56 +(id)showContentSettingsForType:(ContentSettingsType)settingsType | 82 +(id)showContentSettingsForType:(ContentSettingsType)settingsType |
| 57 profile:(Profile*)profile { | 83 profile:(Profile*)profile { |
| 58 profile = profile->GetOriginalProfile(); | 84 profile = profile->GetOriginalProfile(); |
| 59 if (!g_instance) | 85 if (!g_instance) |
| 60 g_instance = [[self alloc] initWithProfile:profile]; | 86 g_instance = [[self alloc] initWithProfile:profile]; |
| 61 | 87 |
| 62 // The code doesn't expect multiple profiles. Check that support for that | 88 // The code doesn't expect multiple profiles. Check that support for that |
| 63 // hasn't been added. | 89 // hasn't been added. |
| 64 DCHECK(g_instance->profile_ == profile); | 90 DCHECK(g_instance->profile_ == profile); |
| 65 | 91 |
| 66 // Select desired tab. | 92 // Select desired tab. |
| 67 if (settingsType == CONTENT_SETTINGS_TYPE_DEFAULT) { | 93 if (settingsType == CONTENT_SETTINGS_TYPE_DEFAULT) { |
| 68 // Remember the last visited page from local state. | 94 // Remember the last visited page from local state. |
| 69 int value = g_instance->lastSelectedTab_.GetValue(); | 95 int value = g_instance->lastSelectedTab_.GetValue(); |
| 70 if (value >= 0 && value < CONTENT_SETTINGS_NUM_TYPES) | 96 if (value >= 0 && value < CONTENT_SETTINGS_NUM_TYPES) |
| 71 settingsType = static_cast<ContentSettingsType>(value); | 97 settingsType = static_cast<ContentSettingsType>(value); |
| 72 if (settingsType == CONTENT_SETTINGS_TYPE_DEFAULT) | 98 if (settingsType == CONTENT_SETTINGS_TYPE_DEFAULT) |
| 73 settingsType = CONTENT_SETTINGS_TYPE_COOKIES; | 99 settingsType = CONTENT_SETTINGS_TYPE_COOKIES; |
| 74 } | 100 } |
| 75 // TODO(thakis): Actually select desired tab. | |
| 76 // TODO(thakis): Safe current tab on tab switch as well. | |
| 77 // TODO(thakis): Autosave window pos. | 101 // TODO(thakis): Autosave window pos. |
| 78 | 102 |
| 103 [g_instance selectTab:settingsType]; |
| 79 [g_instance showWindow:nil]; | 104 [g_instance showWindow:nil]; |
| 80 return g_instance; | 105 return g_instance; |
| 81 } | 106 } |
| 82 | 107 |
| 83 - (id)initWithProfile:(Profile*)profile { | 108 - (id)initWithProfile:(Profile*)profile { |
| 84 DCHECK(profile); | 109 DCHECK(profile); |
| 85 NSString* nibpath = | 110 NSString* nibpath = |
| 86 [mac_util::MainAppBundle() pathForResource:@"ContentSettings" | 111 [mac_util::MainAppBundle() pathForResource:@"ContentSettings" |
| 87 ofType:@"nib"]; | 112 ofType:@"nib"]; |
| 88 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { | 113 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
| 89 profile_ = profile; | 114 profile_ = profile; |
| 90 | 115 |
| 91 // TODO(thakis): Listen for kClearSiteDataOnExit pref change notifications. | 116 observer_.reset(new PrefObserverBridge(self)); |
| 92 clearSiteDataOnExit_.Init(prefs::kClearSiteDataOnExit, | 117 clearSiteDataOnExit_.Init(prefs::kClearSiteDataOnExit, |
| 93 profile->GetPrefs(), NULL); | 118 profile->GetPrefs(), NULL); |
| 94 | 119 |
| 95 // We don't need to observe changes in this value. | 120 // We don't need to observe changes in this value. |
| 96 lastSelectedTab_.Init(prefs::kContentSettingsWindowLastTabIndex, | 121 lastSelectedTab_.Init(prefs::kContentSettingsWindowLastTabIndex, |
| 97 profile->GetPrefs(), NULL); | 122 profile->GetPrefs(), NULL); |
| 98 } | 123 } |
| 99 return self; | 124 return self; |
| 100 } | 125 } |
| 101 | 126 |
| 102 - (void)awakeFromNib { | 127 - (void)awakeFromNib { |
| 103 DCHECK([self window]); | 128 DCHECK([self window]); |
| 104 DCHECK_EQ(self, [[self window] delegate]); | 129 DCHECK_EQ(self, [[self window] delegate]); |
| 105 } | 130 } |
| 106 | 131 |
| 132 // NSWindowDelegate method. |
| 107 - (void)windowWillClose:(NSNotification*)notification { | 133 - (void)windowWillClose:(NSNotification*)notification { |
| 108 [self autorelease]; | 134 [self autorelease]; |
| 109 g_instance = nil; | 135 g_instance = nil; |
| 110 } | 136 } |
| 111 | 137 |
| 138 - (void)selectTab:(ContentSettingsType)settingsType { |
| 139 [self window]; // Make sure the nib file is loaded. |
| 140 DCHECK(tabView_); |
| 141 [tabView_ selectTabViewItemAtIndex:settingsType]; |
| 142 } |
| 143 |
| 144 // NSTabViewDelegate method. |
| 145 - (void) tabView:(NSTabView*)tabView |
| 146 didSelectTabViewItem:(NSTabViewItem*)tabViewItem { |
| 147 DCHECK_EQ(tabView_, tabView); |
| 148 NSInteger index = [tabView indexOfTabViewItem:tabViewItem]; |
| 149 DCHECK_GT(index, CONTENT_SETTINGS_TYPE_DEFAULT); |
| 150 DCHECK_LT(index, CONTENT_SETTINGS_NUM_TYPES); |
| 151 if (index > CONTENT_SETTINGS_TYPE_DEFAULT && |
| 152 index < CONTENT_SETTINGS_NUM_TYPES) |
| 153 lastSelectedTab_.SetValue(index); |
| 154 } |
| 155 |
| 112 // Let esc close the window. | 156 // Let esc close the window. |
| 113 - (void)cancel:(id)sender { | 157 - (void)cancel:(id)sender { |
| 114 [self close]; | 158 [self close]; |
| 115 } | 159 } |
| 116 | 160 |
| 117 - (void)setCookieSettingIndex:(NSInteger)value { | 161 - (void)setCookieSettingIndex:(NSInteger)value { |
| 118 ContentSetting setting = CONTENT_SETTING_DEFAULT; | 162 ContentSetting setting = CONTENT_SETTING_DEFAULT; |
| 119 switch (value) { | 163 switch (value) { |
| 120 case kCookieEnabledIndex: setting = CONTENT_SETTING_ALLOW; break; | 164 case kCookieEnabledIndex: setting = CONTENT_SETTING_ALLOW; break; |
| 121 case kCookieAskIndex: setting = CONTENT_SETTING_ASK; break; | 165 case kCookieAskIndex: setting = CONTENT_SETTING_ASK; break; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 | 304 |
| 261 - (NSInteger)popupsEnabledIndex { | 305 - (NSInteger)popupsEnabledIndex { |
| 262 HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap(); | 306 HostContentSettingsMap* settingsMap = profile_->GetHostContentSettingsMap(); |
| 263 bool enabled = | 307 bool enabled = |
| 264 settingsMap->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS) == | 308 settingsMap->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS) == |
| 265 CONTENT_SETTING_ALLOW; | 309 CONTENT_SETTING_ALLOW; |
| 266 return enabled ? kEnabledIndex : kDisabledIndex; | 310 return enabled ? kEnabledIndex : kDisabledIndex; |
| 267 } | 311 } |
| 268 | 312 |
| 269 @end | 313 @end |
| OLD | NEW |