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

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: Address Ben's comments. 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 const base::Closure& clear_finished_callback) {
76 base::FilePath trash_dir = base_path.Append(FILE_PATH_LITERAL("Trash"));
77 base::CreateDirectory(trash_dir);
78 base::FilePath dest_dir;
79 base::CreateTemporaryDirInDir(trash_dir, "", &dest_dir);
80
81 // Move the current cache directory, if present, into trash.
82 base::FilePath cache_dir = GetCacheDirectory(base_path);
83 if (PathExists(cache_dir)) {
84 base::File::Error error;
85 if (!base::ReplaceFile(cache_dir, dest_dir, &error)) {
86 LOG(ERROR) << "Failed to clear cache content: " << error;
87 }
88 }
89 // Recreate the cache directory.
90 base::CreateDirectory(cache_dir);
91
92 task_runner->PostTaskAndReply(
93 FROM_HERE,
94 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_dir, true),
95 clear_finished_callback);
96 }
97
67 NetworkContext::NetworkContext( 98 NetworkContext::NetworkContext(
68 scoped_ptr<net::URLRequestContext> url_request_context) 99 scoped_ptr<net::URLRequestContext> url_request_context)
69 : net_log_(new MojoNetLog), 100 : net_log_(new MojoNetLog),
70 url_request_context_(url_request_context.Pass()), 101 url_request_context_(url_request_context.Pass()),
71 in_shutdown_(false) { 102 in_shutdown_(false) {
72 url_request_context_->set_net_log(net_log_.get()); 103 url_request_context_->set_net_log(net_log_.get());
73 } 104 }
74 105
75 NetworkContext::NetworkContext(const base::FilePath& base_path) 106 NetworkContext::NetworkContext(const base::FilePath& base_path)
76 : NetworkContext(MakeURLRequestContext(base_path)) { 107 : NetworkContext(MakeURLRequestContext(base_path)) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 builder.set_user_agent(mojo::common::GetUserAgent()); 144 builder.set_user_agent(mojo::common::GetUserAgent());
114 builder.set_proxy_service(net::ProxyService::CreateDirect()); 145 builder.set_proxy_service(net::ProxyService::CreateDirect());
115 builder.set_transport_security_persister_path(base_path); 146 builder.set_transport_security_persister_path(base_path);
116 builder.set_data_enabled(true); 147 builder.set_data_enabled(true);
117 148
118 net::URLRequestContextBuilder::HttpCacheParams cache_params; 149 net::URLRequestContextBuilder::HttpCacheParams cache_params;
119 #if defined(OS_ANDROID) 150 #if defined(OS_ANDROID)
120 // On Android, we store the cache on disk becase we can run only a single 151 // On Android, we store the cache on disk becase we can run only a single
121 // instance of the shell at a time. 152 // instance of the shell at a time.
122 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; 153 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK;
123 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache")); 154 cache_params.path = GetCacheDirectory(base_path);
124 #else 155 #else
125 // On desktop, we store the cache in memory so we can run many shells 156 // 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 157 // in parallel when running tests, otherwise the network services in each
127 // shell will corrupt the disk cache. 158 // shell will corrupt the disk cache.
128 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; 159 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY;
129 #endif 160 #endif
130 161
131 builder.EnableHttpCache(cache_params); 162 builder.EnableHttpCache(cache_params);
132 builder.set_file_enabled(true); 163 builder.set_file_enabled(true);
133 164
134 return make_scoped_ptr(builder.Build()); 165 return make_scoped_ptr(builder.Build());
135 } 166 }
136 167
137 } // namespace mojo 168 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/network/network_context.h ('k') | mojo/services/network/network_service_delegate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698