Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #include "content/browser/resource_context_impl.h"
      6 
      7 #include "base/logging.h"
      8 #include "content/browser/fileapi/chrome_blob_storage_context.h"
      9 #include "content/browser/host_zoom_map_impl.h"
     10 #include "content/browser/loader/resource_dispatcher_host_impl.h"
     11 #include "content/browser/loader/resource_request_info_impl.h"
     12 #include "content/browser/streams/stream_context.h"
     13 #include "content/browser/webui/url_data_manager_backend.h"
     14 #include "content/public/browser/browser_context.h"
     15 #include "content/public/browser/browser_thread.h"
     16 #include "net/base/keygen_handler.h"
     17 #include "net/ssl/client_cert_store.h"
     18 
     19 using base::UserDataAdapter;
     20 
     21 namespace content {
     22 
     23 namespace {
     24 
     25 // Key names on ResourceContext.
     26 const char kBlobStorageContextKeyName[] = "content_blob_storage_context";
     27 const char kHostZoomMapKeyName[] = "content_host_zoom_map";
     28 const char kStreamContextKeyName[] = "content_stream_context";
     29 const char kURLDataManagerBackendKeyName[] = "url_data_manager_backend";
     30 
     31 class NonOwningZoomData : public base::SupportsUserData::Data {
     32  public:
     33   explicit NonOwningZoomData(HostZoomMap* hzm) : host_zoom_map_(hzm) {}
     34   HostZoomMap* host_zoom_map() { return host_zoom_map_; }
     35 
     36  private:
     37   HostZoomMap* host_zoom_map_;
     38 };
     39 
     40 // Used by the default implementation of GetMediaDeviceIDSalt, below.
     41 std::string ReturnEmptySalt() {
     42   return std::string();
     43 }
     44 
     45 }  // namespace
     46 
     47 
     48 ResourceContext::ResourceContext() {
     49   if (ResourceDispatcherHostImpl::Get())
     50     ResourceDispatcherHostImpl::Get()->AddResourceContext(this);
     51 }
     52 
     53 ResourceContext::~ResourceContext() {
     54   ResourceDispatcherHostImpl* rdhi = ResourceDispatcherHostImpl::Get();
     55   if (rdhi) {
     56     rdhi->CancelRequestsForContext(this);
     57     rdhi->RemoveResourceContext(this);
     58   }
     59 
     60   // In some tests this object is destructed on UI thread.
     61   DetachUserDataThread();
     62 }
     63 
     64 ResourceContext::SaltCallback ResourceContext::GetMediaDeviceIDSalt() {
     65   return base::Bind(&ReturnEmptySalt);
     66 }
     67 
     68 scoped_ptr<net::ClientCertStore> ResourceContext::CreateClientCertStore() {
     69   return scoped_ptr<net::ClientCertStore>();
     70 }
     71 
     72 void ResourceContext::CreateKeygenHandler(
     73     uint32 key_size_in_bits,
     74     const std::string& challenge_string,
     75     const GURL& url,
     76     const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback) {
     77   callback.Run(make_scoped_ptr(
     78       new net::KeygenHandler(key_size_in_bits, challenge_string, url)));
     79 }
     80 
     81 ChromeBlobStorageContext* GetChromeBlobStorageContextForResourceContext(
     82     ResourceContext* resource_context) {
     83   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     84   return UserDataAdapter<ChromeBlobStorageContext>::Get(
     85       resource_context, kBlobStorageContextKeyName);
     86 }
     87 
     88 StreamContext* GetStreamContextForResourceContext(
     89     ResourceContext* resource_context) {
     90   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     91   return UserDataAdapter<StreamContext>::Get(
     92       resource_context, kStreamContextKeyName);
     93 }
     94 
     95 HostZoomMap* GetHostZoomMapForResourceContext(ResourceContext* context) {
     96   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     97   return static_cast<NonOwningZoomData*>(
     98       context->GetUserData(kHostZoomMapKeyName))->host_zoom_map();
     99 }
    100 
    101 URLDataManagerBackend* GetURLDataManagerForResourceContext(
    102     ResourceContext* context) {
    103   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    104   if (!context->GetUserData(kURLDataManagerBackendKeyName)) {
    105     context->SetUserData(kURLDataManagerBackendKeyName,
    106                          new URLDataManagerBackend());
    107   }
    108   return static_cast<URLDataManagerBackend*>(
    109       context->GetUserData(kURLDataManagerBackendKeyName));
    110 }
    111 
    112 void InitializeResourceContext(BrowserContext* browser_context) {
    113   ResourceContext* resource_context = browser_context->GetResourceContext();
    114   DCHECK(!resource_context->GetUserData(kHostZoomMapKeyName));
    115 
    116   resource_context->SetUserData(
    117       kBlobStorageContextKeyName,
    118       new UserDataAdapter<ChromeBlobStorageContext>(
    119           ChromeBlobStorageContext::GetFor(browser_context)));
    120 
    121   resource_context->SetUserData(
    122       kStreamContextKeyName,
    123       new UserDataAdapter<StreamContext>(
    124           StreamContext::GetFor(browser_context)));
    125 
    126   // This object is owned by the BrowserContext and not ResourceContext, so
    127   // store a non-owning pointer here.
    128   resource_context->SetUserData(
    129       kHostZoomMapKeyName,
    130       new NonOwningZoomData(
    131           HostZoomMap::GetForBrowserContext(browser_context)));
    132 
    133   resource_context->DetachUserDataThread();
    134 }
    135 
    136 }  // namespace content
    137