summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-17 00:35:21 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-17 00:35:21 +0000
commitc06fa698843c03485193a433ec12acd352135db7 (patch)
tree2301119b2ee586e96a9eafe3f5c5d40e54282ad3
parentcd7551d4c24d1c8b02eca3918157e8fd5230c877 (diff)
downloadchromium_src-c06fa698843c03485193a433ec12acd352135db7.zip
chromium_src-c06fa698843c03485193a433ec12acd352135db7.tar.gz
chromium_src-c06fa698843c03485193a433ec12acd352135db7.tar.bz2
Add a simple cookies sample.
Review URL: http://codereview.chromium.org/2824058 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52783 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/common/extensions/docs/cookies.html19
-rw-r--r--chrome/common/extensions/docs/examples/api/cookies/background.html33
-rw-r--r--chrome/common/extensions/docs/examples/api/cookies/cookie.pngbin0 -> 719 bytes
-rw-r--r--chrome/common/extensions/docs/examples/api/cookies/manager.html297
-rw-r--r--chrome/common/extensions/docs/examples/api/cookies/manifest.json11
-rw-r--r--chrome/common/extensions/docs/static/cookies.html12
6 files changed, 372 insertions, 0 deletions
diff --git a/chrome/common/extensions/docs/cookies.html b/chrome/common/extensions/docs/cookies.html
index e4cf300..704a336 100644
--- a/chrome/common/extensions/docs/cookies.html
+++ b/chrome/common/extensions/docs/cookies.html
@@ -222,6 +222,13 @@
<a>h3Name</a>
</li>
</ol>
+ </li><li>
+ <a href="#examples"> Examples </a>
+ <ol>
+ <li style="display: none; ">
+ <a>h3Name</a>
+ </li>
+ </ol>
</li>
<li>
<a href="#apiReference">API reference: chrome.cookies</a>
@@ -295,6 +302,18 @@ For example:</p>
...
}</pre>
+<h2 id="examples"> Examples </h2>
+
+<p>
+You can find a simple example
+of using the cookies API in the
+<a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/cookies/">examples/api/cookies</a>
+directory.
+For other examples
+and for help in viewing the source code,
+see <a href="samples.html">Samples</a>.
+</p>
+
<!-- END AUTHORED CONTENT -->
</div>
diff --git a/chrome/common/extensions/docs/examples/api/cookies/background.html b/chrome/common/extensions/docs/examples/api/cookies/background.html
new file mode 100644
index 0000000..8cf522f6
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/cookies/background.html
@@ -0,0 +1,33 @@
+<script>
+
+chrome.cookies.onChanged.addListener(function(info) {
+ console.log("onChanged" + JSON.stringify(info));
+});
+
+function focusOrCreateTab(url) {
+ chrome.windows.getAll({"populate":true}, function(windows) {
+ var existing_tab = null;
+ for (var i in windows) {
+ var tabs = windows[i].tabs;
+ for (var j in tabs) {
+ var tab = tabs[j];
+ if (tab.url == url) {
+ existing_tab = tab;
+ break;
+ }
+ }
+ }
+ if (existing_tab) {
+ chrome.tabs.update(existing_tab.id, {"selected":true});
+ } else {
+ chrome.tabs.create({"url":url, "selected":true});
+ }
+ });
+}
+
+chrome.browserAction.onClicked.addListener(function(tab) {
+ var manager_url = chrome.extension.getURL("manager.html");
+ focusOrCreateTab(manager_url);
+});
+
+</script>
diff --git a/chrome/common/extensions/docs/examples/api/cookies/cookie.png b/chrome/common/extensions/docs/examples/api/cookies/cookie.png
new file mode 100644
index 0000000..ca72eca
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/cookies/cookie.png
Binary files differ
diff --git a/chrome/common/extensions/docs/examples/api/cookies/manager.html b/chrome/common/extensions/docs/examples/api/cookies/manager.html
new file mode 100644
index 0000000..8c644c9
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/cookies/manager.html
@@ -0,0 +1,297 @@
+<html>
+<head>
+<style>
+table {
+ border-collapse:collapse;
+}
+
+td {
+ border: 1px solid black;
+ padding-left: 5px;
+}
+
+td.button {
+ border: none;
+}
+
+td.cookie_count {
+ text-align: right;
+}
+
+</style>
+
+<script>
+
+if (!chrome.cookies) {
+ chrome.cookies = chrome.experimental.cookies;
+}
+
+// A simple Timer class.
+function Timer() {
+ this.start_ = new Date();
+
+ this.elapsed = function() {
+ return (new Date()) - this.start_;
+ }
+
+ this.reset = function() {
+ this.start_ = new Date();
+ }
+}
+
+// Compares cookies for "key" (name, domain, etc.) equality, but not "value"
+// equality.
+function cookieMatch(c1, c2) {
+ return (c1.name == c2.name) && (c1.domain == c2.domain) &&
+ (c1.hostOnly == c2.hostOnly) && (c1.path == c2.path) &&
+ (c1.secure == c2.secure) && (c1.httpOnly == c2.httpOnly) &&
+ (c1.session == c2.session) && (c1.storeId == c2.storeId);
+}
+
+// Returns an array of sorted keys from an associative array.
+function sortedKeys(array) {
+ var keys = [];
+ for (var i in array) {
+ keys.push(i);
+ }
+ keys.sort();
+ return keys;
+}
+
+// Shorthand for document.querySelector.
+function select(selector) {
+ return document.querySelector(selector);
+}
+
+// An object used for caching data about the browser's cookies, which we update
+// as notifications come in.
+function CookieCache() {
+ this.cookies_ = {};
+
+ this.reset = function() {
+ this.cookies_ = {};
+ }
+
+ this.add = function(cookie) {
+ var domain = cookie.domain;
+ if (!this.cookies_[domain]) {
+ this.cookies_[domain] = [];
+ }
+ this.cookies_[domain].push(cookie);
+ };
+
+ this.remove = function(cookie) {
+ var domain = cookie.domain;
+ if (this.cookies_[domain]) {
+ var i = 0;
+ while (i < this.cookies_[domain].length) {
+ if (cookieMatch(this.cookies_[domain][i], cookie)) {
+ this.cookies_[domain].splice(i, 1);
+ } else {
+ i++;
+ }
+ }
+ if (this.cookies_[domain].length == 0) {
+ delete this.cookies_[domain];
+ }
+ }
+ };
+
+ // Returns a sorted list of cookie domains that match |filter|. If |filter| is
+ // null, returns all domains.
+ this.getDomains = function(filter) {
+ var result = [];
+ sortedKeys(this.cookies_).forEach(function(domain) {
+ if (!filter || domain.indexOf(filter) != -1) {
+ result.push(domain);
+ }
+ });
+ return result;
+ }
+
+ this.getCookies = function(domain) {
+ return this.cookies_[domain];
+ };
+}
+
+
+var cache = new CookieCache();
+
+
+function removeAllForFilter() {
+ var filter = select("#filter").value;
+ var timer = new Timer();
+ cache.getDomains(filter).forEach(function(domain) {
+ removeCookiesForDomain(domain);
+ });
+}
+
+function removeAll() {
+ var all_cookies = [];
+ cache.getDomains().forEach(function(domain) {
+ cache.getCookies(domain).forEach(function(cookie) {
+ all_cookies.push(cookie);
+ });
+ });
+ cache.reset();
+ var count = all_cookies.length;
+ var timer = new Timer();
+ for (var i = 0; i < count; i++) {
+ removeCookie(all_cookies[i]);
+ }
+ timer.reset();
+ chrome.cookies.getAll({}, function(cookies) {
+ for (var i in cookies) {
+ cache.add(cookies[i]);
+ removeCookie(cookies[i]);
+ }
+ });
+}
+
+function removeCookie(cookie) {
+ var url = "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain +
+ cookie.path;
+ chrome.cookies.remove({"url": url, "name": cookie.name});
+}
+
+function removeCookiesForDomain(domain) {
+ var timer = new Timer();
+ cache.getCookies(domain).forEach(function(cookie) {
+ removeCookie(cookie);
+ });
+}
+
+function resetTable() {
+ var table = select("#cookies");
+ while (table.rows.length > 1) {
+ table.deleteRow(table.rows.length - 1);
+ }
+}
+
+var reload_scheduled = false;
+
+function scheduleReloadCookieTable() {
+ if (!reload_scheduled) {
+ reload_scheduled = true;
+ setTimeout(reloadCookieTable, 250);
+ }
+}
+
+function reloadCookieTable() {
+ reload_scheduled = false;
+
+ var filter = select("#filter").value;
+
+ var domains = cache.getDomains(filter);
+
+ select("#filter_count").innerText = domains.length;
+ select("#total_count").innerText = cache.getDomains().length;
+
+ select("#delete_all_button").innerHTML = "";
+ if (domains.length) {
+ var button = document.createElement("button");
+ button.onclick = removeAllForFilter;
+ button.innerText = "delete all " + domains.length;
+ select("#delete_all_button").appendChild(button);
+ }
+
+ resetTable();
+ var table = select("#cookies");
+
+ domains.forEach(function(domain) {
+ var cookies = cache.getCookies(domain);
+ var row = table.insertRow(-1);
+ row.insertCell(-1).innerText = domain;
+ var cell = row.insertCell(-1);
+ cell.innerText = cookies.length;
+ cell.setAttribute("class", "cookie_count");
+
+ var button = document.createElement("button");
+ button.innerText = "delete";
+ button.onclick = (function(dom){
+ return function() {
+ removeCookiesForDomain(dom);
+ };
+ }(domain));
+ var cell = row.insertCell(-1);
+ cell.appendChild(button);
+ cell.setAttribute("class", "button");
+ });
+}
+
+function focusFilter() {
+ select("#filter").focus();
+}
+
+function resetFilter() {
+ var filter = select("#filter");
+ filter.focus();
+ if (filter.value.length > 0) {
+ filter.value = "";
+ reloadCookieTable();
+ }
+}
+
+var ESCAPE_KEY = 27;
+window.onkeydown = function(event) {
+ if (event.keyCode == ESCAPE_KEY) {
+ resetFilter();
+ }
+}
+
+function listener(info) {
+ cache.remove(info.cookie);
+ if (!info.removed) {
+ cache.add(info.cookie);
+ }
+ scheduleReloadCookieTable();
+}
+
+function startListening() {
+ chrome.cookies.onChanged.addListener(listener);
+}
+
+function stopListening() {
+ chrome.cookies.onChanged.removeListener(listener);
+}
+
+function onload() {
+ focusFilter();
+ var timer = new Timer();
+ chrome.cookies.getAll({}, function(cookies) {
+ startListening();
+ start = new Date();
+ for (var i in cookies) {
+ cache.add(cookies[i]);
+ }
+ timer.reset();
+ reloadCookieTable();
+ });
+}
+
+
+</script>
+</head>
+
+<body onload="onload()" onclick="focusFilter()">
+<h2>Cookies! ... Nom Nom Nom...</h2>
+<button onclick="removeAll()">DELETE ALL!</button>
+<div id="filter_div">
+Filter: <input id="filter" type="text" oninput="reloadCookieTable()">
+<button onclick="resetFilter()">x</button>
+</div>
+<br>
+<div id="summary_div">
+Showing <span id="filter_count"></span> of <span id="total_count"></span> cookie domains.
+<span id="delete_all_button"></span>
+</div>
+<br>
+<table id="cookies">
+<tr class="header">
+<th>Name</th>
+<th>#Cookies</th>
+</tr>
+</table>
+
+</body>
+</html>
diff --git a/chrome/common/extensions/docs/examples/api/cookies/manifest.json b/chrome/common/extensions/docs/examples/api/cookies/manifest.json
new file mode 100644
index 0000000..3abe984
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/cookies/manifest.json
@@ -0,0 +1,11 @@
+{
+ "name" : "Cookie API Test Extension",
+ "version" : "0.7",
+ "description" : "Testing Cookie API",
+ "permissions": [ "cookies", "tabs", "http://*/*", "https://*/*" ],
+ "icons": { "16": "cookie.png", "48": "cookie.png", "128": "cookie.png" },
+ "browser_action": {
+ "default_icon": "cookie.png"
+ },
+ "background_page": "background.html"
+}
diff --git a/chrome/common/extensions/docs/static/cookies.html b/chrome/common/extensions/docs/static/cookies.html
index b74bdfe..a24196d 100644
--- a/chrome/common/extensions/docs/static/cookies.html
+++ b/chrome/common/extensions/docs/static/cookies.html
@@ -18,4 +18,16 @@ For example:</p>
...
}</pre>
+<h2 id="examples"> Examples </h2>
+
+<p>
+You can find a simple example
+of using the cookies API in the
+<a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/cookies/">examples/api/cookies</a>
+directory.
+For other examples
+and for help in viewing the source code,
+see <a href="samples.html">Samples</a>.
+</p>
+
<!-- END AUTHORED CONTENT -->