summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/examples/api/preferences
diff options
context:
space:
mode:
authorbauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-19 10:05:50 +0000
committerbauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-19 10:05:50 +0000
commit7098b20361bb1e28a639aabe05de8c60bc09da94 (patch)
tree1fe219232bf5cbca232319a4265f827a252d160a /chrome/common/extensions/docs/examples/api/preferences
parentffab60baac4a0c0d7d1c70f3089ee392f55c87cf (diff)
downloadchromium_src-7098b20361bb1e28a639aabe05de8c60bc09da94.zip
chromium_src-7098b20361bb1e28a639aabe05de8c60bc09da94.tar.gz
chromium_src-7098b20361bb1e28a639aabe05de8c60bc09da94.tar.bz2
Add sample extension for preference access.
BUG=71067 TEST=none Review URL: http://codereview.chromium.org/6793042 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@82091 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions/docs/examples/api/preferences')
-rw-r--r--chrome/common/extensions/docs/examples/api/preferences/enableReferrer.zipbin0 -> 7276 bytes
-rw-r--r--chrome/common/extensions/docs/examples/api/preferences/enableReferrer/advicedog.jpgbin0 -> 2518 bytes
-rw-r--r--chrome/common/extensions/docs/examples/api/preferences/enableReferrer/manifest.json10
-rw-r--r--chrome/common/extensions/docs/examples/api/preferences/enableReferrer/popup.html129
4 files changed, 139 insertions, 0 deletions
diff --git a/chrome/common/extensions/docs/examples/api/preferences/enableReferrer.zip b/chrome/common/extensions/docs/examples/api/preferences/enableReferrer.zip
new file mode 100644
index 0000000..3669d90
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/preferences/enableReferrer.zip
Binary files differ
diff --git a/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/advicedog.jpg b/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/advicedog.jpg
new file mode 100644
index 0000000..9274fbd
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/advicedog.jpg
Binary files differ
diff --git a/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/manifest.json b/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/manifest.json
new file mode 100644
index 0000000..03b0e5a
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/manifest.json
@@ -0,0 +1,10 @@
+{
+ "name" : "Enable/disable referrers API example extension",
+ "version" : "0.1",
+ "description" : "Sample extension which demonstrates how to access a preference.",
+ "permissions": [ "experimental", "contentSettings" ],
+ "browser_action": {
+ "default_icon": "advicedog.jpg",
+ "popup": "popup.html"
+ }
+}
diff --git a/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/popup.html b/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/popup.html
new file mode 100644
index 0000000..43b5dae
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/preferences/enableReferrer/popup.html
@@ -0,0 +1,129 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <script>
+var cs = chrome.experimental.contentSettings;
+var pref = cs.misc.enableReferrers;
+
+/**
+ * Returns whether the |levelOfControl| means that the extension can change the
+ * preference value.
+ *
+ * @param levelOfControl{string}
+ */
+function settingIsControllable(levelOfControl) {
+ return (levelOfControl == "ControllableByThisExtension" ||
+ levelOfControl == "ControlledByThisExtension");
+}
+
+/**
+ * Updates the UI to reflect the state of the preference.
+ *
+ * @param settings{object} A settings object, as returned from |get()| or the
+ * |onchange| event.
+ */
+function updateUI(settings) {
+ var disableUI = !settingIsControllable(settings.levelOfControl);
+ document.getElementById("regularValue").disabled = disableUI;
+ document.getElementById("useSeparateIncognitoSettings").disabled = disableUI;
+ if (settings.hasOwnProperty('incognitoSpecific')) {
+ var hasIncognitoValue = settings.incognitoSpecific;
+ document.getElementById("useSeparateIncognitoSettings").checked =
+ hasIncognitoValue;
+ document.getElementById("incognitoValue").disabled =
+ disableUI || !hasIncognitoValue;
+ document.getElementById("incognitoValue").checked = settings.value;
+ } else {
+ document.getElementById("regularValue").checked = settings.value;
+ }
+}
+
+/**
+ * Wrapper for |updateUI| which is used as callback for the |get()| method and
+ * which logs the result.
+ * If there was an error getting the preference, does nothing.
+ *
+ * @param settings{object} A settings object, as returned from |get()|.
+ */
+function updateUIFromGet(settings) {
+ if (settings) {
+ console.log('pref.get result:' + JSON.stringify(settings));
+ updateUI(settings);
+ }
+}
+
+/**
+ * Wrapper for |updateUI| which is used as handler for the |onchange| event
+ * and which logs the result.
+ *
+ * @param settings{object} A settings object, as returned from the |onchange|
+ * event.
+ */
+function updateUIFromOnChange(settings) {
+ console.log('pref.onChange event:' + JSON.stringify(settings));
+ updateUI(settings);
+}
+
+/*
+ * Initializes the UI.
+ */
+function init() {
+ chrome.extension.isAllowedIncognitoAccess(function(allowed) {
+ if (allowed) {
+ pref.get({'incognito': true}, updateUIFromGet);
+ document.getElementById("incognito").style.display = "block";
+ document.getElementById("incognito-forbidden").style.display = "none";
+ }
+ });
+ pref.get({}, updateUIFromGet);
+ pref.onChange.addListener(updateUIFromOnChange);
+}
+
+/**
+ * Called from the UI to change the preference value.
+ *
+ * @param enabled{boolean} The new preference value.
+ * @param incognito{boolean} Whether the value is specific to incognito mode.
+ */
+function setPrefValue(enabled, incognito) {
+ pref.set({'value':enabled, 'incognito': incognito});
+}
+
+/**
+ * Called from the UI to change whether to use separate settings for
+ * incognito mode.
+ *
+ * @param value{boolean} whether to use separate settings for
+ * incognito mode.
+ */
+function setUseSeparateIncognitoSettings(value) {
+ if (!value) {
+ pref.clear({'incognito': true});
+ } else {
+ // Explicitly set the value for incognito mode.
+ pref.get({'incognito': true}, function(settings) {
+ pref.set({'incognito': true, 'value': settings.value});
+ });
+ }
+ document.getElementById("incognitoValue").disabled = !value;
+}
+
+ </script>
+</head>
+<body onload="init()">
+
+<div style="width: 300px">
+<input type="checkbox" onclick="setPrefValue(this.checked)" id="regularValue" /> Enable referrers
+
+<div id="incognito" style="display:none">
+<input type="checkbox" onclick="setUseSeparateIncognitoSettings(this.checked)" id="useSeparateIncognitoSettings" /> Use separate setting for incognito mode:
+<br>
+<input type="checkbox" onclick="setPrefValue(this.checked, true)" id="incognitoValue" disabled="disabled"/> Enable referrers in incognito sessions
+</div>
+<div id="incognito-forbidden">
+Select "Allow in incognito" to access incognito preferences
+</div>
+</div>
+
+</body>
+</html> \ No newline at end of file