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

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: rebased to latest, auto-merged net.gyp Created 7 years, 8 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
« no previous file with comments | « net/disk_cache/backend_unittest.cc ('k') | net/disk_cache/cache_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // TODO(pasko): The two caches should never coexist on disk. Each individual
40 // cache backend should fail to initialize if it observes the index that does
41 // not belong to it.
42 if (base::FieldTrialList::FindFullName("SimpleCacheTrial") == "Yes") {
43 // TODO(gavinp,pasko): While simple backend development proceeds, we're only
44 // testing it against net::DISK_CACHE. Turn it on for more cache types as
45 // appropriate.
46 if (type_ == net::DISK_CACHE) {
47 VLOG(1) << "Using the Simple Cache Backend.";
48 return disk_cache::SimpleBackendImpl::CreateBackend(
49 path_, max_bytes_, type_, disk_cache::kNone, thread_, net_log_,
50 backend_, base::Bind(&CacheCreator::OnIOComplete,
51 base::Unretained(this)));
52 }
53 }
54 disk_cache::BackendImpl* new_cache =
55 new disk_cache::BackendImpl(path_, thread_, net_log_);
56 created_cache_ = new_cache;
57 new_cache->SetMaxSize(max_bytes_);
58 new_cache->SetType(type_);
59 new_cache->SetFlags(flags_);
60 int rv = new_cache->Init(
61 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
62 DCHECK_EQ(net::ERR_IO_PENDING, rv);
63 return rv;
64 }
65
66 void CacheCreator::DoCallback(int result) {
67 DCHECK_NE(net::ERR_IO_PENDING, result);
68 if (result == net::OK) {
69 *backend_ = created_cache_;
70 } else {
71 LOG(ERROR) << "Unable to create cache";
72 *backend_ = NULL;
73 delete created_cache_;
74 }
75 callback_.Run(result);
76 delete this;
77 }
78
79 // If the initialization of the cache fails, and |force| is true, we will
80 // discard the whole cache and create a new one.
81 void CacheCreator::OnIOComplete(int result) {
82 if (result == net::OK || !force_ || retry_)
83 return DoCallback(result);
84
85 // This is a failure and we are supposed to try again, so delete the object,
86 // delete all the files, and try again.
87 retry_ = true;
88 delete created_cache_;
89 created_cache_ = NULL;
90 if (!disk_cache::DelayedCacheCleanup(path_))
91 return DoCallback(result);
92
93 // The worker thread will start deleting files soon, but the original folder
94 // is not there anymore... let's create a new set of files.
95 int rv = Run();
96 DCHECK_EQ(net::ERR_IO_PENDING, rv);
97 }
98
99 int CreateCacheBackend(net::CacheType type, const base::FilePath& path,
100 int max_bytes,
101 bool force, base::MessageLoopProxy* thread,
102 net::NetLog* net_log, Backend** backend,
103 const net::CompletionCallback& callback) {
104 DCHECK(!callback.is_null());
105 if (type == net::MEMORY_CACHE) {
106 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log);
107 return *backend ? net::OK : net::ERR_FAILED;
108 }
109 DCHECK(thread);
110 CacheCreator* creator = new CacheCreator(path, force, max_bytes, type, kNone,
111 thread, net_log, backend, callback);
112 return creator->Run();
113 }
114
115
116 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/backend_unittest.cc ('k') | net/disk_cache/cache_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698