Revert of Revert of Disable telemetry tests for Mac 10.9. (patchset #1 id:1 of https...
[chromium-blink-merge.git] / content / browser / browser_context.cc
blob7618833119848bf485a9b2cc2326d15a5d5d98f8
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 } // namespace
96 // static
97 void BrowserContext::AsyncObliterateStoragePartition(
98 BrowserContext* browser_context,
99 const GURL& site,
100 const base::Closure& on_gc_required) {
101 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
102 on_gc_required);
105 // static
106 void BrowserContext::GarbageCollectStoragePartitions(
107 BrowserContext* browser_context,
108 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
109 const base::Closure& done) {
110 GetStoragePartitionMap(browser_context)->GarbageCollect(
111 active_paths.Pass(), done);
114 DownloadManager* BrowserContext::GetDownloadManager(
115 BrowserContext* context) {
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
117 if (!context->GetUserData(kDownloadManagerKeyName)) {
118 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
119 DCHECK(rdh);
120 DownloadManager* download_manager =
121 new DownloadManagerImpl(
122 GetContentClient()->browser()->GetNetLog(), context);
124 context->SetUserData(
125 kDownloadManagerKeyName,
126 download_manager);
127 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
130 return static_cast<DownloadManager*>(
131 context->GetUserData(kDownloadManagerKeyName));
134 // static
135 storage::ExternalMountPoints* BrowserContext::GetMountPoints(
136 BrowserContext* context) {
137 // Ensure that these methods are called on the UI thread, except for
138 // unittests where a UI thread might not have been created.
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
140 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
142 #if defined(OS_CHROMEOS)
143 if (!context->GetUserData(kMountPointsKey)) {
144 scoped_refptr<storage::ExternalMountPoints> mount_points =
145 storage::ExternalMountPoints::CreateRefCounted();
146 context->SetUserData(
147 kMountPointsKey,
148 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
151 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
152 kMountPointsKey);
153 #else
154 return NULL;
155 #endif
158 StoragePartition* BrowserContext::GetStoragePartition(
159 BrowserContext* browser_context,
160 SiteInstance* site_instance) {
161 std::string partition_domain;
162 std::string partition_name;
163 bool in_memory = false;
165 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
166 // this conditional and require that |site_instance| is non-NULL.
167 if (site_instance) {
168 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
169 browser_context, site_instance->GetSiteURL(), true,
170 &partition_domain, &partition_name, &in_memory);
173 return GetStoragePartitionFromConfig(
174 browser_context, partition_domain, partition_name, in_memory);
177 StoragePartition* BrowserContext::GetStoragePartitionForSite(
178 BrowserContext* browser_context,
179 const GURL& site) {
180 std::string partition_domain;
181 std::string partition_name;
182 bool in_memory;
184 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
185 browser_context, site, true, &partition_domain, &partition_name,
186 &in_memory);
188 return GetStoragePartitionFromConfig(
189 browser_context, partition_domain, partition_name, in_memory);
192 void BrowserContext::ForEachStoragePartition(
193 BrowserContext* browser_context,
194 const StoragePartitionCallback& callback) {
195 StoragePartitionImplMap* partition_map =
196 static_cast<StoragePartitionImplMap*>(
197 browser_context->GetUserData(kStorageParitionMapKeyName));
198 if (!partition_map)
199 return;
201 partition_map->ForEach(callback);
204 StoragePartition* BrowserContext::GetDefaultStoragePartition(
205 BrowserContext* browser_context) {
206 return GetStoragePartition(browser_context, NULL);
209 void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
210 const char* data, size_t length,
211 const BlobCallback& callback) {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
214 ChromeBlobStorageContext* blob_context =
215 ChromeBlobStorageContext::GetFor(browser_context);
216 BrowserThread::PostTaskAndReplyWithResult(
217 BrowserThread::IO, FROM_HERE,
218 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
219 make_scoped_refptr(blob_context), data, length),
220 callback);
223 // static
224 void BrowserContext::DeliverPushMessage(
225 BrowserContext* browser_context,
226 const GURL& origin,
227 int64 service_worker_registration_id,
228 const std::string& data,
229 const base::Callback<void(PushDeliveryStatus)>& callback) {
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
231 PushMessagingRouter::DeliverMessage(
232 browser_context, origin, service_worker_registration_id, data, callback);
235 // static
236 void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
237 // Service Workers must shutdown before the browser context is destroyed,
238 // since they keep render process hosts alive and the codebase assumes that
239 // render process hosts die before their profile (browser context) dies.
240 ForEachStoragePartition(browser_context,
241 base::Bind(ShutdownServiceWorkerContext));
244 void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
245 // This will be enough to tickle initialization of BrowserContext if
246 // necessary, which initializes ResourceContext. The reason we don't call
247 // ResourceContext::InitializeResourceContext() directly here is that
248 // ResourceContext initialization may call back into BrowserContext
249 // and when that call returns it'll end rewriting its UserData map. It will
250 // end up rewriting the same value but this still causes a race condition.
252 // See http://crbug.com/115678.
253 GetDefaultStoragePartition(context);
256 void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
257 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
258 SetForceKeepSessionState();
259 StoragePartition* storage_partition =
260 BrowserContext::GetDefaultStoragePartition(browser_context);
262 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
263 BrowserThread::PostTask(
264 BrowserThread::IO, FROM_HERE,
265 base::Bind(
266 &SaveSessionStateOnIOThread,
267 make_scoped_refptr(browser_context->GetRequestContext()),
268 static_cast<AppCacheServiceImpl*>(
269 storage_partition->GetAppCacheService())));
272 DOMStorageContextWrapper* dom_storage_context_proxy =
273 static_cast<DOMStorageContextWrapper*>(
274 storage_partition->GetDOMStorageContext());
275 dom_storage_context_proxy->SetForceKeepSessionState();
277 IndexedDBContextImpl* indexed_db_context_impl =
278 static_cast<IndexedDBContextImpl*>(
279 storage_partition->GetIndexedDBContext());
280 // No task runner in unit tests.
281 if (indexed_db_context_impl->TaskRunner()) {
282 indexed_db_context_impl->TaskRunner()->PostTask(
283 FROM_HERE,
284 base::Bind(&SaveSessionStateOnIndexedDBThread,
285 make_scoped_refptr(indexed_db_context_impl)));
289 #endif // !OS_IOS
291 BrowserContext::~BrowserContext() {
292 #if !defined(OS_IOS)
293 if (GetUserData(kDownloadManagerKeyName))
294 GetDownloadManager(this)->Shutdown();
295 #endif
298 } // namespace content