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_licenses_counter.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/message_loop/message_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 // (which happens when CountingCallback() is called). | |
| 52 void RunAndWaitForResult() { | |
| 53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 54 base::MessageLoop::current()->Run(); | |
|
xhwang
2016/06/21 06:02:41
MessageLoop::Run() is being deprecated. The new wa
michaeln
2016/06/21 20:23:41
Maybe take a look at passwords_counter_browsertest
jrummell
2016/06/23 02:48:55
Thanks for the reference.
jrummell
2016/06/23 02:48:55
Done.
| |
| 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 base::MessageLoop::current()->QuitWhenIdle(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 bool CallbackCalled() { return callback_called_; } | |
| 73 | |
| 74 BrowsingDataCounter::ResultInt GetCount() { | |
| 75 DCHECK(finished_); | |
| 76 return count_; | |
| 77 } | |
| 78 | |
| 79 const std::string& GetOrigin() { | |
| 80 DCHECK(finished_); | |
| 81 return origin_; | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 // Creates a PluginPrivateFileSystem for the |plugin_name| and |origin| | |
| 86 // provided. Returns the file system ID for the created | |
| 87 // PluginPrivateFileSystem. | |
| 88 std::string CreateFileSystem(storage::FileSystemContext* filesystem_context, | |
| 89 const std::string& plugin_name, | |
| 90 const GURL& origin) { | |
| 91 std::string fsid = storage::IsolatedContext::GetInstance() | |
| 92 ->RegisterFileSystemForVirtualPath( | |
| 93 storage::kFileSystemTypePluginPrivate, | |
| 94 ppapi::kPluginPrivateRootName, base::FilePath()); | |
| 95 EXPECT_TRUE(storage::ValidateIsolatedFileSystemId(fsid)); | |
| 96 filesystem_context->OpenPluginPrivateFileSystem( | |
| 97 origin, storage::kFileSystemTypePluginPrivate, fsid, plugin_name, | |
| 98 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 99 base::Bind(&MediaLicensesCounterTest::OnFileSystemOpened, | |
| 100 base::Unretained(this))); | |
| 101 base::MessageLoop::current()->Run(); | |
| 102 return fsid; | |
| 103 } | |
| 104 | |
| 105 // Creates a file named |file_name| in the PluginPrivateFileSystem identified | |
| 106 // by |origin| and |fsid|. | |
| 107 void CreateFile(storage::FileSystemContext* filesystem_context, | |
| 108 const GURL& origin, | |
| 109 const std::string& fsid, | |
| 110 const std::string& file_name) { | |
| 111 std::string root = storage::GetIsolatedFileSystemRootURIString( | |
| 112 origin, fsid, ppapi::kPluginPrivateRootName); | |
| 113 storage::FileSystemURL file_url = | |
| 114 filesystem_context->CrackURL(GURL(root + file_name)); | |
| 115 storage::AsyncFileUtil* file_util = filesystem_context->GetAsyncFileUtil( | |
| 116 storage::kFileSystemTypePluginPrivate); | |
| 117 std::unique_ptr<storage::FileSystemOperationContext> operation_context = | |
| 118 base::WrapUnique( | |
| 119 new storage::FileSystemOperationContext(filesystem_context)); | |
| 120 operation_context->set_allowed_bytes_growth( | |
| 121 storage::QuotaManager::kNoLimit); | |
| 122 file_util->EnsureFileExists( | |
| 123 std::move(operation_context), file_url, | |
| 124 base::Bind(&MediaLicensesCounterTest::OnFileCreated, | |
| 125 base::Unretained(this))); | |
| 126 base::MessageLoop::current()->Run(); | |
| 127 } | |
| 128 | |
| 129 void OnFileSystemOpened(base::File::Error result) { | |
| 130 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); | |
| 131 base::MessageLoop::current()->QuitWhenIdle(); | |
| 132 } | |
| 133 | |
| 134 void OnFileCreated(base::File::Error result, bool created) { | |
| 135 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); | |
| 136 EXPECT_TRUE(created); | |
| 137 base::MessageLoop::current()->QuitWhenIdle(); | |
| 138 } | |
| 139 | |
| 140 bool callback_called_ = false; | |
| 141 bool finished_ = false; | |
| 142 BrowsingDataCounter::ResultInt count_; | |
| 143 std::string origin_; | |
| 144 }; | |
| 145 | |
| 146 // Tests that for the empty file system, the result is zero. | |
| 147 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, Empty) { | |
| 148 MediaLicenseCounter counter; | |
| 149 counter.Init(browser()->profile(), | |
| 150 base::Bind(&MediaLicensesCounterTest::CountingCallback, | |
| 151 base::Unretained(this))); | |
| 152 counter.Restart(); | |
| 153 | |
| 154 RunAndWaitForResult(); | |
| 155 | |
| 156 EXPECT_TRUE(CallbackCalled()); | |
| 157 EXPECT_EQ(0u, GetCount()); | |
| 158 EXPECT_TRUE(GetOrigin().empty()); | |
| 159 } | |
| 160 | |
| 161 // Tests that for a non-empty cache, the result is nonzero. | |
| 162 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, NonEmpty) { | |
| 163 CreateMediaLicenseTestData(); | |
| 164 | |
| 165 MediaLicenseCounter counter; | |
| 166 counter.Init(browser()->profile(), | |
| 167 base::Bind(&MediaLicensesCounterTest::CountingCallback, | |
| 168 base::Unretained(this))); | |
| 169 counter.Restart(); | |
| 170 | |
| 171 RunAndWaitForResult(); | |
| 172 | |
| 173 EXPECT_TRUE(CallbackCalled()); | |
| 174 EXPECT_EQ(1u, GetCount()); | |
| 175 EXPECT_EQ(kTestOrigin, GetOrigin()); | |
| 176 } | |
| 177 | |
| 178 // Tests that the counter does not count if the deletion preference is false. | |
| 179 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, PrefIsFalse) { | |
| 180 SetMediaLicenseDeletionPref(false); | |
| 181 | |
| 182 MediaLicenseCounter counter; | |
| 183 counter.Init(browser()->profile(), | |
| 184 base::Bind(&MediaLicensesCounterTest::CountingCallback, | |
| 185 base::Unretained(this))); | |
| 186 counter.Restart(); | |
| 187 | |
| 188 EXPECT_FALSE(CallbackCalled()); | |
| 189 } | |
| 190 | |
| 191 // Tests that the counter starts counting automatically when the deletion | |
| 192 // pref changes to true. | |
| 193 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, PrefChanged) { | |
| 194 SetMediaLicenseDeletionPref(false); | |
| 195 | |
| 196 MediaLicenseCounter counter; | |
| 197 counter.Init(browser()->profile(), | |
| 198 base::Bind(&MediaLicensesCounterTest::CountingCallback, | |
| 199 base::Unretained(this))); | |
| 200 SetMediaLicenseDeletionPref(true); | |
| 201 | |
| 202 RunAndWaitForResult(); | |
| 203 | |
| 204 EXPECT_TRUE(CallbackCalled()); | |
| 205 EXPECT_EQ(0u, GetCount()); | |
| 206 EXPECT_TRUE(GetOrigin().empty()); | |
| 207 } | |
| 208 | |
| 209 } // namespace | |
| OLD | NEW |