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 "android_webview/browser/net_disk_cache_remover.h"
6
7 #include "base/bind_helpers.h"
8 #include "content/public/browser/browser_context.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/web_contents.h"
11 #include "net/disk_cache/disk_cache.h"
12 #include "net/http/http_cache.h"
13 #include "net/http/http_transaction_factory.h"
14 #include "net/url_request/url_request_context_getter.h"
15 #include "net/url_request/url_request_context.h"
16 #include "net/base/completion_callback.h"
17
18 using content::BrowserThread;
19 using disk_cache::Backend;
20 using net::CompletionCallback;
21 using net::URLRequestContextGetter;
22
23 namespace {
24 // Everything is called and accessed on the IO thread.
25
Noop(int rv)26 void Noop(int rv) {
27 DCHECK(rv == net::OK);
28 }
29
CallDoomAllEntries(Backend ** backend,int rv)30 void CallDoomAllEntries(Backend** backend, int rv) {
31 DCHECK(rv == net::OK);
32 (*backend)->DoomAllEntries(base::Bind(&Noop));
33 }
34
ClearHttpDiskCacheOfContext(URLRequestContextGetter * context_getter)35 void ClearHttpDiskCacheOfContext(URLRequestContextGetter* context_getter) {
36 typedef Backend* BackendPtr; // Make line below easier to understand.
37 BackendPtr* backend_ptr = new BackendPtr(NULL);
38 CompletionCallback callback(base::Bind(&CallDoomAllEntries,
39 base::Owned(backend_ptr)));
40
41 int rv = context_getter->GetURLRequestContext()->
42 http_transaction_factory()->GetCache()->GetBackend(backend_ptr, callback);
43
44 // If not net::ERR_IO_PENDING, then backend pointer is updated but callback
45 // is not called, so call it explicitly.
46 if (rv != net::ERR_IO_PENDING)
47 callback.Run(net::OK);
48 }
49
ClearHttpDiskCacheOnIoThread(URLRequestContextGetter * main_context_getter,URLRequestContextGetter * media_context_getter)50 void ClearHttpDiskCacheOnIoThread(
51 URLRequestContextGetter* main_context_getter,
52 URLRequestContextGetter* media_context_getter) {
53 ClearHttpDiskCacheOfContext(main_context_getter);
54 ClearHttpDiskCacheOfContext(media_context_getter);
55 }
56
57 } // namespace
58
59 namespace android_webview {
60
RemoveHttpDiskCache(content::BrowserContext * browser_context,int renderer_child_id)61 void RemoveHttpDiskCache(content::BrowserContext* browser_context,
62 int renderer_child_id) {
63 URLRequestContextGetter* main_context_getter =
64 browser_context->GetRequestContextForRenderProcess(renderer_child_id);
65 URLRequestContextGetter* media_context_getter =
66 browser_context->GetMediaRequestContextForRenderProcess(
67 renderer_child_id);
68
69 BrowserThread::PostTask(
70 BrowserThread::IO, FROM_HERE,
71 base::Bind(&ClearHttpDiskCacheOnIoThread,
72 base::Unretained(main_context_getter),
73 base::Unretained(media_context_getter)));
74 }
75
76 } // namespace android_webview
77