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

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

Issue 2516713002: [BlobStorage] Implementing disk. (Closed)
Patch Set: test & windows fix Created 4 years 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
kinuko 2016/11/27 14:45:22 nit: unnecessary empty line
dmurph 2016/11/28 19:20:36 Done.
11 #include "base/files/file.h"
12 #include "base/files/file_enumerator.h"
13 #include "base/files/file_util.h"
10 #include "base/guid.h" 14 #include "base/guid.h"
15 #include "base/metrics/histogram_macros.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/task_runner.h"
18 #include "base/threading/sequenced_worker_pool.h"
11 #include "content/public/browser/blob_handle.h" 19 #include "content/public/browser/blob_handle.h"
12 #include "content/public/browser/browser_context.h" 20 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
14 #include "storage/browser/blob/blob_data_builder.h" 22 #include "storage/browser/blob/blob_data_builder.h"
15 #include "storage/browser/blob/blob_data_handle.h" 23 #include "storage/browser/blob/blob_data_handle.h"
16 #include "storage/browser/blob/blob_storage_context.h" 24 #include "storage/browser/blob/blob_storage_context.h"
17 25
26 using base::FilePath;
18 using base::UserDataAdapter; 27 using base::UserDataAdapter;
19 using storage::BlobStorageContext; 28 using storage::BlobStorageContext;
20 29
21 namespace content { 30 namespace content {
22 31
23 namespace { 32 namespace {
33 const FilePath::CharType kBlobStorageContextKeyName[] =
34 FILE_PATH_LITERAL("content_blob_storage_context");
35 const FilePath::CharType kBlobStorageParentDirectory[] =
36 FILE_PATH_LITERAL("blob_storage");
24 37
25 const char kBlobStorageContextKeyName[] = "content_blob_storage_context"; 38 // Removes all folders in the parent directory except for the
39 // |current_run_dir| folder. If this path is empty, then we delete all folders.
40 void RemoveOldBlobStorageDirectories(FilePath blob_storage_parent,
41 const FilePath& current_run_dir) {
42 if (!base::DirectoryExists(blob_storage_parent)) {
43 return;
44 }
45 base::FileEnumerator enumerator(blob_storage_parent, false,
46 base::FileEnumerator::DIRECTORIES);
47 bool success = true;
48 for (FilePath name = enumerator.Next(); !name.empty();
49 name = enumerator.Next()) {
50 if (current_run_dir.empty() || name != current_run_dir)
51 success &= base::DeleteFile(name, true);
52 }
53 LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.CleanupSuccess", success);
54 }
26 55
27 class BlobHandleImpl : public BlobHandle { 56 class BlobHandleImpl : public BlobHandle {
28 public: 57 public:
29 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) 58 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle)
30 : handle_(std::move(handle)) {} 59 : handle_(std::move(handle)) {}
31 60
32 ~BlobHandleImpl() override {} 61 ~BlobHandleImpl() override {}
33 62
34 std::string GetUUID() override { return handle_->uuid(); } 63 std::string GetUUID() override { return handle_->uuid(); }
35 64
36 private: 65 private:
37 std::unique_ptr<storage::BlobDataHandle> handle_; 66 std::unique_ptr<storage::BlobDataHandle> handle_;
38 }; 67 };
39 68
40 } // namespace 69 } // namespace
41 70
42 ChromeBlobStorageContext::ChromeBlobStorageContext() {} 71 ChromeBlobStorageContext::ChromeBlobStorageContext() {}
43 72
44 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( 73 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor(
45 BrowserContext* context) { 74 BrowserContext* context) {
46 if (!context->GetUserData(kBlobStorageContextKeyName)) { 75 if (!context->GetUserData(kBlobStorageContextKeyName)) {
47 scoped_refptr<ChromeBlobStorageContext> blob = 76 scoped_refptr<ChromeBlobStorageContext> blob =
48 new ChromeBlobStorageContext(); 77 new ChromeBlobStorageContext();
49 context->SetUserData( 78 context->SetUserData(
50 kBlobStorageContextKeyName, 79 kBlobStorageContextKeyName,
51 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); 80 new UserDataAdapter<ChromeBlobStorageContext>(blob.get()));
81
52 // Check first to avoid memory leak in unittests. 82 // Check first to avoid memory leak in unittests.
53 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { 83 bool io_thread_valid = BrowserThread::IsMessageLoopValid(BrowserThread::IO);
84
85 // Resolve our storage directories.
86 FilePath blob_storage_parent =
87 context->GetPath().Append(kBlobStorageParentDirectory);
88 FilePath blob_storage_dir = blob_storage_parent.Append(
89 FilePath::FromUTF8Unsafe(base::GenerateGUID()));
90
91 // Only populate the task runner if we're not off the record. This enables
92 // paging/saving blob data to disk.
93 scoped_refptr<base::TaskRunner> file_task_runner;
94
95 // If we're not incognito mode, schedule all of our file tasks to enable
96 // disk on the storage context.
97 if (!context->IsOffTheRecord() && io_thread_valid) {
98 file_task_runner =
99 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
100 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
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 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);
77 137
78 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 138 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
79 context_->AddFinishedBlob(&blob_data_builder); 139 context_->AddFinishedBlob(&blob_data_builder);
80 if (!blob_data_handle) 140 if (!blob_data_handle)
81 return std::unique_ptr<BlobHandle>(); 141 return std::unique_ptr<BlobHandle>();
82 142
83 std::unique_ptr<BlobHandle> blob_handle( 143 std::unique_ptr<BlobHandle> blob_handle(
84 new BlobHandleImpl(std::move(blob_data_handle))); 144 new BlobHandleImpl(std::move(blob_data_handle)));
85 return blob_handle; 145 return blob_handle;
86 } 146 }
87 147
88 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateFileBackedBlob( 148 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateFileBackedBlob(
89 const base::FilePath& path, 149 const FilePath& path,
90 int64_t offset, 150 int64_t offset,
91 int64_t size, 151 int64_t size,
92 const base::Time& expected_modification_time) { 152 const base::Time& expected_modification_time) {
93 DCHECK_CURRENTLY_ON(BrowserThread::IO); 153 DCHECK_CURRENTLY_ON(BrowserThread::IO);
94 154
95 std::string uuid(base::GenerateGUID()); 155 std::string uuid(base::GenerateGUID());
96 storage::BlobDataBuilder blob_data_builder(uuid); 156 storage::BlobDataBuilder blob_data_builder(uuid);
97 blob_data_builder.AppendFile(path, offset, size, expected_modification_time); 157 blob_data_builder.AppendFile(path, offset, size, expected_modification_time);
98 158
99 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 159 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
(...skipping 11 matching lines...) Expand all
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