1 /* 2 * Copyright (C) 2009 Google Inc. All Rights Reserved. 3 * (C) 2008 Apple Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "StorageAreaProxy.h" 29 30 #if ENABLE(DOM_STORAGE) 31 32 #include "DOMWindow.h" 33 #include "Document.h" 34 #include "EventNames.h" 35 #include "ExceptionCode.h" 36 #include "Frame.h" 37 #include "Page.h" 38 #include "PageGroup.h" 39 #include "SecurityOrigin.h" 40 #include "StorageAreaImpl.h" 41 #include "StorageEvent.h" 42 43 #include "WebStorageArea.h" 44 #include "WebString.h" 45 #include "WebURL.h" 46 47 namespace WebCore { 48 49 StorageAreaProxy::StorageAreaProxy(WebKit::WebStorageArea* storageArea, StorageType storageType) 50 : m_storageArea(storageArea) 51 , m_storageType(storageType) 52 { 53 } 54 55 StorageAreaProxy::~StorageAreaProxy() 56 { 57 } 58 59 unsigned StorageAreaProxy::length() const 60 { 61 return m_storageArea->length(); 62 } 63 64 String StorageAreaProxy::key(unsigned index) const 65 { 66 return m_storageArea->key(index); 67 } 68 69 String StorageAreaProxy::getItem(const String& key) const 70 { 71 return m_storageArea->getItem(key); 72 } 73 74 String StorageAreaProxy::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame) 75 { 76 bool quotaException = false; 77 WebKit::WebString oldValue; 78 m_storageArea->setItem(key, value, frame->document()->url(), quotaException, oldValue); 79 ec = quotaException ? QUOTA_EXCEEDED_ERR : 0; 80 String oldValueString = oldValue; 81 if (oldValueString != value) 82 storageEvent(key, oldValue, value, m_storageType, frame->document()->securityOrigin(), frame); 83 return oldValue; 84 } 85 86 String StorageAreaProxy::removeItem(const String& key, Frame* frame) 87 { 88 WebKit::WebString oldValue; 89 m_storageArea->removeItem(key, frame->document()->url(), oldValue); 90 if (!oldValue.isNull()) 91 storageEvent(key, oldValue, String(), m_storageType, frame->document()->securityOrigin(), frame); 92 return oldValue; 93 } 94 95 bool StorageAreaProxy::clear(Frame* frame) 96 { 97 bool clearedSomething; 98 m_storageArea->clear(frame->document()->url(), clearedSomething); 99 if (clearedSomething) 100 storageEvent(String(), String(), String(), m_storageType, frame->document()->securityOrigin(), frame); 101 return clearedSomething; 102 } 103 104 bool StorageAreaProxy::contains(const String& key) const 105 { 106 return !getItem(key).isNull(); 107 } 108 109 // Copied from WebCore/storage/StorageEventDispatcher.cpp out of necessity. It's probably best to keep it current. 110 void StorageAreaProxy::storageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin, Frame* sourceFrame) 111 { 112 Page* page = sourceFrame->page(); 113 if (!page) 114 return; 115 116 // We need to copy all relevant frames from every page to a vector since sending the event to one frame might mutate the frame tree 117 // of any given page in the group or mutate the page group itself. 118 Vector<RefPtr<Frame> > frames; 119 if (storageType == SessionStorage) { 120 // Send events only to our page. 121 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) { 122 if (sourceFrame != frame && frame->document()->securityOrigin()->equal(securityOrigin)) 123 frames.append(frame); 124 } 125 126 for (unsigned i = 0; i < frames.size(); ++i) 127 frames[i]->document()->enqueueStorageEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), frames[i]->domWindow()->sessionStorage())); 128 } else { 129 // Send events to every page. 130 const HashSet<Page*>& pages = page->group().pages(); 131 HashSet<Page*>::const_iterator end = pages.end(); 132 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) { 133 for (Frame* frame = (*it)->mainFrame(); frame; frame = frame->tree()->traverseNext()) { 134 if (sourceFrame != frame && frame->document()->securityOrigin()->equal(securityOrigin)) 135 frames.append(frame); 136 } 137 } 138 139 for (unsigned i = 0; i < frames.size(); ++i) 140 frames[i]->document()->enqueueStorageEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), frames[i]->domWindow()->localStorage())); 141 } 142 } 143 144 } // namespace WebCore 145 146 #endif // ENABLE(DOM_STORAGE) 147