| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/extensions/chrome_requirements_checker.h" | 5 #include "extensions/browser/requirements_checker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 8 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 9 #include "chrome/grit/generated_resources.h" | |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/gpu_feature_checker.h" | 12 #include "content/public/browser/gpu_feature_checker.h" |
| 12 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
| 13 #include "extensions/common/manifest.h" | |
| 14 #include "extensions/common/manifest_handlers/requirements_info.h" | 14 #include "extensions/common/manifest_handlers/requirements_info.h" |
| 15 #include "extensions/strings/grit/extensions_strings.h" |
| 15 #include "gpu/config/gpu_feature_type.h" | 16 #include "gpu/config/gpu_feature_type.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 ChromeRequirementsChecker::ChromeRequirementsChecker() | 21 RequirementsChecker::RequirementsChecker( |
| 21 : pending_requirement_checks_(0), weak_ptr_factory_(this) { | 22 scoped_refptr<const Extension> extension) |
| 22 } | 23 : PreloadCheck(extension), weak_ptr_factory_(this) {} |
| 23 | 24 |
| 24 ChromeRequirementsChecker::~ChromeRequirementsChecker() { | 25 RequirementsChecker::~RequirementsChecker() {} |
| 25 } | |
| 26 | 26 |
| 27 void ChromeRequirementsChecker::Check( | 27 void RequirementsChecker::Start(ResultCallback callback) { |
| 28 const scoped_refptr<const Extension>& extension, | |
| 29 const RequirementsCheckedCallback& callback) { | |
| 30 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 28 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 31 | 29 |
| 32 callback_ = callback; | |
| 33 const RequirementsInfo& requirements = | 30 const RequirementsInfo& requirements = |
| 34 RequirementsInfo::GetRequirements(extension.get()); | 31 RequirementsInfo::GetRequirements(extension()); |
| 35 | 32 |
| 36 if (requirements.npapi) { | |
| 37 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 33 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 38 errors_.push_back( | 34 if (requirements.npapi) |
| 39 l10n_util::GetStringUTF8(IDS_EXTENSION_NPAPI_NOT_SUPPORTED)); | 35 errors_.insert(NPAPI_NOT_SUPPORTED); |
| 40 #endif | 36 #endif |
| 41 } | |
| 42 | 37 |
| 43 if (requirements.window_shape) { | |
| 44 #if !defined(USE_AURA) | 38 #if !defined(USE_AURA) |
| 45 errors_.push_back( | 39 if (requirements.window_shape) |
| 46 l10n_util::GetStringUTF8(IDS_EXTENSION_WINDOW_SHAPE_NOT_SUPPORTED)); | 40 errors_.insert(WINDOW_SHAPE_NOT_SUPPORTED); |
| 47 #endif | 41 #endif |
| 48 } | |
| 49 | 42 |
| 43 callback_ = std::move(callback); |
| 50 if (requirements.webgl) { | 44 if (requirements.webgl) { |
| 51 ++pending_requirement_checks_; | |
| 52 webgl_checker_ = content::GpuFeatureChecker::Create( | 45 webgl_checker_ = content::GpuFeatureChecker::Create( |
| 53 gpu::GPU_FEATURE_TYPE_ACCELERATED_WEBGL, | 46 gpu::GPU_FEATURE_TYPE_ACCELERATED_WEBGL, |
| 54 base::Bind(&ChromeRequirementsChecker::SetWebGLAvailability, | 47 base::Bind(&RequirementsChecker::VerifyWebGLAvailability, |
| 55 weak_ptr_factory_.GetWeakPtr())); | 48 weak_ptr_factory_.GetWeakPtr())); |
| 56 } | |
| 57 | |
| 58 if (pending_requirement_checks_ == 0) { | |
| 59 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 60 base::Bind(callback_, errors_)); | |
| 61 // Reset the callback so any ref-counted bound parameters will get released. | |
| 62 callback_.Reset(); | |
| 63 return; | |
| 64 } | |
| 65 // Running the GPU checkers down here removes any race condition that arises | |
| 66 // from the use of pending_requirement_checks_. | |
| 67 if (webgl_checker_.get()) | |
| 68 webgl_checker_->CheckGpuFeatureAvailability(); | 49 webgl_checker_->CheckGpuFeatureAvailability(); |
| 69 } | 50 } else { |
| 70 | 51 PostRunCallback(); |
| 71 void ChromeRequirementsChecker::SetWebGLAvailability(bool available) { | |
| 72 if (!available) { | |
| 73 errors_.push_back( | |
| 74 l10n_util::GetStringUTF8(IDS_EXTENSION_WEBGL_NOT_SUPPORTED)); | |
| 75 } | |
| 76 MaybeRunCallback(); | |
| 77 } | |
| 78 | |
| 79 void ChromeRequirementsChecker::MaybeRunCallback() { | |
| 80 if (--pending_requirement_checks_ == 0) { | |
| 81 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 82 base::Bind(callback_, errors_)); | |
| 83 // Reset the callback so any ref-counted bound parameters will get released. | |
| 84 callback_.Reset(); | |
| 85 errors_.clear(); | |
| 86 } | 52 } |
| 87 } | 53 } |
| 88 | 54 |
| 55 base::string16 RequirementsChecker::GetErrorMessage() const { |
| 56 // Join the error messages into one string. |
| 57 std::vector<std::string> messages; |
| 58 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 59 if (errors_.count(NPAPI_NOT_SUPPORTED)) { |
| 60 messages.push_back( |
| 61 l10n_util::GetStringUTF8(IDS_EXTENSION_NPAPI_NOT_SUPPORTED)); |
| 62 } |
| 63 #endif |
| 64 if (errors_.count(WEBGL_NOT_SUPPORTED)) { |
| 65 messages.push_back( |
| 66 l10n_util::GetStringUTF8(IDS_EXTENSION_WEBGL_NOT_SUPPORTED)); |
| 67 } |
| 68 #if !defined(USE_AURA) |
| 69 if (errors_.count(WINDOW_SHAPE_NOT_SUPPORTED)) { |
| 70 messages.push_back( |
| 71 l10n_util::GetStringUTF8(IDS_EXTENSION_WINDOW_SHAPE_NOT_SUPPORTED)); |
| 72 } |
| 73 #endif |
| 74 |
| 75 return base::UTF8ToUTF16(base::JoinString(messages, " ")); |
| 76 } |
| 77 |
| 78 void RequirementsChecker::VerifyWebGLAvailability(bool available) { |
| 79 if (!available) |
| 80 errors_.insert(WEBGL_NOT_SUPPORTED); |
| 81 PostRunCallback(); |
| 82 } |
| 83 |
| 84 void RequirementsChecker::PostRunCallback() { |
| 85 // TODO(michaelpg): This always forces the callback to run asynchronously |
| 86 // to maintain the assumption in |
| 87 // ExtensionService::LoadExtensionsFromCommandLineFlag(). Remove these helper |
| 88 // functions after crbug.com/708354 is addressed. |
| 89 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 90 base::Bind(&RequirementsChecker::RunCallback, |
| 91 weak_ptr_factory_.GetWeakPtr())); |
| 92 } |
| 93 |
| 94 void RequirementsChecker::RunCallback() { |
| 95 DCHECK(callback_); |
| 96 std::move(callback_).Run(errors_); |
| 97 } |
| 98 |
| 89 } // namespace extensions | 99 } // namespace extensions |
| OLD | NEW |