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

Side by Side Diff: content/browser/blob_storage/chrome_blob_storage_context.cc

Issue 2055053003: [BlobAsync] Disk support for blob storage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 2 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 (c) 2012 The Chromium Authors. All rights reserved. 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 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 "content/browser/blob_storage/chrome_blob_storage_context.h" 5 #include "content/browser/blob_storage/chrome_blob_storage_context.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_util.h"
10 #include "base/guid.h" 13 #include "base/guid.h"
14 #include "base/metrics/histogram_macros.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/task_runner.h"
17 #include "base/threading/sequenced_worker_pool.h"
11 #include "content/public/browser/blob_handle.h" 18 #include "content/public/browser/blob_handle.h"
12 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
14 #include "storage/browser/blob/blob_data_builder.h" 21 #include "storage/browser/blob/blob_data_builder.h"
15 #include "storage/browser/blob/blob_data_handle.h" 22 #include "storage/browser/blob/blob_data_handle.h"
16 #include "storage/browser/blob/blob_storage_context.h" 23 #include "storage/browser/blob/blob_storage_context.h"
17 24
25 using base::FilePath;
18 using base::UserDataAdapter; 26 using base::UserDataAdapter;
19 using storage::BlobStorageContext; 27 using storage::BlobStorageContext;
20 28
21 namespace content { 29 namespace content {
22 30
23 namespace { 31 namespace {
32 const char kBlobStorageParentDirectory[] = "blob_storage";
33
34 // Removes all folders in the parent directory except for the
35 // |current_run_dir| folder. If this path is empty, then we delete all folders.
36 void RemoveOldBlobStorageDirectories(FilePath blob_storage_parent,
37 const FilePath& current_run_dir) {
38 if (!base::DirectoryExists(blob_storage_parent)) {
39 return;
40 }
41 base::FileEnumerator enumerator(blob_storage_parent, false,
42 base::FileEnumerator::DIRECTORIES);
43 std::vector<std::string> components;
44 bool success = true;
45 for (base::FilePath name = enumerator.Next(); !name.empty();
46 name = enumerator.Next()) {
47 if (current_run_dir.empty() || name != current_run_dir)
48 success &= base::DeleteFile(name, true);
49 }
50 LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.CleanupSuccess", success);
51 }
24 52
25 const char kBlobStorageContextKeyName[] = "content_blob_storage_context"; 53 const char kBlobStorageContextKeyName[] = "content_blob_storage_context";
26 54
27 class BlobHandleImpl : public BlobHandle { 55 class BlobHandleImpl : public BlobHandle {
28 public: 56 public:
29 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) 57 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle)
30 : handle_(std::move(handle)) {} 58 : handle_(std::move(handle)) {}
31 59
32 ~BlobHandleImpl() override {} 60 ~BlobHandleImpl() override {}
33 61
34 std::string GetUUID() override { return handle_->uuid(); } 62 std::string GetUUID() override { return handle_->uuid(); }
35 63
36 private: 64 private:
37 std::unique_ptr<storage::BlobDataHandle> handle_; 65 std::unique_ptr<storage::BlobDataHandle> handle_;
38 }; 66 };
39 67
40 } // namespace 68 } // namespace
41 69
42 ChromeBlobStorageContext::ChromeBlobStorageContext() {} 70 ChromeBlobStorageContext::ChromeBlobStorageContext() {}
43 71
44 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( 72 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor(
45 BrowserContext* context) { 73 BrowserContext* context) {
46 if (!context->GetUserData(kBlobStorageContextKeyName)) { 74 if (!context->GetUserData(kBlobStorageContextKeyName)) {
47 scoped_refptr<ChromeBlobStorageContext> blob = 75 scoped_refptr<ChromeBlobStorageContext> blob =
48 new ChromeBlobStorageContext(); 76 new ChromeBlobStorageContext();
49 context->SetUserData( 77 context->SetUserData(
50 kBlobStorageContextKeyName, 78 kBlobStorageContextKeyName,
51 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); 79 new UserDataAdapter<ChromeBlobStorageContext>(blob.get()));
52 // Check first to avoid memory leak in unittests. 80
53 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { 81 // Check to avoid memory leak in unittests.
82 bool io_thread_valid = BrowserThread::IsMessageLoopValid(BrowserThread::IO);
83
84 // Resolve our storage directories.
85 base::FilePath blob_storage_parent =
86 context->GetPath().Append(kBlobStorageParentDirectory);
87 base::FilePath blob_storage_dir =
88 blob_storage_parent.Append(base::GenerateGUID());
89
90 // Only populate the task runner if we're not off the record. This enables
91 // paging/saving blob data to disk.
92 scoped_refptr<base::TaskRunner> file_task_runner;
93
94 // If we're not incognito mode, schedule all of our file tasks to enable
95 // disk on the storage context.
96 if (!context->IsOffTheRecord() && io_thread_valid) {
97 file_task_runner =
98 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
99 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
100
101 // Removes our old blob directories if they exist.
102 BrowserThread::PostAfterStartupTask(
103 FROM_HERE, file_task_runner,
104 base::Bind(&RemoveOldBlobStorageDirectories,
105 base::Passed(&blob_storage_parent), blob_storage_dir));
106 }
107
108 if (io_thread_valid) {
54 BrowserThread::PostTask( 109 BrowserThread::PostTask(
55 BrowserThread::IO, FROM_HERE, 110 BrowserThread::IO, FROM_HERE,
56 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); 111 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob,
112 base::Passed(&blob_storage_dir),
113 base::Passed(&file_task_runner)));
57 } 114 }
58 } 115 }
59 116
60 return UserDataAdapter<ChromeBlobStorageContext>::Get( 117 return UserDataAdapter<ChromeBlobStorageContext>::Get(
61 context, kBlobStorageContextKeyName); 118 context, kBlobStorageContextKeyName);
62 } 119 }
63 120
64 void ChromeBlobStorageContext::InitializeOnIOThread() { 121 void ChromeBlobStorageContext::InitializeOnIOThread(
122 base::FilePath blob_storage_dir,
123 scoped_refptr<base::TaskRunner> file_task_runner) {
65 DCHECK_CURRENTLY_ON(BrowserThread::IO); 124 DCHECK_CURRENTLY_ON(BrowserThread::IO);
66 context_.reset(new BlobStorageContext()); 125 context_.reset(new BlobStorageContext(std::move(blob_storage_dir),
126 std::move(file_task_runner)));
67 } 127 }
68 128
69 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( 129 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob(
70 const char* data, 130 const char* data,
71 size_t length) { 131 size_t length) {
72 DCHECK_CURRENTLY_ON(BrowserThread::IO); 132 DCHECK_CURRENTLY_ON(BrowserThread::IO);
73 133
74 std::string uuid(base::GenerateGUID()); 134 std::string uuid(base::GenerateGUID());
75 storage::BlobDataBuilder blob_data_builder(uuid); 135 storage::BlobDataBuilder blob_data_builder(uuid);
76 blob_data_builder.AppendData(data, length); 136 blob_data_builder.AppendData(data, length);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { 171 void ChromeBlobStorageContext::DeleteOnCorrectThread() const {
112 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && 172 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) &&
113 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { 173 !BrowserThread::CurrentlyOn(BrowserThread::IO)) {
114 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); 174 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this);
115 return; 175 return;
116 } 176 }
117 delete this; 177 delete this;
118 } 178 }
119 179
120 } // namespace content 180 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698