Skip to content

Commit

Permalink
* 修复window.addEventListener("storage"无效的问题。这个在electron-ui的demo里用到。无效会…
Browse files Browse the repository at this point in the history
…导致换肤不了
  • Loading branch information
weolar committed Jul 10, 2018
1 parent 1ddbb1c commit 2d09dd7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
29 changes: 29 additions & 0 deletions third_party/WebKit/Source/web/WebStorageAreaImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

#include "third_party/WebKit/Source/web/WebStorageAreaImpl.h"
#include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "third_party/WebKit/Source/platform/weborigin/KURL.h"
#include "third_party/WebKit/Source/wtf/text/StringBuilder.h"
#include "content/web_impl_win/BlinkPlatformImpl.h"
#include "net/FileSystem.h"
#include "wtf/text/WTFStringUtil.h"

Expand Down Expand Up @@ -240,6 +242,11 @@ void WebStorageAreaImpl::setItem(const WebString& key, const WebString& value, c
pageStorageArea = it->value;

size_t sizeOld = pageStorageArea->size();

String oldValue;
HashMap<String, String>::iterator iter = pageStorageArea->find(keyString);
if (pageStorageArea->end() != iter)
oldValue = iter->value;
pageStorageArea->set(keyString, value);
size_t size = pageStorageArea->size();

Expand All @@ -251,6 +258,7 @@ void WebStorageAreaImpl::setItem(const WebString& key, const WebString& value, c
m_delaySaveTimer.startOneShot(0.5, FROM_HERE);

invalidateIterator();
dispatchStorageEvent(key, oldValue, value, pageUrl);
}

void WebStorageAreaImpl::removeItem(const WebString& key, const WebURL& pageUrl)
Expand All @@ -269,6 +277,11 @@ void WebStorageAreaImpl::removeItem(const WebString& key, const WebURL& pageUrl)
// String output = String::format("removeItem: %p %s %d\n", m_cachedArea, keyString.utf8().data(), size);
// OutputDebugStringA(output.utf8().data());

String oldValue;
HashMap<String, String>::iterator iter = pageStorageArea->find(keyString);
if (pageStorageArea->end() != iter)
oldValue = iter->value;

pageStorageArea->remove(keyString);
invalidateIterator();

Expand All @@ -277,6 +290,8 @@ void WebStorageAreaImpl::removeItem(const WebString& key, const WebURL& pageUrl)
delete it->value;
m_cachedArea->remove(it);
}

dispatchStorageEvent(key, oldValue, String(), pageUrl);
}

void WebStorageAreaImpl::clear(const WebURL& pageUrl)
Expand All @@ -292,6 +307,20 @@ void WebStorageAreaImpl::clear(const WebURL& pageUrl)
m_cachedArea->remove(it);

invalidateIterator();
dispatchStorageEvent(String(), String(), String(), pageUrl);
}

void WebStorageAreaImpl::dispatchStorageEvent(const String& key, const String& oldValue, const String& newValue, const WebURL& pageUrl)
{
if (oldValue == newValue)
return;

if (m_isLocal) {
WebStorageEventDispatcher::dispatchLocalStorageEvent(key, oldValue, newValue, KURL(ParsedURLString, m_origin), pageUrl, nullptr, true);
} else {
WebStorageNamespace* webStorageNamespace = ((content::BlinkPlatformImpl*)Platform::current())->createSessionStorageNamespace();
WebStorageEventDispatcher::dispatchSessionStorageEvent(key, oldValue, newValue, KURL(ParsedURLString, m_origin), pageUrl, *webStorageNamespace, nullptr, true);
}
}

void WebStorageAreaImpl::invalidateIterator()
Expand Down
8 changes: 2 additions & 6 deletions third_party/WebKit/Source/web/WebStorageAreaImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ class WebStorageAreaImpl : public WebStorageArea {

// See WebStorageArea.h for documentation on these functions.
virtual unsigned length();

virtual WebString key(unsigned index);

virtual WebString getItem(const WebString& key);

virtual void setItem(const WebString& key, const WebString& value, const WebURL& page_url, WebStorageArea::Result& result);

virtual void removeItem(const WebString& key, const WebURL& page_url);

virtual void clear(const WebURL& url);

virtual size_t memoryBytesUsedByCache() const;

private:
Expand All @@ -43,6 +37,8 @@ class WebStorageAreaImpl : public WebStorageArea {
void invalidateIterator();
void setIteratorToIndex(unsigned);

void dispatchStorageEvent(const String& key, const String& oldValue, const String& newValue, const WebURL& pageUrl);

String m_origin;
DOMStorageMap* m_cachedArea;
bool m_isLocal;
Expand Down

0 comments on commit 2d09dd7

Please sign in to comment.