Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 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_licenses_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 class MediaLicensesCounterTest : public InProcessBrowserTest { | |
| 31 public: | |
| 32 void SetUpOnMainThread() override { SetMediaLicenseDeletionPref(true); } | |
| 33 | |
| 34 void SetMediaLicenseDeletionPref(bool value) { | |
| 35 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDeleteMediaLicenses, | |
| 36 value); | |
| 37 } | |
| 38 | |
| 39 // Create some test data for origin |kOrigin|. | |
| 40 void CreateMediaLicenseTestData() { | |
| 41 storage::FileSystemContext* filesystem_context = | |
| 42 content::BrowserContext::GetDefaultStoragePartition( | |
| 43 browser()->profile()) | |
| 44 ->GetFileSystemContext(); | |
| 45 std::string clearkey_fsid = | |
| 46 CreateFileSystem(filesystem_context, kClearKeyCdmPluginId, kOrigin); | |
| 47 CreateFile(filesystem_context, kOrigin, clearkey_fsid, "foo"); | |
| 48 } | |
| 49 | |
| 50 // Start running and allow the asynchronous IO operations to complete. | |
| 51 void RunAndWaitForResult() { | |
| 52 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 53 run_loop_.reset(new base::RunLoop()); | |
| 54 run_loop_->Run(); | |
| 55 } | |
| 56 | |
| 57 // Callback from the counter. | |
| 58 void CountingCallback(std::unique_ptr<BrowsingDataCounter::Result> result) { | |
| 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 60 | |
| 61 callback_called_ = true; | |
| 62 finished_ = result->Finished(); | |
| 63 if (finished_) { | |
| 64 MediaLicenseCounter::MediaLicenseResult* media_result = | |
| 65 static_cast<MediaLicenseCounter::MediaLicenseResult*>(result.get()); | |
| 66 count_ = media_result->Value(); | |
| 67 origin_ = media_result->GetOneOrigin(); | |
| 68 } | |
| 69 | |
| 70 if (run_loop_ && finished_) | |
| 71 run_loop_->Quit(); | |
| 72 } | |
| 73 | |
| 74 bool CallbackCalled() { return callback_called_; } | |
| 75 | |
| 76 BrowsingDataCounter::ResultInt GetCount() { | |
| 77 DCHECK(finished_); | |
| 78 return count_; | |
| 79 } | |
| 80 | |
| 81 const std::string& GetOrigin() { | |
| 82 DCHECK(finished_); | |
| 83 return origin_; | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 // Creates a PluginPrivateFileSystem for the |plugin_name| and |origin| | |
| 88 // provided. Returns the file system ID for the created | |
| 89 // PluginPrivateFileSystem. | |
| 90 std::string CreateFileSystem(storage::FileSystemContext* filesystem_context, | |
| 91 const std::string& plugin_name, | |
| 92 const GURL& origin) { | |
| 93 std::string fsid = storage::IsolatedContext::GetInstance() | |
| 94 ->RegisterFileSystemForVirtualPath( | |
| 95 storage::kFileSystemTypePluginPrivate, | |
| 96 ppapi::kPluginPrivateRootName, base::FilePath()); | |
| 97 EXPECT_TRUE(storage::ValidateIsolatedFileSystemId(fsid)); | |
| 98 filesystem_context->OpenPluginPrivateFileSystem( | |
| 99 origin, storage::kFileSystemTypePluginPrivate, fsid, plugin_name, | |
| 100 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 101 base::Bind(&MediaLicensesCounterTest::OnFileSystemOpened, | |
| 102 base::Unretained(this))); | |
| 103 RunAndWaitForResult(); | |
| 104 return fsid; | |
| 105 } | |
| 106 | |
| 107 // Creates a file named |file_name| in the PluginPrivateFileSystem identified | |
| 108 // by |origin| and |fsid|. | |
| 109 void CreateFile(storage::FileSystemContext* filesystem_context, | |
| 110 const GURL& origin, | |
| 111 const std::string& fsid, | |
| 112 const std::string& file_name) { | |
| 113 std::string root = storage::GetIsolatedFileSystemRootURIString( | |
| 114 origin, fsid, ppapi::kPluginPrivateRootName); | |
| 115 storage::FileSystemURL file_url = | |
| 116 filesystem_context->CrackURL(GURL(root + file_name)); | |
| 117 storage::AsyncFileUtil* file_util = filesystem_context->GetAsyncFileUtil( | |
| 118 storage::kFileSystemTypePluginPrivate); | |
| 119 std::unique_ptr<storage::FileSystemOperationContext> operation_context = | |
| 120 base::WrapUnique( | |
| 121 new storage::FileSystemOperationContext(filesystem_context)); | |
| 122 operation_context->set_allowed_bytes_growth( | |
| 123 storage::QuotaManager::kNoLimit); | |
| 124 file_util->EnsureFileExists( | |
| 125 std::move(operation_context), file_url, | |
| 126 base::Bind(&MediaLicensesCounterTest::OnFileCreated, | |
| 127 base::Unretained(this))); | |
| 128 RunAndWaitForResult(); | |
| 129 } | |
| 130 | |
| 131 void OnFileSystemOpened(base::File::Error result) { | |
| 132 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); | |
| 133 if (run_loop_) | |
| 134 run_loop_->Quit(); | |
| 135 } | |
| 136 | |
| 137 void OnFileCreated(base::File::Error result, bool created) { | |
| 138 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); | |
| 139 EXPECT_TRUE(created); | |
| 140 if (run_loop_) | |
| 141 run_loop_->Quit(); | |
| 142 } | |
| 143 | |
| 144 bool callback_called_ = false; | |
| 145 bool finished_ = false; | |
| 146 BrowsingDataCounter::ResultInt count_; | |
| 147 std::string origin_; | |
| 148 | |
| 149 std::unique_ptr<base::RunLoop> run_loop_; | |
| 150 }; | |
| 151 | |
| 152 // Tests that for the empty file system, the result is zero. | |
| 153 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, Empty) { | |
| 154 MediaLicenseCounter counter; | |
| 155 counter.Init(browser()->profile(), | |
| 156 base::Bind(&MediaLicensesCounterTest::CountingCallback, | |
| 157 base::Unretained(this))); | |
| 158 counter.Restart(); | |
| 159 | |
| 160 RunAndWaitForResult(); | |
| 161 | |
| 162 EXPECT_TRUE(CallbackCalled()); | |
| 163 EXPECT_EQ(0u, GetCount()); | |
| 164 EXPECT_TRUE(GetOrigin().empty()); | |
| 165 } | |
| 166 | |
| 167 // Tests that for a non-empty cache, the result is nonzero. | |
|
msramek
2016/06/28 09:07:51
Copy&paste? :)
jrummell
2016/06/28 18:12:47
Yup. Done.
| |
| 168 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, NonEmpty) { | |
| 169 CreateMediaLicenseTestData(); | |
| 170 | |
| 171 MediaLicenseCounter counter; | |
| 172 counter.Init(browser()->profile(), | |
| 173 base::Bind(&MediaLicensesCounterTest::CountingCallback, | |
| 174 base::Unretained(this))); | |
| 175 counter.Restart(); | |
| 176 | |
| 177 RunAndWaitForResult(); | |
| 178 | |
| 179 EXPECT_TRUE(CallbackCalled()); | |
| 180 EXPECT_EQ(1u, GetCount()); | |
| 181 EXPECT_EQ(kTestOrigin, GetOrigin()); | |
| 182 } | |
| 183 | |
| 184 // Tests that the counter does not count if the deletion preference is false. | |
| 185 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, PrefIsFalse) { | |
| 186 SetMediaLicenseDeletionPref(false); | |
| 187 | |
| 188 MediaLicenseCounter counter; | |
| 189 counter.Init(browser()->profile(), | |
| 190 base::Bind(&MediaLicensesCounterTest::CountingCallback, | |
| 191 base::Unretained(this))); | |
| 192 counter.Restart(); | |
| 193 | |
| 194 EXPECT_FALSE(CallbackCalled()); | |
| 195 } | |
| 196 | |
| 197 // Tests that the counter starts counting automatically when the deletion | |
| 198 // pref changes to true. | |
| 199 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, PrefChanged) { | |
| 200 SetMediaLicenseDeletionPref(false); | |
| 201 | |
| 202 MediaLicenseCounter counter; | |
| 203 counter.Init(browser()->profile(), | |
| 204 base::Bind(&MediaLicensesCounterTest::CountingCallback, | |
| 205 base::Unretained(this))); | |
| 206 SetMediaLicenseDeletionPref(true); | |
| 207 | |
| 208 RunAndWaitForResult(); | |
| 209 | |
| 210 EXPECT_TRUE(CallbackCalled()); | |
| 211 EXPECT_EQ(0u, GetCount()); | |
| 212 EXPECT_TRUE(GetOrigin().empty()); | |
| 213 } | |
| 214 | |
| 215 } // namespace | |
| OLD | NEW |