Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1758)

Side by Side Diff: net/disk_cache/cache_creator.cc

Issue 12794003: Initialize the simple cache backend at runtime. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: upload from git Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "base/file_util.h"
6 #include "base/metrics/field_trial.h"
7 #include "base/stringprintf.h"
8 #include "net/base/net_errors.h"
9 #include "net/disk_cache/backend_impl.h"
10 #include "net/disk_cache/cache_util.h"
11 #include "net/disk_cache/mem_backend_impl.h"
12 #include "net/disk_cache/simple/simple_backend_impl.h"
13
14 namespace disk_cache {
15
16 CacheCreator::CacheCreator(
17 const base::FilePath& path, bool force, int max_bytes,
18 net::CacheType type, uint32 flags,
19 base::MessageLoopProxy* thread, net::NetLog* net_log,
20 disk_cache::Backend** backend,
21 const net::CompletionCallback& callback)
22 : path_(path),
23 force_(force),
24 retry_(false),
25 max_bytes_(max_bytes),
26 type_(type),
27 flags_(flags),
28 thread_(thread),
29 backend_(backend),
30 callback_(callback),
31 created_cache_(NULL),
32 net_log_(net_log) {
33 }
34
35 CacheCreator::~CacheCreator() {
36 }
37
38 int CacheCreator::Run() {
39 if (type_ == net::MEMORY_CACHE) {
40 *backend_ = disk_cache::MemBackendImpl::CreateBackend(max_bytes_, net_log_);
41 return *backend_ ? net::OK : net::ERR_FAILED;
42 }
43 // TODO(pasko): The two caches should never coexist. Set up cache cleanup in
44 // two cases: (1) the other version of the cache exists, (2) we are in the
45 // "Control" group of the experiment.
46 if (base::FieldTrialList::FindFullName("SimpleCacheTrial") == "Yes") {
47 // TODO(gavinp,pasko): While simple backend development proceeds, we're only
48 // testing it against net::DISK_CACHE. Turn it on for more cache types as
49 // appropriate.
50 if (type_ == net::DISK_CACHE) {
51 VLOG(1) << "Using the Simple Cache Backend.";
52 return disk_cache::SimpleBackendImpl::CreateBackend(path_, max_bytes_,
53 type_, disk_cache::kNone, thread_, net_log_, backend_,
54 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
55 }
56 }
57 disk_cache::BackendImpl* new_cache = new disk_cache::BackendImpl(
58 path_, thread_, net_log_);
59 created_cache_ = new_cache;
60 new_cache->SetMaxSize(max_bytes_);
61 new_cache->SetType(type_);
62 new_cache->SetFlags(flags_);
63 int rv = new_cache->Init(
64 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
65 DCHECK_EQ(net::ERR_IO_PENDING, rv);
66 return rv;
67 }
68
69 void CacheCreator::DoCallback(int result) {
70 DCHECK_NE(net::ERR_IO_PENDING, result);
71 if (result == net::OK) {
72 *backend_ = created_cache_;
73 } else {
74 LOG(ERROR) << "Unable to create cache";
75 *backend_ = NULL;
76 delete created_cache_;
77 }
78 callback_.Run(result);
79 delete this;
80 }
81
82 // If the initialization of the cache fails, and |force| is true, we will
83 // discard the whole cache and create a new one.
84 void CacheCreator::OnIOComplete(int result) {
85 if (result == net::OK || !force_ || retry_)
86 return DoCallback(result);
87
88 // This is a failure and we are supposed to try again, so delete the object,
89 // delete all the files, and try again.
90 retry_ = true;
91 delete created_cache_;
92 created_cache_ = NULL;
93 if (!disk_cache::DelayedCacheCleanup(path_))
94 return DoCallback(result);
95
96 // The worker thread will start deleting files soon, but the original folder
97 // is not there anymore... let's create a new set of files.
98 int rv = Run();
99 DCHECK_EQ(net::ERR_IO_PENDING, rv);
100 }
101
102 int CreateCacheBackend(net::CacheType type, const base::FilePath& path,
103 int max_bytes, bool force, base::MessageLoopProxy* thread,
104 net::NetLog* net_log, Backend** backend,
105 const net::CompletionCallback& callback) {
106 DCHECK(!callback.is_null());
107 DCHECK(thread || type == net::MEMORY_CACHE);
108 CacheCreator* creator = new CacheCreator(path, force, max_bytes, type, kNone,
109 thread, net_log, backend, callback);
110 return creator->Run();
111 }
112
113
114 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698