Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / resource_context_impl.cc
blobb99c80d54ab0d6d5b4bb105d09cf47af59567bbe
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/browser/resource_context_impl.h"
7 #include "base/logging.h"
8 #include "content/browser/fileapi/chrome_blob_storage_context.h"
9 #include "content/browser/loader/resource_dispatcher_host_impl.h"
10 #include "content/browser/loader/resource_request_info_impl.h"
11 #include "content/browser/streams/stream_context.h"
12 #include "content/browser/webui/url_data_manager_backend.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "net/base/keygen_handler.h"
16 #include "net/ssl/client_cert_store.h"
18 using base::UserDataAdapter;
20 namespace content {
22 namespace {
24 // Key names on ResourceContext.
25 const char kBlobStorageContextKeyName[] = "content_blob_storage_context";
26 const char kStreamContextKeyName[] = "content_stream_context";
27 const char kURLDataManagerBackendKeyName[] = "url_data_manager_backend";
29 // Used by the default implementation of GetMediaDeviceIDSalt, below.
30 std::string ReturnEmptySalt() {
31 return std::string();
34 } // namespace
37 ResourceContext::ResourceContext() {
38 if (ResourceDispatcherHostImpl::Get())
39 ResourceDispatcherHostImpl::Get()->AddResourceContext(this);
42 ResourceContext::~ResourceContext() {
43 ResourceDispatcherHostImpl* rdhi = ResourceDispatcherHostImpl::Get();
44 if (rdhi) {
45 rdhi->CancelRequestsForContext(this);
46 rdhi->RemoveResourceContext(this);
49 // In some tests this object is destructed on UI thread.
50 DetachUserDataThread();
53 ResourceContext::SaltCallback ResourceContext::GetMediaDeviceIDSalt() {
54 return base::Bind(&ReturnEmptySalt);
57 scoped_ptr<net::ClientCertStore> ResourceContext::CreateClientCertStore() {
58 return scoped_ptr<net::ClientCertStore>();
61 void ResourceContext::CreateKeygenHandler(
62 uint32 key_size_in_bits,
63 const std::string& challenge_string,
64 const GURL& url,
65 const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback) {
66 callback.Run(make_scoped_ptr(
67 new net::KeygenHandler(key_size_in_bits, challenge_string, url)));
70 ChromeBlobStorageContext* GetChromeBlobStorageContextForResourceContext(
71 const ResourceContext* resource_context) {
72 DCHECK_CURRENTLY_ON(BrowserThread::IO);
73 return UserDataAdapter<ChromeBlobStorageContext>::Get(
74 resource_context, kBlobStorageContextKeyName);
77 StreamContext* GetStreamContextForResourceContext(
78 const ResourceContext* resource_context) {
79 DCHECK_CURRENTLY_ON(BrowserThread::IO);
80 return UserDataAdapter<StreamContext>::Get(
81 resource_context, kStreamContextKeyName);
84 URLDataManagerBackend* GetURLDataManagerForResourceContext(
85 ResourceContext* context) {
86 DCHECK_CURRENTLY_ON(BrowserThread::IO);
87 if (!context->GetUserData(kURLDataManagerBackendKeyName)) {
88 context->SetUserData(kURLDataManagerBackendKeyName,
89 new URLDataManagerBackend());
91 return static_cast<URLDataManagerBackend*>(
92 context->GetUserData(kURLDataManagerBackendKeyName));
95 void InitializeResourceContext(BrowserContext* browser_context) {
96 ResourceContext* resource_context = browser_context->GetResourceContext();
98 resource_context->SetUserData(
99 kBlobStorageContextKeyName,
100 new UserDataAdapter<ChromeBlobStorageContext>(
101 ChromeBlobStorageContext::GetFor(browser_context)));
103 resource_context->SetUserData(
104 kStreamContextKeyName,
105 new UserDataAdapter<StreamContext>(
106 StreamContext::GetFor(browser_context)));
108 resource_context->DetachUserDataThread();
111 } // namespace content