- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / web_view / isolation / storage.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This method initializes the two types of DOM storage.
6 function initDomStorage(value) {
7   window.localStorage.setItem('foo', 'local-' + value);
8   window.sessionStorage.setItem('bar', 'session-' + value);
9 }
10
11 // The code below is used for testing IndexedDB isolation.
12 // The test uses three basic operations -- open, read, write -- to verify proper
13 // isolation across webview tags with different storage partitions.
14 // Each of the basic functions below sets document.title to a specific text,
15 // which the main browser test is waiting for. This is needed because all
16 // the functions get their results through callbacks and cannot return the
17 // values directly.
18 var isolation = {};
19 window.indexedDB = window.indexedDB || window.webkitIndexedDB;
20
21 isolation.db = null;
22 isolation.onerror = function(e) {
23   document.title = "error";
24 };
25
26 // This method opens the database and creates the objectStore if it doesn't
27 // exist. It sets the document.title to a string referring to which
28 // operation has been performed - open vs create.
29 function initIDB() {
30   var v = 3;
31   var ranVersionChangeTransaction = false;
32   var request = indexedDB.open("isolation", v);
33   request.onupgradeneeded = function(e) {
34     isolation.db = e.target.result;
35     var store = isolation.db.createObjectStore(
36         "partitions", {keyPath: "id"});
37     e.target.transaction.oncomplete = function() {
38       ranVersionChangeTransaction = true;
39     }
40   }
41   request.onsuccess = function(e) {
42     isolation.db = e.target.result;
43     if (ranVersionChangeTransaction) {
44       document.title = "idb created";
45     } else {
46       document.title = "idb open";
47     }
48   };
49   request.onerror = isolation.onerror;
50   request.onblocked = isolation.onerror;
51 }
52
53 // This method adds a |value| to the database identified by |id|.
54 function addItemIDB(id, value) {
55   var trans = isolation.db.transaction(["partitions"], "readwrite");
56   var store = trans.objectStore("partitions");
57   var data = { "partition": value, "id": id };
58
59   var request = store.put(data);
60   request.onsuccess = function(e) {
61     document.title = "addItemIDB complete";
62   };
63   request.onerror = isolation.onerror;
64 };
65
66 var storedValue = null;
67
68 // This method reads an item from the database, identified by |id|. Since
69 // the value cannot be returned directly, it is saved into the global
70 // "storedValue" variable, which is then read through getValueIDB().
71 function readItemIDB(id) {
72   storedValue = null;
73   var trans = isolation.db.transaction(["partitions"], "readwrite");
74   var store = trans.objectStore("partitions");
75
76   var request = store.get(id);
77   request.onsuccess = function(e) {
78     if (!!e.target.result != false) {
79       storedValue = request.result.partition;
80     }
81     document.title = "readItemIDB complete";
82   };
83   request.onerror = isolation.onerror;
84 }
85
86 function getValueIDB() {
87   return storedValue;
88 }