create partitionId with encodedURIcomponent
This commit is contained in:
parent
da5bac42f3
commit
8f59c0b642
7 changed files with 37 additions and 41 deletions
|
@ -28,15 +28,17 @@ namespace atom {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
const base::FilePath::CharType kStoragePartitionDirName[] = "Partitions";
|
const base::FilePath::CharType kStoragePartitionDirname[] = "Partitions";
|
||||||
|
|
||||||
void GetStoragePartitionConfig(const GURL& partition,
|
void GetStoragePartitionConfig(const GURL& partition,
|
||||||
base::FilePath* partition_path,
|
base::FilePath* partition_path,
|
||||||
bool* in_memory,
|
bool* in_memory,
|
||||||
std::string* id) {
|
std::string* id) {
|
||||||
*in_memory = (partition.path() != "/persist");
|
*in_memory = (partition.path() != "/persist");
|
||||||
*id = partition.query();
|
net::UnescapeRule::Type flags =
|
||||||
*partition_path = base::FilePath(kStoragePartitionDirName).AppendASCII(*id);
|
net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS;
|
||||||
|
*id = net::UnescapeURLComponent(partition.query(), flags);
|
||||||
|
*partition_path = base::FilePath(kStoragePartitionDirname).AppendASCII(*id);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -55,7 +57,6 @@ AtomBrowserMainParts::AtomBrowserMainParts()
|
||||||
}
|
}
|
||||||
|
|
||||||
AtomBrowserMainParts::~AtomBrowserMainParts() {
|
AtomBrowserMainParts::~AtomBrowserMainParts() {
|
||||||
STLDeleteValues(&browser_context_map_);
|
|
||||||
for (const auto& callback : destruction_callbacks_)
|
for (const auto& callback : destruction_callbacks_)
|
||||||
callback.Run();
|
callback.Run();
|
||||||
}
|
}
|
||||||
|
@ -72,14 +73,13 @@ content::BrowserContext* AtomBrowserMainParts::GetBrowserContextForPartition(
|
||||||
bool in_memory;
|
bool in_memory;
|
||||||
base::FilePath partition_path;
|
base::FilePath partition_path;
|
||||||
GetStoragePartitionConfig(partition, &partition_path, &in_memory, &id);
|
GetStoragePartitionConfig(partition, &partition_path, &in_memory, &id);
|
||||||
auto item = browser_context_map_.find(id);
|
if (browser_context_map_.contains(id))
|
||||||
if (item != browser_context_map_.end())
|
return browser_context_map_.get(id);
|
||||||
return item->second;
|
|
||||||
|
|
||||||
auto browser_context = CreateBrowserContext();
|
scoped_ptr<brightray::BrowserContext> browser_context(CreateBrowserContext());
|
||||||
browser_context->Initialize(partition_path, in_memory);
|
browser_context->Initialize(partition_path.value(), in_memory);
|
||||||
browser_context_map_[id] = browser_context;
|
browser_context_map_.set(id, browser_context.Pass());
|
||||||
return browser_context;
|
return browser_context_map_.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AtomBrowserMainParts::RegisterDestructionCallback(
|
void AtomBrowserMainParts::RegisterDestructionCallback(
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "base/callback.h"
|
#include "base/callback.h"
|
||||||
|
#include "base/containers/scoped_ptr_hash_map.h"
|
||||||
#include "base/timer/timer.h"
|
#include "base/timer/timer.h"
|
||||||
#include "brightray/browser/browser_main_parts.h"
|
#include "brightray/browser/browser_main_parts.h"
|
||||||
#include "content/public/browser/browser_context.h"
|
#include "content/public/browser/browser_context.h"
|
||||||
|
@ -78,7 +79,8 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
|
||||||
std::list<base::Closure> destruction_callbacks_;
|
std::list<base::Closure> destruction_callbacks_;
|
||||||
|
|
||||||
// partition_id => browser_context
|
// partition_id => browser_context
|
||||||
std::map<std::string, content::BrowserContext*> browser_context_map_;
|
base::ScopedPtrHashMap<std::string, scoped_ptr<brightray::BrowserContext>>
|
||||||
|
browser_context_map_;
|
||||||
|
|
||||||
static AtomBrowserMainParts* self_;
|
static AtomBrowserMainParts* self_;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
crypto = require 'crypto'
|
|
||||||
ipc = require 'ipc'
|
ipc = require 'ipc'
|
||||||
webContents = require 'web-contents'
|
webContents = require 'web-contents'
|
||||||
webViewManager = null # Doesn't exist in early initialization.
|
webViewManager = null # Doesn't exist in early initialization.
|
||||||
|
@ -41,12 +40,16 @@ getNextInstanceId = (webContents) ->
|
||||||
|
|
||||||
# Generate URL encoded partition id.
|
# Generate URL encoded partition id.
|
||||||
getPartitionId = (partition) ->
|
getPartitionId = (partition) ->
|
||||||
persist = partition.startsWith('persist:')
|
|
||||||
# Guest site url will be chrome-guest://fake-host/{persist}?{partitionId}
|
# Guest site url will be chrome-guest://fake-host/{persist}?{partitionId}
|
||||||
partitionId = "chrome-guest://fake-host/"
|
partitionId = "chrome-guest://fake-host/"
|
||||||
if partition
|
if partition
|
||||||
partitionId += if persist then 'persist?' else '?'
|
persist = partition.startsWith('persist:')
|
||||||
partitionId += crypto.createHash('sha256').update(partition).digest('hex')
|
if persist
|
||||||
|
partition = partition.substring('persist:'.length)
|
||||||
|
partitionId += 'persist?'
|
||||||
|
else
|
||||||
|
partitionId += '?'
|
||||||
|
partitionId += encodeURIComponent(partition)
|
||||||
return partitionId
|
return partitionId
|
||||||
|
|
||||||
# Create a new guest instance.
|
# Create a new guest instance.
|
||||||
|
|
|
@ -142,7 +142,8 @@ Sets the storage partition used by the `webview`. If the storage partition ID st
|
||||||
the `webview` will use a persistent storage partition available to all `webview` in the app with
|
the `webview` will use a persistent storage partition available to all `webview` in the app with
|
||||||
the same storage partition ID. if there is no `persist:` prefix, the `webview` will
|
the same storage partition ID. if there is no `persist:` prefix, the `webview` will
|
||||||
use an in-memory storage partition. By assigning the same partition ID, multiple `webview`
|
use an in-memory storage partition. By assigning the same partition ID, multiple `webview`
|
||||||
can share the same storage partition.
|
can share the same storage partition. If the storage partition ID is unset then default storage
|
||||||
|
of the app will be used.
|
||||||
|
|
||||||
This value can only be modified before the first navigation, since the storage partition of an active
|
This value can only be modified before the first navigation, since the storage partition of an active
|
||||||
renderer process cannot change. Subsequent attempts to modify the value will fail with a
|
renderer process cannot change. Subsequent attempts to modify the value will fail with a
|
||||||
|
|
1
spec/fixtures/pages/partition/one.html
vendored
1
spec/fixtures/pages/partition/one.html
vendored
|
@ -1,5 +1,4 @@
|
||||||
<script>
|
<script>
|
||||||
window.localStorage.setItem('test', 'one');
|
|
||||||
const item = window.localStorage.getItem('test');
|
const item = window.localStorage.getItem('test');
|
||||||
console.log([item, window.localStorage.length].join(' '));
|
console.log([item, window.localStorage.length].join(' '));
|
||||||
</script>
|
</script>
|
||||||
|
|
4
spec/fixtures/pages/partition/two.html
vendored
4
spec/fixtures/pages/partition/two.html
vendored
|
@ -1,4 +0,0 @@
|
||||||
<script>
|
|
||||||
const item = window.localStorage.getItem('test');
|
|
||||||
console.log([item, window.localStorage.length].join(' '));
|
|
||||||
</script>
|
|
|
@ -155,30 +155,25 @@ describe '<webview> tag', ->
|
||||||
document.body.appendChild webview
|
document.body.appendChild webview
|
||||||
|
|
||||||
describe 'partition attribute', ->
|
describe 'partition attribute', ->
|
||||||
isolatedWebview = null
|
|
||||||
beforeEach ->
|
|
||||||
isolatedWebview = new WebView
|
|
||||||
|
|
||||||
afterEach ->
|
|
||||||
document.body.removeChild isolatedWebview
|
|
||||||
|
|
||||||
it 'isolates storage for different id', (done) ->
|
it 'isolates storage for different id', (done) ->
|
||||||
listener = (e) ->
|
listener = (e) ->
|
||||||
document.body.appendChild isolatedWebview
|
|
||||||
webview.removeEventListener 'did-finish-load', listener
|
|
||||||
listener2 = (e) ->
|
|
||||||
assert.equal e.message, "one 1"
|
|
||||||
webview.removeEventListener 'console-message', listener2
|
|
||||||
listener3 = (e) ->
|
|
||||||
assert.equal e.message, " 0"
|
assert.equal e.message, " 0"
|
||||||
isolatedWebview.removeEventListener 'console-message', listener3
|
webview.removeEventListener 'console-message', listener
|
||||||
done()
|
done()
|
||||||
webview.addEventListener 'did-finish-load', listener
|
window.localStorage.setItem 'test', 'one'
|
||||||
webview.addEventListener 'console-message', listener2
|
webview.addEventListener 'console-message', listener
|
||||||
|
webview.src = "file://#{fixtures}/pages/partition/one.html"
|
||||||
|
webview.partition = "test"
|
||||||
|
document.body.appendChild webview
|
||||||
|
|
||||||
|
it 'uses current session storage when no id is provided', (done) ->
|
||||||
|
listener = (e) ->
|
||||||
|
assert.equal e.message, "one 1"
|
||||||
|
webview.removeEventListener 'console-message', listener
|
||||||
|
done()
|
||||||
|
window.localStorage.setItem 'test', 'one'
|
||||||
|
webview.addEventListener 'console-message', listener
|
||||||
webview.src = "file://#{fixtures}/pages/partition/one.html"
|
webview.src = "file://#{fixtures}/pages/partition/one.html"
|
||||||
isolatedWebview.addEventListener 'console-message', listener3
|
|
||||||
isolatedWebview.setAttribute 'partition', 'test'
|
|
||||||
isolatedWebview.src = "file://#{fixtures}/pages/partition/two.html"
|
|
||||||
document.body.appendChild webview
|
document.body.appendChild webview
|
||||||
|
|
||||||
describe 'new-window event', ->
|
describe 'new-window event', ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue