Add ICU message format support
[chromium-blink-merge.git] / content / browser / browser_context.cc
blob25ffaf2d835cb527c2b10d10cb43f773d4283433
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/public/browser/browser_context.h"
7 #if !defined(OS_IOS)
8 #include "content/browser/download/download_manager_impl.h"
9 #include "content/browser/fileapi/chrome_blob_storage_context.h"
10 #include "content/browser/indexed_db/indexed_db_context_impl.h"
11 #include "content/browser/loader/resource_dispatcher_host_impl.h"
12 #include "content/browser/push_messaging/push_messaging_router.h"
13 #include "content/browser/storage_partition_impl_map.h"
14 #include "content/common/child_process_host_impl.h"
15 #include "content/public/browser/blob_handle.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/browser/site_instance.h"
19 #include "net/cookies/cookie_monster.h"
20 #include "net/cookies/cookie_store.h"
21 #include "net/ssl/channel_id_service.h"
22 #include "net/ssl/channel_id_store.h"
23 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "storage/browser/database/database_tracker.h"
26 #include "storage/browser/fileapi/external_mount_points.h"
27 #endif // !OS_IOS
29 using base::UserDataAdapter;
31 namespace content {
33 // Only ~BrowserContext() is needed on iOS.
34 #if !defined(OS_IOS)
35 namespace {
37 // Key names on BrowserContext.
38 const char kDownloadManagerKeyName[] = "download_manager";
39 const char kStorageParitionMapKeyName[] = "content_storage_partition_map";
41 #if defined(OS_CHROMEOS)
42 const char kMountPointsKey[] = "mount_points";
43 #endif // defined(OS_CHROMEOS)
45 StoragePartitionImplMap* GetStoragePartitionMap(
46 BrowserContext* browser_context) {
47 StoragePartitionImplMap* partition_map =
48 static_cast<StoragePartitionImplMap*>(
49 browser_context->GetUserData(kStorageParitionMapKeyName));
50 if (!partition_map) {
51 partition_map = new StoragePartitionImplMap(browser_context);
52 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
54 return partition_map;
57 StoragePartition* GetStoragePartitionFromConfig(
58 BrowserContext* browser_context,
59 const std::string& partition_domain,
60 const std::string& partition_name,
61 bool in_memory) {
62 StoragePartitionImplMap* partition_map =
63 GetStoragePartitionMap(browser_context);
65 if (browser_context->IsOffTheRecord())
66 in_memory = true;
68 return partition_map->Get(partition_domain, partition_name, in_memory);
71 void SaveSessionStateOnIOThread(
72 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
73 AppCacheServiceImpl* appcache_service) {
74 net::URLRequestContext* context = context_getter->GetURLRequestContext();
75 context->cookie_store()->GetCookieMonster()->
76 SetForceKeepSessionState();
77 context->channel_id_service()->GetChannelIDStore()->
78 SetForceKeepSessionState();
79 appcache_service->set_force_keep_session_state();
82 void SaveSessionStateOnIndexedDBThread(
83 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
84 indexed_db_context->SetForceKeepSessionState();
87 void ShutdownServiceWorkerContext(StoragePartition* partition) {
88 ServiceWorkerContextWrapper* wrapper =
89 static_cast<ServiceWorkerContextWrapper*>(
90 partition->GetServiceWorkerContext());
91 wrapper->process_manager()->Shutdown();
94 void SetDownloadManager(BrowserContext* context,
95 content::DownloadManager* download_manager) {
96 DCHECK_CURRENTLY_ON(BrowserThread::UI);
97 DCHECK(download_manager);
98 context->SetUserData(kDownloadManagerKeyName, download_manager);
101 } // namespace
103 // static
104 void BrowserContext::AsyncObliterateStoragePartition(
105 BrowserContext* browser_context,
106 const GURL& site,
107 const base::Closure& on_gc_required) {
108 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
109 on_gc_required);
112 // static
113 void BrowserContext::GarbageCollectStoragePartitions(
114 BrowserContext* browser_context,
115 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
116 const base::Closure& done) {
117 GetStoragePartitionMap(browser_context)->GarbageCollect(
118 active_paths.Pass(), done);
121 DownloadManager* BrowserContext::GetDownloadManager(
122 BrowserContext* context) {
123 DCHECK_CURRENTLY_ON(BrowserThread::UI);
124 if (!context->GetUserData(kDownloadManagerKeyName)) {
125 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
126 DCHECK(rdh);
127 DownloadManager* download_manager =
128 new DownloadManagerImpl(
129 GetContentClient()->browser()->GetNetLog(), context);
131 SetDownloadManager(context, download_manager);
132 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
135 return static_cast<DownloadManager*>(
136 context->GetUserData(kDownloadManagerKeyName));
139 // static
140 storage::ExternalMountPoints* BrowserContext::GetMountPoints(
141 BrowserContext* context) {
142 // Ensure that these methods are called on the UI thread, except for
143 // unittests where a UI thread might not have been created.
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
145 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
147 #if defined(OS_CHROMEOS)
148 if (!context->GetUserData(kMountPointsKey)) {
149 scoped_refptr<storage::ExternalMountPoints> mount_points =
150 storage::ExternalMountPoints::CreateRefCounted();
151 context->SetUserData(
152 kMountPointsKey,
153 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
156 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
157 kMountPointsKey);
158 #else
159 return NULL;
160 #endif
163 StoragePartition* BrowserContext::GetStoragePartition(
164 BrowserContext* browser_context,
165 SiteInstance* site_instance) {
166 std::string partition_domain;
167 std::string partition_name;
168 bool in_memory = false;
170 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
171 // this conditional and require that |site_instance| is non-NULL.
172 if (site_instance) {
173 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
174 browser_context, site_instance->GetSiteURL(), true,
175 &partition_domain, &partition_name, &in_memory);
178 return GetStoragePartitionFromConfig(
179 browser_context, partition_domain, partition_name, in_memory);
182 StoragePartition* BrowserContext::GetStoragePartitionForSite(
183 BrowserContext* browser_context,
184 const GURL& site) {
185 std::string partition_domain;
186 std::string partition_name;
187 bool in_memory;
189 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
190 browser_context, site, true, &partition_domain, &partition_name,
191 &in_memory);
193 return GetStoragePartitionFromConfig(
194 browser_context, partition_domain, partition_name, in_memory);
197 void BrowserContext::ForEachStoragePartition(
198 BrowserContext* browser_context,
199 const StoragePartitionCallback& callback) {
200 StoragePartitionImplMap* partition_map =
201 static_cast<StoragePartitionImplMap*>(
202 browser_context->GetUserData(kStorageParitionMapKeyName));
203 if (!partition_map)
204 return;
206 partition_map->ForEach(callback);
209 StoragePartition* BrowserContext::GetDefaultStoragePartition(
210 BrowserContext* browser_context) {
211 return GetStoragePartition(browser_context, NULL);
214 // static
215 void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
216 const char* data, size_t length,
217 const BlobCallback& callback) {
218 DCHECK_CURRENTLY_ON(BrowserThread::UI);
220 ChromeBlobStorageContext* blob_context =
221 ChromeBlobStorageContext::GetFor(browser_context);
222 BrowserThread::PostTaskAndReplyWithResult(
223 BrowserThread::IO, FROM_HERE,
224 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
225 make_scoped_refptr(blob_context), data, length),
226 callback);
229 // static
230 void BrowserContext::CreateFileBackedBlob(
231 BrowserContext* browser_context,
232 const base::FilePath& path,
233 int64_t offset,
234 int64_t size,
235 const base::Time& expected_modification_time,
236 const BlobCallback& callback) {
237 DCHECK_CURRENTLY_ON(BrowserThread::UI);
239 ChromeBlobStorageContext* blob_context =
240 ChromeBlobStorageContext::GetFor(browser_context);
241 BrowserThread::PostTaskAndReplyWithResult(
242 BrowserThread::IO, FROM_HERE,
243 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
244 make_scoped_refptr(blob_context), path, offset, size,
245 expected_modification_time),
246 callback);
249 // static
250 void BrowserContext::DeliverPushMessage(
251 BrowserContext* browser_context,
252 const GURL& origin,
253 int64 service_worker_registration_id,
254 const std::string& data,
255 const base::Callback<void(PushDeliveryStatus)>& callback) {
256 DCHECK_CURRENTLY_ON(BrowserThread::UI);
257 PushMessagingRouter::DeliverMessage(
258 browser_context, origin, service_worker_registration_id, data, callback);
261 // static
262 void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
263 // Service Workers must shutdown before the browser context is destroyed,
264 // since they keep render process hosts alive and the codebase assumes that
265 // render process hosts die before their profile (browser context) dies.
266 ForEachStoragePartition(browser_context,
267 base::Bind(ShutdownServiceWorkerContext));
270 void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
271 // This will be enough to tickle initialization of BrowserContext if
272 // necessary, which initializes ResourceContext. The reason we don't call
273 // ResourceContext::InitializeResourceContext() directly here is that
274 // ResourceContext initialization may call back into BrowserContext
275 // and when that call returns it'll end rewriting its UserData map. It will
276 // end up rewriting the same value but this still causes a race condition.
278 // See http://crbug.com/115678.
279 GetDefaultStoragePartition(context);
282 void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
283 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
284 SetForceKeepSessionState();
285 StoragePartition* storage_partition =
286 BrowserContext::GetDefaultStoragePartition(browser_context);
288 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
289 BrowserThread::PostTask(
290 BrowserThread::IO, FROM_HERE,
291 base::Bind(
292 &SaveSessionStateOnIOThread,
293 make_scoped_refptr(browser_context->GetRequestContext()),
294 static_cast<AppCacheServiceImpl*>(
295 storage_partition->GetAppCacheService())));
298 DOMStorageContextWrapper* dom_storage_context_proxy =
299 static_cast<DOMStorageContextWrapper*>(
300 storage_partition->GetDOMStorageContext());
301 dom_storage_context_proxy->SetForceKeepSessionState();
303 IndexedDBContextImpl* indexed_db_context_impl =
304 static_cast<IndexedDBContextImpl*>(
305 storage_partition->GetIndexedDBContext());
306 // No task runner in unit tests.
307 if (indexed_db_context_impl->TaskRunner()) {
308 indexed_db_context_impl->TaskRunner()->PostTask(
309 FROM_HERE,
310 base::Bind(&SaveSessionStateOnIndexedDBThread,
311 make_scoped_refptr(indexed_db_context_impl)));
315 void BrowserContext::SetDownloadManagerForTesting(
316 BrowserContext* browser_context,
317 DownloadManager* download_manager) {
318 SetDownloadManager(browser_context, download_manager);
321 #endif // !OS_IOS
323 BrowserContext::~BrowserContext() {
324 #if !defined(OS_IOS)
325 if (GetUserData(kDownloadManagerKeyName))
326 GetDownloadManager(this)->Shutdown();
327 #endif
330 } // namespace content