Pass partition name instead of path to BrowserContext

This commit is contained in:
Cheng Zhao 2015-09-05 19:47:44 +08:00
parent f2bdca31b3
commit 3773f81fd5
7 changed files with 35 additions and 56 deletions

View file

@ -161,19 +161,21 @@ WebContents::WebContents(v8::Isolate* isolate,
type_ = is_guest ? WEB_VIEW : BROWSER_WINDOW; type_ = is_guest ? WEB_VIEW : BROWSER_WINDOW;
// TODO(zcbenz): Use host's BrowserContext when no partition is specified.
content::BrowserContext* browser_context = content::BrowserContext* browser_context =
AtomBrowserMainParts::Get()->browser_context(); AtomBrowserMainParts::Get()->browser_context();
content::WebContents* web_contents; content::WebContents* web_contents;
if (is_guest) { if (is_guest) {
GURL guest_site; std::string partition;
options.Get("partition", &guest_site); bool in_memory = false;
// use hosts' browser_context when no partition is specified. options.Get("partition", &partition);
if (!guest_site.query().empty()) { options.Get("inMemory", &in_memory);
if (!partition.empty()) {
browser_context = AtomBrowserMainParts::Get() browser_context = AtomBrowserMainParts::Get()
->GetBrowserContextForPartition(guest_site); ->GetBrowserContextForPartition(partition, in_memory);
} }
auto site_instance = content::SiteInstance* site_instance = content::SiteInstance::CreateForURL(
content::SiteInstance::CreateForURL(browser_context, guest_site); browser_context, GURL("chrome-guest://fake-host"));
content::WebContents::CreateParams params(browser_context, site_instance); content::WebContents::CreateParams params(browser_context, site_instance);
guest_delegate_.reset(new WebViewGuestDelegate); guest_delegate_.reset(new WebViewGuestDelegate);
params.guest_delegate = guest_delegate_.get(); params.guest_delegate = guest_delegate_.get();
@ -190,7 +192,7 @@ WebContents::WebContents(v8::Isolate* isolate,
// Save the preferences. // Save the preferences.
base::DictionaryValue web_preferences; base::DictionaryValue web_preferences;
mate::ConvertFromV8(isolate, options.GetHandle(), &web_preferences); mate::ConvertFromV8(isolate, options.GetHandle(), &web_preferences);
new WebContentsPreferences(web_contents, std::move(web_preferences)); new WebContentsPreferences(web_contents, &web_preferences);
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent()); web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());

View file

@ -26,24 +26,6 @@
namespace atom { namespace atom {
namespace {
const base::FilePath::CharType kStoragePartitionDirname[] =
FILE_PATH_LITERAL("Partitions");
void GetStoragePartitionConfig(const GURL& partition,
base::FilePath* partition_path,
bool* in_memory,
std::string* id) {
*in_memory = (partition.path() != "/persist");
net::UnescapeRule::Type flags =
net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS;
*id = net::UnescapeURLComponent(partition.query(), flags);
*partition_path = base::FilePath(kStoragePartitionDirname).AppendASCII(*id);
}
} // namespace
// static // static
AtomBrowserMainParts* AtomBrowserMainParts::self_ = NULL; AtomBrowserMainParts* AtomBrowserMainParts::self_ = NULL;
@ -69,18 +51,20 @@ AtomBrowserMainParts* AtomBrowserMainParts::Get() {
} }
content::BrowserContext* AtomBrowserMainParts::GetBrowserContextForPartition( content::BrowserContext* AtomBrowserMainParts::GetBrowserContextForPartition(
const GURL& partition) { const std::string& partition, bool in_memory) {
std::string id; if (browser_context_map_.contains(partition))
bool in_memory; return browser_context_map_.get(partition);
base::FilePath partition_path;
GetStoragePartitionConfig(partition, &partition_path, &in_memory, &id);
if (browser_context_map_.contains(id))
return browser_context_map_.get(id);
scoped_ptr<brightray::BrowserContext> browser_context(CreateBrowserContext()); scoped_ptr<brightray::BrowserContext> browser_context(CreateBrowserContext());
<<<<<<< HEAD
browser_context->Initialize(partition_path.AsUTF8Unsafe(), in_memory); browser_context->Initialize(partition_path.AsUTF8Unsafe(), in_memory);
browser_context_map_.set(id, browser_context.Pass()); browser_context_map_.set(id, browser_context.Pass());
return browser_context_map_.get(id); return browser_context_map_.get(id);
=======
browser_context->Initialize(partition, in_memory);
browser_context_map_.set(partition, browser_context.Pass());
return browser_context_map_.get(partition);
>>>>>>> Pass partition name instead of path to BrowserContext
} }
void AtomBrowserMainParts::RegisterDestructionCallback( void AtomBrowserMainParts::RegisterDestructionCallback(

View file

@ -35,7 +35,7 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
// Returns the BrowserContext associated with the partition. // Returns the BrowserContext associated with the partition.
content::BrowserContext* GetBrowserContextForPartition( content::BrowserContext* GetBrowserContextForPartition(
const GURL& partition); const std::string& partition, bool in_memory);
// Register a callback that should be destroyed before JavaScript environment // Register a callback that should be destroyed before JavaScript environment
// gets destroyed. // gets destroyed.

View file

@ -38,29 +38,21 @@ moveLastToFirst = (list) ->
getNextInstanceId = (webContents) -> getNextInstanceId = (webContents) ->
++nextInstanceId ++nextInstanceId
# Generate URL encoded partition id. # Parse the partition string.
getPartitionId = (partition) -> parsePartition = (partition) ->
# Guest site url will be chrome-guest://fake-host/{persist}?{partitionId} return ['', false] unless partition
partitionId = "chrome-guest://fake-host/" if partition.startsWith 'persist:'
if partition [partition.substr('persist:'.length), false]
persist = partition.startsWith('persist:') else
if persist [partition, true]
partition = partition.substring('persist:'.length)
partitionId += 'persist?'
else
# Just to differentiate from same persistant ID
partition += "_temp"
partitionId += '?'
partitionId += encodeURIComponent(partition)
return partitionId
# Create a new guest instance. # Create a new guest instance.
createGuest = (embedder, params) -> createGuest = (embedder, params) ->
webViewManager ?= process.atomBinding 'web_view_manager' webViewManager ?= process.atomBinding 'web_view_manager'
id = getNextInstanceId embedder id = getNextInstanceId embedder
partitionId = getPartitionId params.partition [partition, inMemory] = parsePartition params.partition
guest = webContents.create {isGuest: true, partition: partitionId, embedder} guest = webContents.create {isGuest: true, partition, inMemory, embedder}
guestInstances[id] = {guest, embedder} guestInstances[id] = {guest, embedder}
# Destroy guest when the embedder is gone or navigated. # Destroy guest when the embedder is gone or navigated.
@ -138,7 +130,6 @@ attachGuest = (embedder, elementInstanceId, guestInstanceId, params) ->
'plugins': params.plugins 'plugins': params.plugins
'web-security': !params.disablewebsecurity 'web-security': !params.disablewebsecurity
webPreferences['preload-url'] = params.preload if params.preload webPreferences['preload-url'] = params.preload if params.preload
webPreferences['partition'] = getPartitionId(params.partition) if params.partition
webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest, webPreferences webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest, webPreferences
guest.attachParams = params guest.attachParams = params

View file

@ -4,6 +4,8 @@
#include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_contents_preferences.h"
#include <string>
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
@ -36,8 +38,8 @@ const char* kWebRuntimeFeatures[] = {
WebContentsPreferences::WebContentsPreferences( WebContentsPreferences::WebContentsPreferences(
content::WebContents* web_contents, content::WebContents* web_contents,
base::DictionaryValue&& web_preferences) { base::DictionaryValue* web_preferences) {
web_preferences_.Swap(&web_preferences); web_preferences_.Swap(web_preferences);
web_contents->SetUserData(kWebPreferencesKey, this); web_contents->SetUserData(kWebPreferencesKey, this);
} }

View file

@ -34,7 +34,7 @@ class WebContentsPreferences
content::WebContents* web_contents, content::WebPreferences* prefs); content::WebContents* web_contents, content::WebPreferences* prefs);
WebContentsPreferences(content::WebContents* web_contents, WebContentsPreferences(content::WebContents* web_contents,
base::DictionaryValue&& web_preferences); base::DictionaryValue* web_preferences);
~WebContentsPreferences() override; ~WebContentsPreferences() override;
// $.extend(|web_preferences_|, |new_web_preferences|). // $.extend(|web_preferences_|, |new_web_preferences|).

2
vendor/brightray vendored

@ -1 +1 @@
Subproject commit 4d8f5d879d484db54895c3456ded3a3d3246415d Subproject commit 55bab304fd1c92a79e636bca0313f7cbda000c06