OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <vector> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/file_path.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/string_util.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "chrome/browser/extensions/extension_browsertest.h" |
| 15 #include "chrome/browser/extensions/requirements_checker.h" |
| 16 #include "chrome/browser/gpu_blacklist.h" |
| 17 #include "chrome/browser/gpu_util.h" |
| 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/extension_file_util.h" |
| 21 #include "chrome/test/base/test_launcher_utils.h" |
| 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "grit/generated_resources.h" |
| 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/gl/gl_switches.h" |
| 26 |
| 27 namespace extensions { |
| 28 |
| 29 class RequirementsCheckerBrowserTest : public ExtensionBrowserTest { |
| 30 public: |
| 31 void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 32 // In linux, we need to launch GPU process to decide if WebGL is allowed. |
| 33 // Run it on top of osmesa to avoid bot driver issues. |
| 34 #if defined(OS_LINUX) |
| 35 CHECK(test_launcher_utils::OverrideGLImplementation( |
| 36 command_line, gfx::kGLImplementationOSMesaName)) << |
| 37 "kUseGL must not be set multiple times!"; |
| 38 #endif |
| 39 } |
| 40 |
| 41 scoped_refptr<const Extension> LoadExtensionFromDirName( |
| 42 const std::string& extension_dir_name) { |
| 43 FilePath extension_path; |
| 44 std::string load_error; |
| 45 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); |
| 46 extension_path = extension_path.AppendASCII("requirements_checker") |
| 47 .AppendASCII(extension_dir_name); |
| 48 scoped_refptr<const Extension> extension = |
| 49 extension_file_util::LoadExtension(extension_path, Extension::LOAD, |
| 50 0, &load_error); |
| 51 CHECK(load_error.length() == 0u); |
| 52 return extension; |
| 53 } |
| 54 |
| 55 void ValidateRequirementErrors(std::vector<std::string> expected_errors, |
| 56 std::vector<std::string> actual_errors) { |
| 57 ASSERT_EQ(expected_errors, actual_errors); |
| 58 requirement_errors_.swap(actual_errors); |
| 59 } |
| 60 |
| 61 void BlackListGPUFeatures(std::vector<content::GpuFeatureType> features) { |
| 62 std::vector<std::string> string_features; |
| 63 for (std::vector<content::GpuFeatureType>::iterator it = features.begin(); |
| 64 it != features.end(); |
| 65 ++it) { |
| 66 string_features.push_back( |
| 67 "\"" + gpu_util::GpuFeatureTypeToString(*it) + "\""); |
| 68 } |
| 69 |
| 70 static const std::string json_blacklist = |
| 71 "{\n" |
| 72 " \"name\": \"gpu blacklist\",\n" |
| 73 " \"version\": \"1.0\",\n" |
| 74 " \"entries\": [\n" |
| 75 " {\n" |
| 76 " \"id\": 1,\n" |
| 77 " \"blacklist\": [" + JoinString(string_features, ',') + "]\n" |
| 78 " }\n" |
| 79 " ]\n" |
| 80 "}"; |
| 81 GpuBlacklist* blacklist = GpuBlacklist::GetInstance(); |
| 82 ASSERT_TRUE(blacklist); |
| 83 ASSERT_TRUE(blacklist->LoadGpuBlacklist(json_blacklist, |
| 84 GpuBlacklist::kAllOs)); |
| 85 blacklist->UpdateGpuDataManager(); |
| 86 } |
| 87 |
| 88 protected: |
| 89 std::vector<std::string> requirement_errors_; |
| 90 RequirementsChecker checker_; |
| 91 }; |
| 92 |
| 93 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest, CheckEmptyExtension) { |
| 94 scoped_refptr<const Extension> extension( |
| 95 LoadExtensionFromDirName("no_requirements")); |
| 96 ASSERT_TRUE(extension.get()); |
| 97 checker_.Check(extension, base::Bind( |
| 98 &RequirementsCheckerBrowserTest::ValidateRequirementErrors, |
| 99 base::Unretained(this), std::vector<std::string>())); |
| 100 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 101 } |
| 102 |
| 103 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest, CheckNpapiExtension) { |
| 104 scoped_refptr<const Extension> extension( |
| 105 LoadExtensionFromDirName("require_npapi")); |
| 106 ASSERT_TRUE(extension.get()); |
| 107 |
| 108 std::vector<std::string> expected_errors; |
| 109 // npapi plugins are dissalowd on CROMEOS. |
| 110 #if defined(OS_CHROMEOS) |
| 111 expected_errors.push_back(l10n_util::GetStringUTF8( |
| 112 IDS_EXTENSION_NPAPI_NOT_SUPPORTED)); |
| 113 #endif // defined(OS_CHROMEOS) |
| 114 |
| 115 checker_.Check(extension, base::Bind( |
| 116 &RequirementsCheckerBrowserTest::ValidateRequirementErrors, |
| 117 base::Unretained(this), expected_errors)); |
| 118 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 119 } |
| 120 |
| 121 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest, DisallowGPUFeatures) { |
| 122 scoped_refptr<const Extension> extension( |
| 123 LoadExtensionFromDirName("require_3d")); |
| 124 ASSERT_TRUE(extension.get()); |
| 125 |
| 126 // Backlist webgl |
| 127 std::vector<content::GpuFeatureType> blacklisted_features; |
| 128 blacklisted_features.push_back(content::GPU_FEATURE_TYPE_WEBGL); |
| 129 BlackListGPUFeatures(blacklisted_features); |
| 130 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 131 |
| 132 std::vector<std::string> expected_errors; |
| 133 expected_errors.push_back(l10n_util::GetStringUTF8( |
| 134 IDS_EXTENSION_WEBGL_NOT_SUPPORTED)); |
| 135 |
| 136 checker_.Check(extension, base::Bind( |
| 137 &RequirementsCheckerBrowserTest::ValidateRequirementErrors, |
| 138 base::Unretained(this), expected_errors)); |
| 139 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 140 |
| 141 // Blacklist css3d |
| 142 blacklisted_features.clear(); |
| 143 blacklisted_features.push_back( |
| 144 content::GPU_FEATURE_TYPE_ACCELERATED_COMPOSITING); |
| 145 BlackListGPUFeatures(blacklisted_features); |
| 146 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 147 |
| 148 expected_errors.clear(); |
| 149 expected_errors.push_back(l10n_util::GetStringUTF8( |
| 150 IDS_EXTENSION_CSS3D_NOT_SUPPORTED)); |
| 151 |
| 152 checker_.Check(extension, base::Bind( |
| 153 &RequirementsCheckerBrowserTest::ValidateRequirementErrors, |
| 154 base::Unretained(this), expected_errors)); |
| 155 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 156 |
| 157 // Blacklist both webgl and css3d |
| 158 blacklisted_features.clear(); |
| 159 blacklisted_features.push_back(content::GPU_FEATURE_TYPE_WEBGL); |
| 160 BlackListGPUFeatures(blacklisted_features); |
| 161 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 162 |
| 163 expected_errors.push_back(l10n_util::GetStringUTF8( |
| 164 IDS_EXTENSION_WEBGL_NOT_SUPPORTED)); |
| 165 |
| 166 checker_.Check(extension, base::Bind( |
| 167 &RequirementsCheckerBrowserTest::ValidateRequirementErrors, |
| 168 base::Unretained(this), expected_errors)); |
| 169 } |
| 170 |
| 171 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest, Check3DExtension) { |
| 172 scoped_refptr<const Extension> extension( |
| 173 LoadExtensionFromDirName("require_3d")); |
| 174 ASSERT_TRUE(extension.get()); |
| 175 |
| 176 std::vector<std::string> expected_errors; |
| 177 expected_errors.push_back(l10n_util::GetStringUTF8( |
| 178 IDS_EXTENSION_WEBGL_NOT_SUPPORTED)); |
| 179 |
| 180 checker_.Check(extension, base::Bind( |
| 181 &RequirementsCheckerBrowserTest::ValidateRequirementErrors, |
| 182 base::Unretained(this), std::vector<std::string>())); |
| 183 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 184 } |
| 185 |
| 186 } // extensions |
OLD | NEW |