Home | History | Annotate | Download | only in appcache
      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/appcache/chrome_appcache_service.h"
      6 
      7 #include "base/files/file_path.h"
      8 #include "content/public/browser/browser_thread.h"
      9 #include "content/public/browser/content_browser_client.h"
     10 #include "content/public/browser/resource_context.h"
     11 #include "net/base/net_errors.h"
     12 #include "net/url_request/url_request_context_getter.h"
     13 #include "webkit/browser/appcache/appcache_storage_impl.h"
     14 #include "webkit/browser/quota/quota_manager.h"
     15 
     16 namespace content {
     17 
     18 ChromeAppCacheService::ChromeAppCacheService(
     19     quota::QuotaManagerProxy* quota_manager_proxy)
     20     : AppCacheService(quota_manager_proxy),
     21       resource_context_(NULL) {
     22 }
     23 
     24 void ChromeAppCacheService::InitializeOnIOThread(
     25     const base::FilePath& cache_path,
     26     ResourceContext* resource_context,
     27     net::URLRequestContextGetter* request_context_getter,
     28     scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) {
     29   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     30 
     31   cache_path_ = cache_path;
     32   resource_context_ = resource_context;
     33 
     34   // The |request_context_getter| can be NULL in some unit tests.
     35   //
     36   // TODO(ajwong): TestProfile is difficult to work with. The
     37   // SafeBrowsing tests require that GetRequestContext return NULL
     38   // so we can't depend on having a non-NULL value here. See crbug/149783.
     39   if (request_context_getter)
     40     set_request_context(request_context_getter->GetURLRequestContext());
     41 
     42   // Init our base class.
     43   Initialize(
     44       cache_path_,
     45       BrowserThread::GetMessageLoopProxyForThread(
     46           BrowserThread::FILE_USER_BLOCKING)
     47           .get(),
     48       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get());
     49   set_appcache_policy(this);
     50   set_special_storage_policy(special_storage_policy.get());
     51 }
     52 
     53 bool ChromeAppCacheService::CanLoadAppCache(const GURL& manifest_url,
     54                                             const GURL& first_party) {
     55   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     56   // We don't prompt for read access.
     57   return GetContentClient()->browser()->AllowAppCache(
     58       manifest_url, first_party, resource_context_);
     59 }
     60 
     61 bool ChromeAppCacheService::CanCreateAppCache(
     62     const GURL& manifest_url, const GURL& first_party) {
     63   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     64   return GetContentClient()->browser()->AllowAppCache(
     65       manifest_url, first_party, resource_context_);
     66 }
     67 
     68 ChromeAppCacheService::~ChromeAppCacheService() {}
     69 
     70 void ChromeAppCacheService::DeleteOnCorrectThread() const {
     71   if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) &&
     72       !BrowserThread::CurrentlyOn(BrowserThread::IO)) {
     73     BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this);
     74     return;
     75   }
     76   delete this;
     77 }
     78 
     79 }  // namespace content
     80