Index: chrome/browser/profiles/profile_impl_io_data.cc |
diff --git a/chrome/browser/profiles/profile_impl_io_data.cc b/chrome/browser/profiles/profile_impl_io_data.cc |
index 01d81017a39d2853fdec2621d6b50750b757f6fe..4552ef676dae24fc990a9f45706948ce17c62f2c 100644 |
--- a/chrome/browser/profiles/profile_impl_io_data.cc |
+++ b/chrome/browser/profiles/profile_impl_io_data.cc |
@@ -6,6 +6,7 @@ |
#include "base/command_line.h" |
#include "base/logging.h" |
+#include "base/stl_util-inl.h" |
#include "chrome/browser/browser_process.h" |
#include "chrome/browser/io_thread.h" |
#include "chrome/browser/net/chrome_cookie_policy.h" |
@@ -15,6 +16,8 @@ |
#include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
#include "chrome/common/chrome_constants.h" |
#include "chrome/common/chrome_switches.h" |
+#include "chrome/common/extensions/extension.h" |
+#include "chrome/common/pref_names.h" |
#include "chrome/common/url_constants.h" |
#include "content/browser/browser_thread.h" |
#include "net/ftp/ftp_network_layer.h" |
@@ -36,6 +39,14 @@ ProfileImplIOData::Handle::~Handle() { |
media_request_context_getter_->CleanupOnUIThread(); |
if (extensions_request_context_getter_) |
extensions_request_context_getter_->CleanupOnUIThread(); |
+ |
+ // Clean up all isolated app request contexts. |
+ for (ChromeURLRequestContextGetterMap::iterator iter = |
+ app_request_context_getter_map_.begin(); |
+ iter != app_request_context_getter_map_.end(); |
+ ++iter) { |
+ iter->second->CleanupOnUIThread(); |
+ } |
} |
void ProfileImplIOData::Handle::Init(const FilePath& cookie_path, |
@@ -60,6 +71,32 @@ void ProfileImplIOData::Handle::Init(const FilePath& cookie_path, |
io_data_->lazy_params_.reset(lazy_params); |
} |
+void ProfileImplIOData::Handle::InitIsolatedApp(const Extension* installed_app, |
+ const FilePath& cookie_path, |
+ const FilePath& cache_path, |
+ int cache_max_size) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ // This may be called multiple times before InitializeAppRequestContext. |
+ // Only do the initialization once. |
+ std::string id = installed_app->id(); |
+ if (io_data_->lazy_params_map_.find(id) != io_data_->lazy_params_map_.end()) |
+ return; |
+ |
+ // We only need to keep track of parameters that differ from the main context. |
+ LazyParams* lazy_params = new LazyParams; |
+ lazy_params->cookie_path = cookie_path; |
+ lazy_params->cache_path = cache_path; |
+ lazy_params->cache_max_size = cache_max_size; |
+ |
+ // Needed for the cookie store. |
+ lazy_params->profile_params.clear_local_state_on_exit = |
+ profile_->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit); |
+ |
+ // Keep track of this app's LazyParams until InitializeAppRequestContext. |
+ io_data_->lazy_params_map_[id] = lazy_params; |
+} |
+ |
scoped_refptr<ChromeURLRequestContextGetter> |
ProfileImplIOData::Handle::GetMainRequestContextGetter() const { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
@@ -96,6 +133,28 @@ ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const { |
return extensions_request_context_getter_; |
} |
+scoped_refptr<ChromeURLRequestContextGetter> |
+ProfileImplIOData::Handle::GetIsolatedAppRequestContextGetter( |
+ const Extension* installed_app) const { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ CHECK(installed_app); |
+ LazyInitialize(); |
+ |
+ // Keep a map of request context getters, one per requested app ID. |
+ std::string id = installed_app->id(); |
+ ChromeURLRequestContextGetterMap::iterator iter = |
+ app_request_context_getter_map_.find(id); |
+ if (iter != app_request_context_getter_map_.end()) |
+ return iter->second; |
+ |
+ ChromeURLRequestContextGetter* context = |
+ ChromeURLRequestContextGetter::CreateOriginalForIsolatedApp( |
+ profile_, io_data_, installed_app); |
+ app_request_context_getter_map_[id] = context; |
+ |
+ return context; |
+} |
+ |
void ProfileImplIOData::Handle::LazyInitialize() const { |
if (!initialized_) { |
InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); |
@@ -110,7 +169,9 @@ ProfileImplIOData::LazyParams::LazyParams() |
ProfileImplIOData::LazyParams::~LazyParams() {} |
ProfileImplIOData::ProfileImplIOData() : ProfileIOData(false) {} |
-ProfileImplIOData::~ProfileImplIOData() {} |
+ProfileImplIOData::~ProfileImplIOData() { |
+ STLDeleteValues(&lazy_params_map_); |
+} |
void ProfileImplIOData::LazyInitializeInternal() const { |
main_request_context_ = new RequestContext; |
@@ -243,6 +304,11 @@ void ProfileImplIOData::LazyInitializeInternal() const { |
extensions_request_context_->set_cookie_store( |
extensions_cookie_store); |
+ main_request_context_->set_cookie_delegate( |
+ profile_params.cookie_monster_delegate); |
+ media_request_context_->set_cookie_delegate( |
+ profile_params.cookie_monster_delegate); |
+ |
main_http_factory_.reset(main_cache); |
media_http_factory_.reset(media_cache); |
main_request_context_->set_http_transaction_factory(main_cache); |
@@ -254,6 +320,69 @@ void ProfileImplIOData::LazyInitializeInternal() const { |
lazy_params_.reset(); |
} |
+scoped_refptr<ProfileIOData::RequestContext> |
+ProfileImplIOData::InitializeAppRequestContext( |
+ scoped_refptr<ChromeURLRequestContext> main_context, |
+ const Extension* installed_app) const { |
+ scoped_refptr<ProfileIOData::RequestContext> context = new RequestContext; |
+ |
+ // Copy most state from the main context. |
+ main_context->Copy(context); |
+ |
+ // Get the app-specific LazyParams, which was created in InitIsolatedApp. |
+ std::string id = installed_app->id(); |
+ LazyParamsMap::iterator iter = lazy_params_map_.find(id); |
+ DCHECK(iter != lazy_params_map_.end()); |
+ scoped_ptr<LazyParams> lazy_params(iter->second); |
+ |
+ const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
+ bool record_mode = chrome::kRecordModeEnabled && |
+ command_line.HasSwitch(switches::kRecordMode); |
+ bool playback_mode = command_line.HasSwitch(switches::kPlaybackMode); |
+ |
+ // Use a separate HTTP disk cache for isolated apps. |
+ net::HttpCache::DefaultBackend* app_backend = |
+ new net::HttpCache::DefaultBackend( |
+ net::DISK_CACHE, |
+ lazy_params->cache_path, |
+ lazy_params->cache_max_size, |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); |
+ net::HttpNetworkSession* main_network_session = |
+ main_http_factory_->GetSession(); |
+ net::HttpCache* app_cache = |
+ new net::HttpCache(main_network_session, app_backend); |
+ |
+ scoped_refptr<net::CookieStore> cookie_store = NULL; |
+ if (record_mode || playback_mode) { |
+ // Don't use existing cookies and use an in-memory store. |
+ cookie_store = new net::CookieMonster( |
+ NULL, main_context->cookie_delegate()); |
+ app_cache->set_mode( |
+ record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK); |
+ } |
+ |
+ // Use an app-specific cookie store. |
+ if (!cookie_store) { |
+ DCHECK(!lazy_params->cookie_path.empty()); |
+ |
+ scoped_refptr<SQLitePersistentCookieStore> cookie_db = |
+ new SQLitePersistentCookieStore(lazy_params->cookie_path); |
+ cookie_db->SetClearLocalStateOnExit( |
+ lazy_params->profile_params.clear_local_state_on_exit); |
+ cookie_store = |
+ new net::CookieMonster(cookie_db.get(), |
+ main_context->cookie_delegate()); |
+ } |
+ |
+ context->set_cookie_store(cookie_store); |
+ context->set_cookie_delegate(main_context->cookie_delegate()); |
+ |
+ context->set_http_transaction_factory(app_cache); |
+ |
+ lazy_params_map_.erase(iter); |
+ return context; |
+} |
+ |
scoped_refptr<ChromeURLRequestContext> |
ProfileImplIOData::AcquireMainRequestContext() const { |
DCHECK(main_request_context_); |
@@ -280,3 +409,15 @@ ProfileImplIOData::AcquireExtensionsRequestContext() const { |
extensions_request_context_ = NULL; |
return context; |
} |
+ |
+scoped_refptr<ChromeURLRequestContext> |
+ProfileImplIOData::AcquireIsolatedAppRequestContext( |
+ scoped_refptr<ChromeURLRequestContext> main_context, |
+ const Extension* installed_app) const { |
+ // We create per-app contexts on demand, unlike the others above. |
+ scoped_refptr<RequestContext> app_request_context = |
+ InitializeAppRequestContext(main_context, installed_app); |
+ DCHECK(app_request_context); |
+ app_request_context->set_profile_io_data(this); |
+ return app_request_context; |
+} |