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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/services/network/network_context.cc
diff --git a/mojo/services/network/network_context.cc b/mojo/services/network/network_context.cc
index cd5a8ab828027c2d9b956185e5eed30c494a5f2e..c674fa28f236e4ebc17a58407f6615cbcfa2bc66 100644
--- a/mojo/services/network/network_context.cc
+++ b/mojo/services/network/network_context.cc
@@ -10,6 +10,7 @@
#include "base/base_paths.h"
#include "base/bind.h"
#include "base/command_line.h"
+#include "base/files/file_util.h"
#include "base/path_service.h"
#include "mojo/common/user_agent.h"
#include "mojo/services/network/url_loader_impl.h"
@@ -24,6 +25,10 @@ namespace mojo {
namespace {
// Logs network information to the specified file.
const char kLogNetLog[] = "log-net-log";
+
+base::FilePath GetCacheDirectory(const base::FilePath& base_path) {
+ return base_path.Append(FILE_PATH_LITERAL("Cache"));
+}
}
class NetworkContext::MojoNetLog : public net::NetLog {
@@ -64,6 +69,32 @@ class NetworkContext::MojoNetLog : public net::NetLog {
DISALLOW_COPY_AND_ASSIGN(MojoNetLog);
};
+// static
+void NetworkContext::ClearCache(const base::FilePath& base_path,
+ scoped_refptr<base::TaskRunner> task_runner,
+ const base::Closure& clear_finished_callback) {
+ base::FilePath trash_dir = base_path.Append(FILE_PATH_LITERAL("Trash"));
+ base::CreateDirectory(trash_dir);
+ base::FilePath dest_dir;
+ base::CreateTemporaryDirInDir(trash_dir, "", &dest_dir);
+
+ // Move the current cache directory, if present, into trash.
+ base::FilePath cache_dir = GetCacheDirectory(base_path);
+ if (PathExists(cache_dir)) {
+ base::File::Error error;
+ if (!base::ReplaceFile(cache_dir, dest_dir, &error)) {
+ LOG(ERROR) << "Failed to clear cache content: " << error;
+ }
+ }
+ // Recreate the cache directory.
+ base::CreateDirectory(cache_dir);
+
+ task_runner->PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(base::IgnoreResult(&base::DeleteFile), trash_dir, true),
+ clear_finished_callback);
+}
+
NetworkContext::NetworkContext(
scoped_ptr<net::URLRequestContext> url_request_context)
: net_log_(new MojoNetLog),
@@ -120,7 +151,7 @@ scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext(
// On Android, we store the cache on disk becase we can run only a single
// instance of the shell at a time.
cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK;
- cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache"));
+ cache_params.path = GetCacheDirectory(base_path);
#else
// On desktop, we store the cache in memory so we can run many shells
// in parallel when running tests, otherwise the network services in each
« 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