summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/ui')
-rw-r--r--chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.cc53
-rw-r--r--chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.h41
-rw-r--r--chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper_unittest.cc69
-rw-r--r--chrome/browser/ui/tab_contents/tab_contents_wrapper.cc17
-rw-r--r--chrome/browser/ui/tab_contents/tab_contents_wrapper.h7
5 files changed, 179 insertions, 8 deletions
diff --git a/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.cc b/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.cc
new file mode 100644
index 0000000..4a5abb9
--- /dev/null
+++ b/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.cc
@@ -0,0 +1,53 @@
+// Copyright (c) 2011 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.
+
+#include "chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.h"
+
+#include "chrome/browser/prefs/pref_service.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/ui/constrained_window_tab_helper.h"
+#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
+#include "chrome/common/pref_names.h"
+#include "webkit/glue/webpreferences.h"
+
+const char* kPerTabPrefsToObserve[] = {
+ prefs::kWebKitJavascriptEnabled
+};
+
+const int kPerTabPrefsToObserveLength = arraysize(kPerTabPrefsToObserve);
+
+PerTabPrefsTabHelper::PerTabPrefsTabHelper(
+ TabContentsWrapper* wrapper)
+ : TabContentsObserver(wrapper->tab_contents()),
+ wrapper_(wrapper) {
+ prefs_.reset(
+ wrapper_->profile()->GetPrefs()->CreatePrefServiceWithPerTabPrefStore());
+ RegisterPerTabUserPrefs(prefs_.get());
+
+ // Notify the wrapper about all interested prefs changes.
+ pref_change_registrar_.Init(prefs_.get());
+ for (int i = 0; i < kPerTabPrefsToObserveLength; ++i) {
+ pref_change_registrar_.Add(kPerTabPrefsToObserve[i], wrapper_);
+ }
+}
+
+PerTabPrefsTabHelper::~PerTabPrefsTabHelper() {
+}
+
+void PerTabPrefsTabHelper::OverrideWebPreferences(WebPreferences* prefs) {
+ prefs->javascript_enabled =
+ prefs_->GetBoolean(prefs::kWebKitJavascriptEnabled);
+}
+
+void PerTabPrefsTabHelper::RegisterPerTabUserPrefs(PrefService* prefs) {
+ WebPreferences pref_defaults;
+
+ prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
+ pref_defaults.javascript_enabled,
+ PrefService::UNSYNCABLE_PREF);
+}
+
+void PerTabPrefsTabHelper::TabContentsDestroyed(TabContents* tab) {
+ pref_change_registrar_.RemoveAll();
+}
diff --git a/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.h b/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.h
new file mode 100644
index 0000000..5784dd1
--- /dev/null
+++ b/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.h
@@ -0,0 +1,41 @@
+// Copyright (c) 2011 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.
+
+#ifndef CHROME_BROWSER_UI_TAB_CONTENTS_PER_TAB_PREFS_TAB_HELPER_H_
+#define CHROME_BROWSER_UI_TAB_CONTENTS_PER_TAB_PREFS_TAB_HELPER_H_
+#pragma once
+
+#include "chrome/browser/prefs/pref_change_registrar.h"
+#include "content/browser/tab_contents/tab_contents_observer.h"
+
+class PrefService;
+class TabContentsWrapper;
+struct WebPreferences;
+
+// Per-tab class to override user preferences.
+class PerTabPrefsTabHelper : public TabContentsObserver {
+ public:
+ explicit PerTabPrefsTabHelper(TabContentsWrapper* tab_contents);
+ virtual ~PerTabPrefsTabHelper();
+
+ PrefService* prefs() { return prefs_.get(); }
+
+ void OverrideWebPreferences(WebPreferences* prefs);
+
+ private:
+ void RegisterPerTabUserPrefs(PrefService* prefs);
+
+ // TabContentsObserver overrides:
+ virtual void TabContentsDestroyed(TabContents* tab) OVERRIDE;
+
+ // Our owning TabContentsWrapper.
+ TabContentsWrapper* wrapper_;
+
+ scoped_ptr<PrefService> prefs_;
+ PrefChangeRegistrar pref_change_registrar_;
+
+ DISALLOW_COPY_AND_ASSIGN(PerTabPrefsTabHelper);
+};
+
+#endif // CHROME_BROWSER_UI_TAB_CONTENTS_PER_TAB_PREFS_TAB_HELPER_H_
diff --git a/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper_unittest.cc b/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper_unittest.cc
new file mode 100644
index 0000000..3655686
--- /dev/null
+++ b/chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper_unittest.cc
@@ -0,0 +1,69 @@
+// Copyright (c) 2011 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.
+
+#include "chrome/common/pref_names.h"
+#include "chrome/browser/prefs/pref_service.h"
+#include "chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.h"
+#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
+#include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
+#include "content/browser/tab_contents/test_tab_contents.h"
+#include "content/test/test_browser_thread.h"
+
+using content::BrowserThread;
+
+class PerTabPrefsTabHelperTest : public TabContentsWrapperTestHarness {
+ public:
+ PerTabPrefsTabHelperTest()
+ : TabContentsWrapperTestHarness(),
+ ui_thread_(BrowserThread::UI, &message_loop_) {}
+
+ virtual ~PerTabPrefsTabHelperTest() {}
+
+ TabContentsWrapper* contents_wrapper2() {
+ return contents_wrapper2_.get();
+ }
+
+ void SetContents2(TestTabContents* contents) {
+ contents_wrapper2_.reset(
+ contents ? new TabContentsWrapper(contents) : NULL);
+ }
+
+ protected:
+ virtual void SetUp() OVERRIDE {
+ TabContentsWrapperTestHarness::SetUp();
+ SetContents2(CreateTestTabContents());
+ }
+
+ virtual void TearDown() OVERRIDE {
+ contents_wrapper2_.reset();
+ TabContentsWrapperTestHarness::TearDown();
+ }
+
+ private:
+ content::TestBrowserThread ui_thread_;
+ scoped_ptr<TabContentsWrapper> contents_wrapper2_;
+
+ DISALLOW_COPY_AND_ASSIGN(PerTabPrefsTabHelperTest);
+};
+
+TEST_F(PerTabPrefsTabHelperTest, PerTabJavaScriptEnabled) {
+ const char* key = prefs::kWebKitJavascriptEnabled;
+ PrefService* prefs1 = contents_wrapper()->per_tab_prefs_tab_helper()->prefs();
+ PrefService* prefs2 =
+ contents_wrapper2()->per_tab_prefs_tab_helper()->prefs();
+ const bool initial_value = prefs1->GetBoolean(key);
+ EXPECT_EQ(initial_value, prefs2->GetBoolean(key));
+
+ prefs1->SetBoolean(key, !initial_value);
+ EXPECT_EQ(!initial_value, prefs1->GetBoolean(key));
+ EXPECT_EQ(initial_value, prefs2->GetBoolean(key));
+
+ prefs1->SetBoolean(key, initial_value);
+ EXPECT_EQ(initial_value, prefs1->GetBoolean(key));
+ EXPECT_EQ(initial_value, prefs2->GetBoolean(key));
+
+ prefs2->SetBoolean(key, !initial_value);
+ EXPECT_EQ(initial_value, prefs1->GetBoolean(key));
+ EXPECT_EQ(!initial_value, prefs2->GetBoolean(key));
+}
diff --git a/chrome/browser/ui/tab_contents/tab_contents_wrapper.cc b/chrome/browser/ui/tab_contents/tab_contents_wrapper.cc
index 0413250..2c534ec 100644
--- a/chrome/browser/ui/tab_contents/tab_contents_wrapper.cc
+++ b/chrome/browser/ui/tab_contents/tab_contents_wrapper.cc
@@ -49,6 +49,7 @@
#include "chrome/browser/ui/intents/web_intent_picker_controller.h"
#include "chrome/browser/ui/sad_tab_observer.h"
#include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
+#include "chrome/browser/ui/tab_contents/per_tab_prefs_tab_helper.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper_delegate.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
@@ -84,8 +85,8 @@ const char* kPrefsToObserve[] = {
prefs::kWebKitDefaultFontSize,
prefs::kWebKitFantasyFontFamily,
prefs::kWebKitFixedFontFamily,
+ prefs::kWebKitGlobalJavascriptEnabled,
prefs::kWebKitJavaEnabled,
- prefs::kWebKitJavascriptEnabled,
prefs::kWebKitLoadsImagesAutomatically,
prefs::kWebKitMinimumFontSize,
prefs::kWebKitMinimumLogicalFontSize,
@@ -282,6 +283,7 @@ TabContentsWrapper::TabContentsWrapper(TabContents* contents)
password_manager_delegate_.reset(new PasswordManagerDelegateImpl(this));
password_manager_.reset(
new PasswordManager(contents, password_manager_delegate_.get()));
+ per_tab_prefs_tab_helper_.reset(new PerTabPrefsTabHelper(this));
prerender_tab_helper_.reset(new prerender::PrerenderTabHelper(this));
print_view_manager_.reset(new printing::PrintViewManager(this));
restore_tab_helper_.reset(new RestoreTabHelper(this));
@@ -306,8 +308,6 @@ TabContentsWrapper::TabContentsWrapper(TabContents* contents)
new ExtensionWebNavigationTabObserver(contents));
external_protocol_observer_.reset(new ExternalProtocolObserver(contents));
plugin_observer_.reset(new PluginObserver(this));
- per_tab_prefs_.reset(
- profile()->GetPrefs()->CreatePrefServiceWithPerTabPrefStore());
print_preview_.reset(new printing::PrintPreviewMessageHandler(contents));
sad_tab_observer_.reset(new SadTabObserver(contents));
// Start the in-browser thumbnailing if the feature is enabled.
@@ -374,7 +374,7 @@ void TabContentsWrapper::RegisterUserPrefs(PrefService* prefs) {
PrefService::SYNCABLE_PREF);
WebPreferences pref_defaults;
- prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
+ prefs->RegisterBooleanPref(prefs::kWebKitGlobalJavascriptEnabled,
pref_defaults.javascript_enabled,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
@@ -632,7 +632,9 @@ void TabContentsWrapper::Observe(int type,
case chrome::NOTIFICATION_PREF_CHANGED: {
std::string* pref_name_in = content::Details<std::string>(details).ptr();
DCHECK(content::Source<PrefService>(source).ptr() ==
- profile()->GetPrefs());
+ profile()->GetPrefs() ||
+ content::Source<PrefService>(source).ptr() ==
+ per_tab_prefs_tab_helper_->prefs());
if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
UpdateAlternateErrorPageURL(render_view_host());
} else if ((*pref_name_in == prefs::kDefaultCharset) ||
@@ -690,8 +692,9 @@ void TabContentsWrapper::UpdateAlternateErrorPageURL(RenderViewHost* rvh) {
void TabContentsWrapper::UpdateWebPreferences() {
RenderViewHostDelegate* rvhd = tab_contents();
- tab_contents()->render_view_host()->UpdateWebkitPreferences(
- rvhd->GetWebkitPrefs());
+ WebPreferences prefs = rvhd->GetWebkitPrefs();
+ per_tab_prefs_tab_helper_->OverrideWebPreferences(&prefs);
+ tab_contents()->render_view_host()->UpdateWebkitPreferences(prefs);
}
void TabContentsWrapper::UpdateRendererPreferences() {
diff --git a/chrome/browser/ui/tab_contents/tab_contents_wrapper.h b/chrome/browser/ui/tab_contents/tab_contents_wrapper.h
index 0844291..f912cf3 100644
--- a/chrome/browser/ui/tab_contents/tab_contents_wrapper.h
+++ b/chrome/browser/ui/tab_contents/tab_contents_wrapper.h
@@ -39,6 +39,7 @@ class NavigationController;
class OmniboxSearchHint;
class PasswordManager;
class PasswordManagerDelegate;
+class PerTabPrefsTabHelper;
class PluginObserver;
class PrefService;
class Profile;
@@ -179,6 +180,10 @@ class TabContentsWrapper : public TabContentsObserver,
InfoBarTabHelper* infobar_tab_helper() { return infobar_tab_helper_.get(); }
PasswordManager* password_manager() { return password_manager_.get(); }
+ PerTabPrefsTabHelper* per_tab_prefs_tab_helper() {
+ return per_tab_prefs_tab_helper_.get();
+ }
+
prerender::PrerenderTabHelper* prerender_tab_helper() {
return prerender_tab_helper_.get();
}
@@ -293,6 +298,7 @@ class TabContentsWrapper : public TabContentsObserver,
scoped_ptr<PasswordManagerDelegate> password_manager_delegate_;
scoped_ptr<PasswordManager> password_manager_;
+ scoped_ptr<PerTabPrefsTabHelper> per_tab_prefs_tab_helper_;
scoped_ptr<prerender::PrerenderTabHelper> prerender_tab_helper_;
// Handles print job for this contents.
@@ -324,7 +330,6 @@ class TabContentsWrapper : public TabContentsObserver,
scoped_ptr<ExtensionWebNavigationTabObserver> webnavigation_observer_;
scoped_ptr<ExternalProtocolObserver> external_protocol_observer_;
scoped_ptr<PluginObserver> plugin_observer_;
- scoped_ptr<PrefService> per_tab_prefs_; // Allows overriding user preferences.
scoped_ptr<printing::PrintPreviewMessageHandler> print_preview_;
scoped_ptr<SadTabObserver> sad_tab_observer_;
scoped_ptr<ThumbnailGenerator> thumbnail_generation_observer_;