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

Side by Side Diff: mojo/services/network/network_context.cc

Issue 1344853002: Teach the network service to clear its disk cache if requested. (Closed) Base URL: https://github.com/domokit/monet.git@master
Patch Set: Created 5 years, 3 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/services/network/network_context.h" 5 #include "mojo/services/network/network_context.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base_paths.h" 10 #include "base/base_paths.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/files/file_util.h"
13 #include "base/path_service.h" 14 #include "base/path_service.h"
14 #include "mojo/common/user_agent.h" 15 #include "mojo/common/user_agent.h"
15 #include "mojo/services/network/url_loader_impl.h" 16 #include "mojo/services/network/url_loader_impl.h"
16 #include "net/log/net_log_util.h" 17 #include "net/log/net_log_util.h"
17 #include "net/log/write_to_file_net_log_observer.h" 18 #include "net/log/write_to_file_net_log_observer.h"
18 #include "net/proxy/proxy_service.h" 19 #include "net/proxy/proxy_service.h"
19 #include "net/url_request/url_request_context.h" 20 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_builder.h" 21 #include "net/url_request/url_request_context_builder.h"
21 22
22 namespace mojo { 23 namespace mojo {
23 24
24 namespace { 25 namespace {
25 // Logs network information to the specified file. 26 // Logs network information to the specified file.
26 const char kLogNetLog[] = "log-net-log"; 27 const char kLogNetLog[] = "log-net-log";
28
29 base::FilePath GetCacheDirectory(const base::FilePath& base_path) {
30 return base_path.Append(FILE_PATH_LITERAL("Cache"));
31 }
27 } 32 }
28 33
29 class NetworkContext::MojoNetLog : public net::NetLog { 34 class NetworkContext::MojoNetLog : public net::NetLog {
30 public: 35 public:
31 MojoNetLog() { 36 MojoNetLog() {
32 const base::CommandLine* command_line = 37 const base::CommandLine* command_line =
33 base::CommandLine::ForCurrentProcess(); 38 base::CommandLine::ForCurrentProcess();
34 if (!command_line->HasSwitch(kLogNetLog)) 39 if (!command_line->HasSwitch(kLogNetLog))
35 return; 40 return;
36 41
(...skipping 20 matching lines...) Expand all
57 if (write_to_file_observer_) 62 if (write_to_file_observer_)
58 write_to_file_observer_->StopObserving(nullptr); 63 write_to_file_observer_->StopObserving(nullptr);
59 } 64 }
60 65
61 private: 66 private:
62 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; 67 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_;
63 68
64 DISALLOW_COPY_AND_ASSIGN(MojoNetLog); 69 DISALLOW_COPY_AND_ASSIGN(MojoNetLog);
65 }; 70 };
66 71
72 // static
73 void NetworkContext::ClearCache(const base::FilePath& base_path,
74 scoped_refptr<base::TaskRunner> task_runner) {
75 base::FilePath trash_dir = base_path.Append(FILE_PATH_LITERAL("Trash"));
76 base::CreateDirectory(trash_dir);
77 base::FilePath dest_dir;
78 base::CreateTemporaryDirInDir(trash_dir, "", &dest_dir);
79
80 // Move the current cache directory, if present, into trash.
81 base::FilePath cache_dir = GetCacheDirectory(base_path);
82 if (PathExists(cache_dir)) {
83 base::File::Error error;
84 if (!base::ReplaceFile(cache_dir, dest_dir, &error)) {
85 LOG(ERROR) << "Failed to clear cache content: " << error;
86 }
87 }
88 // Recreate the cache directory.
89 base::CreateDirectory(cache_dir);
90
91 task_runner->PostTask(
92 FROM_HERE,
93 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_dir, true));
94 }
95
67 NetworkContext::NetworkContext( 96 NetworkContext::NetworkContext(
68 scoped_ptr<net::URLRequestContext> url_request_context) 97 scoped_ptr<net::URLRequestContext> url_request_context)
69 : net_log_(new MojoNetLog), 98 : net_log_(new MojoNetLog),
70 url_request_context_(url_request_context.Pass()), 99 url_request_context_(url_request_context.Pass()),
71 in_shutdown_(false) { 100 in_shutdown_(false) {
72 url_request_context_->set_net_log(net_log_.get()); 101 url_request_context_->set_net_log(net_log_.get());
73 } 102 }
74 103
75 NetworkContext::NetworkContext(const base::FilePath& base_path) 104 NetworkContext::NetworkContext(const base::FilePath& base_path)
76 : NetworkContext(MakeURLRequestContext(base_path)) { 105 : NetworkContext(MakeURLRequestContext(base_path)) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 builder.set_user_agent(mojo::common::GetUserAgent()); 142 builder.set_user_agent(mojo::common::GetUserAgent());
114 builder.set_proxy_service(net::ProxyService::CreateDirect()); 143 builder.set_proxy_service(net::ProxyService::CreateDirect());
115 builder.set_transport_security_persister_path(base_path); 144 builder.set_transport_security_persister_path(base_path);
116 builder.set_data_enabled(true); 145 builder.set_data_enabled(true);
117 146
118 net::URLRequestContextBuilder::HttpCacheParams cache_params; 147 net::URLRequestContextBuilder::HttpCacheParams cache_params;
119 #if defined(OS_ANDROID) 148 #if defined(OS_ANDROID)
120 // On Android, we store the cache on disk becase we can run only a single 149 // On Android, we store the cache on disk becase we can run only a single
121 // instance of the shell at a time. 150 // instance of the shell at a time.
122 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; 151 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK;
123 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); 152 cache_params.path = GetCacheDirectory(base_path);
124 #else 153 #else
125 // On desktop, we store the cache in memory so we can run many shells 154 // On desktop, we store the cache in memory so we can run many shells
126 // in parallel when running tests, otherwise the network services in each 155 // in parallel when running tests, otherwise the network services in each
127 // shell will corrupt the disk cache. 156 // shell will corrupt the disk cache.
128 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; 157 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY;
129 #endif 158 #endif
130 159
131 builder.EnableHttpCache(cache_params); 160 builder.EnableHttpCache(cache_params);
132 builder.set_file_enabled(true); 161 builder.set_file_enabled(true);
133 162
134 return make_scoped_ptr(builder.Build()); 163 return make_scoped_ptr(builder.Build());
135 } 164 }
136 165
137 } // namespace mojo 166 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698