Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/browsing_data/media_license_counter.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "chrome/browser/ui/browser.h" | |
| 10 #include "chrome/common/pref_names.h" | |
| 11 #include "chrome/test/base/in_process_browser_test.h" | |
| 12 #include "components/prefs/pref_service.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "content/public/browser/storage_partition.h" | |
| 15 #include "ppapi/shared_impl/ppapi_constants.h" | |
| 16 #include "storage/browser/fileapi/async_file_util.h" | |
| 17 #include "storage/browser/fileapi/file_system_context.h" | |
| 18 #include "storage/browser/fileapi/file_system_operation_context.h" | |
| 19 #include "storage/browser/fileapi/isolated_context.h" | |
| 20 #include "storage/browser/quota/quota_manager.h" | |
| 21 #include "storage/common/fileapi/file_system_util.h" | |
| 22 #include "url/gurl.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const char kTestOrigin[] = "https://host1/"; | |
| 27 const GURL kOrigin(kTestOrigin); | |
| 28 const char kClearKeyCdmPluginId[] = "application_x-ppapi-clearkey-cdm"; | |
| 29 | |
| 30 // Helper class that waits for an event, but keeps the MessageLoop running. | |
| 31 class AwaitCompletionHelper { | |
|
xhwang
2016/06/17 06:24:30
Add a TODO to avoid duplicating this class?
jrummell
2016/06/21 00:13:44
Removed it, as the places it is used is all asynch
| |
| 32 public: | |
| 33 AwaitCompletionHelper() : start_(false), already_quit_(false) {} | |
| 34 virtual ~AwaitCompletionHelper() {} | |
| 35 | |
| 36 void BlockUntilNotified() { | |
| 37 if (!already_quit_) { | |
| 38 DCHECK(!start_); | |
| 39 start_ = true; | |
| 40 base::MessageLoop::current()->Run(); | |
| 41 } else { | |
| 42 DCHECK(!start_); | |
| 43 already_quit_ = false; | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 void Notify() { | |
| 48 if (start_) { | |
| 49 DCHECK(!already_quit_); | |
| 50 base::MessageLoop::current()->QuitWhenIdle(); | |
| 51 start_ = false; | |
| 52 } else { | |
| 53 DCHECK(!already_quit_); | |
| 54 already_quit_ = true; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 bool start_; | |
| 60 bool already_quit_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper); | |
| 63 }; | |
| 64 | |
| 65 class MediaLicenseCounterTest : public InProcessBrowserTest { | |
| 66 public: | |
| 67 void SetUpOnMainThread() override { SetMediaLicenseDeletionPref(true); } | |
| 68 | |
| 69 void SetMediaLicenseDeletionPref(bool value) { | |
| 70 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDeleteMediaLicenses, | |
| 71 value); | |
| 72 } | |
| 73 | |
| 74 // Create some test data for origin |kOrigin|. | |
| 75 void CreateMediaLicenseTestData() { | |
| 76 storage::FileSystemContext* filesystem_context = | |
| 77 content::BrowserContext::GetDefaultStoragePartition( | |
| 78 browser()->profile()) | |
| 79 ->GetFileSystemContext(); | |
| 80 std::string clearkey_fsid = | |
| 81 CreateFileSystem(filesystem_context, kClearKeyCdmPluginId, kOrigin); | |
| 82 CreateFile(filesystem_context, kOrigin, clearkey_fsid, "foo"); | |
| 83 } | |
| 84 | |
| 85 // Wait for IO thread operations, such as cache creation, counting, writing, | |
| 86 // deletion etc. | |
| 87 void WaitForIOThread() { | |
|
xhwang
2016/06/17 06:24:30
This is copied from cache_counter_browsertest.cc b
jrummell
2016/06/21 00:13:44
Done (and simplified to use base::MessageLoop like
| |
| 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 89 run_loop_.reset(new base::RunLoop()); | |
| 90 run_loop_->Run(); | |
| 91 } | |
| 92 | |
| 93 // Callback from the counter. | |
| 94 void CountingCallback(std::unique_ptr<BrowsingDataCounter::Result> result) { | |
| 95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 96 | |
| 97 callback_called_ = true; | |
| 98 finished_ = result->Finished(); | |
| 99 if (finished_) { | |
| 100 MediaLicenseCounter::MediaLicenseResult* media_result = | |
| 101 static_cast<MediaLicenseCounter::MediaLicenseResult*>(result.get()); | |
| 102 count_ = media_result->Value(); | |
| 103 origin_ = media_result->GetOneOrigin(); | |
| 104 } | |
| 105 | |
| 106 if (run_loop_ && finished_) | |
| 107 run_loop_->Quit(); | |
| 108 } | |
| 109 | |
| 110 bool CallbackCalled() { return callback_called_; } | |
| 111 | |
| 112 BrowsingDataCounter::ResultInt GetCount() { | |
| 113 DCHECK(finished_); | |
| 114 return count_; | |
| 115 } | |
| 116 | |
| 117 const std::string& GetOrigin() { | |
| 118 DCHECK(finished_); | |
| 119 return origin_; | |
| 120 } | |
| 121 | |
| 122 private: | |
| 123 // Creates a PluginPrivateFileSystem for the |plugin_name| and |origin| | |
| 124 // provided. Returns the file system ID for the created | |
| 125 // PluginPrivateFileSystem. | |
| 126 std::string CreateFileSystem(storage::FileSystemContext* filesystem_context, | |
| 127 const std::string& plugin_name, | |
| 128 const GURL& origin) { | |
| 129 AwaitCompletionHelper await_completion; | |
| 130 std::string fsid = storage::IsolatedContext::GetInstance() | |
| 131 ->RegisterFileSystemForVirtualPath( | |
| 132 storage::kFileSystemTypePluginPrivate, | |
| 133 ppapi::kPluginPrivateRootName, base::FilePath()); | |
| 134 EXPECT_TRUE(storage::ValidateIsolatedFileSystemId(fsid)); | |
| 135 filesystem_context->OpenPluginPrivateFileSystem( | |
| 136 origin, storage::kFileSystemTypePluginPrivate, fsid, plugin_name, | |
| 137 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 138 base::Bind(&MediaLicenseCounterTest::OnFileSystemOpened, | |
| 139 base::Unretained(this), &await_completion)); | |
| 140 await_completion.BlockUntilNotified(); | |
| 141 return fsid; | |
| 142 } | |
| 143 | |
| 144 // Creates a file named |file_name| in the PluginPrivateFileSystem identified | |
| 145 // by |origin| and |fsid|. | |
| 146 void CreateFile(storage::FileSystemContext* filesystem_context, | |
| 147 const GURL& origin, | |
| 148 const std::string& fsid, | |
| 149 const std::string& file_name) { | |
| 150 AwaitCompletionHelper await_completion; | |
| 151 std::string root = storage::GetIsolatedFileSystemRootURIString( | |
| 152 origin, fsid, ppapi::kPluginPrivateRootName); | |
| 153 storage::FileSystemURL file_url = | |
| 154 filesystem_context->CrackURL(GURL(root + file_name)); | |
| 155 storage::AsyncFileUtil* file_util = filesystem_context->GetAsyncFileUtil( | |
| 156 storage::kFileSystemTypePluginPrivate); | |
| 157 std::unique_ptr<storage::FileSystemOperationContext> operation_context = | |
| 158 base::WrapUnique( | |
| 159 new storage::FileSystemOperationContext(filesystem_context)); | |
| 160 operation_context->set_allowed_bytes_growth( | |
| 161 storage::QuotaManager::kNoLimit); | |
| 162 file_util->EnsureFileExists( | |
| 163 std::move(operation_context), file_url, | |
| 164 base::Bind(&MediaLicenseCounterTest::OnFileCreated, | |
| 165 base::Unretained(this), &await_completion)); | |
| 166 await_completion.BlockUntilNotified(); | |
| 167 } | |
| 168 | |
| 169 void OnFileSystemOpened(AwaitCompletionHelper* await_completion, | |
| 170 base::File::Error result) { | |
| 171 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); | |
| 172 await_completion->Notify(); | |
| 173 } | |
| 174 | |
| 175 void OnFileCreated(AwaitCompletionHelper* await_completion, | |
| 176 base::File::Error result, | |
| 177 bool created) { | |
| 178 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); | |
| 179 EXPECT_TRUE(created); | |
| 180 await_completion->Notify(); | |
| 181 } | |
| 182 | |
| 183 bool callback_called_ = false; | |
| 184 bool finished_ = false; | |
| 185 BrowsingDataCounter::ResultInt count_; | |
| 186 std::string origin_; | |
| 187 | |
| 188 std::unique_ptr<base::RunLoop> run_loop_; | |
| 189 }; | |
| 190 | |
| 191 // Tests that for the empty file system, the result is zero. | |
| 192 IN_PROC_BROWSER_TEST_F(MediaLicenseCounterTest, Empty) { | |
| 193 MediaLicenseCounter counter; | |
| 194 counter.Init(browser()->profile(), | |
| 195 base::Bind(&MediaLicenseCounterTest::CountingCallback, | |
| 196 base::Unretained(this))); | |
| 197 counter.Restart(); | |
| 198 | |
| 199 WaitForIOThread(); | |
| 200 | |
| 201 EXPECT_TRUE(CallbackCalled()); | |
| 202 EXPECT_EQ(0u, GetCount()); | |
| 203 EXPECT_TRUE(GetOrigin().empty()); | |
| 204 } | |
| 205 | |
| 206 // Tests that for a non-empty cache, the result is nonzero. | |
| 207 IN_PROC_BROWSER_TEST_F(MediaLicenseCounterTest, NonEmpty) { | |
| 208 CreateMediaLicenseTestData(); | |
| 209 | |
| 210 MediaLicenseCounter counter; | |
| 211 counter.Init(browser()->profile(), | |
| 212 base::Bind(&MediaLicenseCounterTest::CountingCallback, | |
| 213 base::Unretained(this))); | |
| 214 counter.Restart(); | |
| 215 | |
| 216 WaitForIOThread(); | |
| 217 | |
| 218 EXPECT_TRUE(CallbackCalled()); | |
| 219 EXPECT_EQ(1u, GetCount()); | |
| 220 EXPECT_EQ(kTestOrigin, GetOrigin()); | |
| 221 } | |
| 222 | |
| 223 // Tests that the counter does not count if the deletion preference is false. | |
| 224 IN_PROC_BROWSER_TEST_F(MediaLicenseCounterTest, PrefIsFalse) { | |
| 225 SetMediaLicenseDeletionPref(false); | |
| 226 | |
| 227 MediaLicenseCounter counter; | |
| 228 counter.Init(browser()->profile(), | |
| 229 base::Bind(&MediaLicenseCounterTest::CountingCallback, | |
| 230 base::Unretained(this))); | |
| 231 counter.Restart(); | |
| 232 | |
| 233 EXPECT_FALSE(CallbackCalled()); | |
| 234 } | |
| 235 | |
| 236 // Tests that the counter starts counting automatically when the deletion | |
| 237 // pref changes to true. | |
| 238 IN_PROC_BROWSER_TEST_F(MediaLicenseCounterTest, PrefChanged) { | |
| 239 SetMediaLicenseDeletionPref(false); | |
| 240 | |
| 241 MediaLicenseCounter counter; | |
| 242 counter.Init(browser()->profile(), | |
| 243 base::Bind(&MediaLicenseCounterTest::CountingCallback, | |
| 244 base::Unretained(this))); | |
| 245 SetMediaLicenseDeletionPref(true); | |
| 246 | |
| 247 WaitForIOThread(); | |
| 248 | |
| 249 EXPECT_TRUE(CallbackCalled()); | |
| 250 EXPECT_EQ(0u, GetCount()); | |
| 251 EXPECT_TRUE(GetOrigin().empty()); | |
| 252 } | |
| 253 | |
| 254 } // namespace | |
| OLD | NEW |