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 "chrome/browser/extensions/requirements_provider.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/string16.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/gpu_feature_checker.h" |
| 11 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/manifest.h" |
| 14 #include "content/public/common/gpu_feature_type.h" |
| 15 |
| 16 namespace keys = extension_manifest_keys; |
| 17 |
| 18 namespace { |
| 19 |
| 20 const char* kWebGlError = "WebGL is not supported"; |
| 21 const char* kCSS3dError = "CSS3d is not supported"; |
| 22 #if defined(OS_CHROMEOS) |
| 23 const char* kPluginsError = "Plugins are not supported"; |
| 24 #endif |
| 25 |
| 26 } // namespace |
| 27 |
| 28 namespace extensions { |
| 29 |
| 30 RequirementsProvider::RequirementsProvider() |
| 31 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 32 } |
| 33 |
| 34 RequirementsProvider::~RequirementsProvider() { |
| 35 weak_factory_.InvalidateWeakPtrs(); |
| 36 } |
| 37 |
| 38 bool RequirementsProvider::Supports(const Extension* extension, |
| 39 std::list<string16>* errors, bool* async, |
| 40 ExtensionService* service) { |
| 41 *async = false; |
| 42 errors->clear(); |
| 43 DictionaryValue* requirements_value = NULL; |
| 44 extension->manifest()->GetDictionary(keys::kRequirements, |
| 45 &requirements_value); |
| 46 if (!requirements_value) |
| 47 return true; |
| 48 |
| 49 for (DictionaryValue::key_iterator it = requirements_value->begin_keys(); |
| 50 it != requirements_value->end_keys(); ++it) { |
| 51 DictionaryValue* requirement_value; |
| 52 requirements_value->GetDictionaryWithoutPathExpansion( |
| 53 *it, &requirement_value); |
| 54 if (!requirement_value) |
| 55 continue; |
| 56 |
| 57 if (*it == "plugins") { |
| 58 #if defined(OS_CHROMEOS) |
| 59 errors->push_back(ASCIIToUTF16(kPluginsError)); |
| 60 #endif |
| 61 } else if (*it == "3D") { |
| 62 ListValue* features; |
| 63 requirement_value->GetListWithoutPathExpansion("features", &features); |
| 64 if (!features) |
| 65 continue; |
| 66 std::string feature; |
| 67 base::ListValue::iterator it; |
| 68 for (it = features->begin(); it != features->end(); ++it) { |
| 69 if ((*it)->GetAsString(&feature)) { |
| 70 if (feature == "webgl") { |
| 71 if (checked_for_webgl_) { |
| 72 if (!webgl_supported_) |
| 73 errors->push_back(ASCIIToUTF16(kWebGlError)); |
| 74 } else { |
| 75 *async = true; |
| 76 // If the check is not ongoing, request one. |
| 77 if (!webgl_checker_.get()) { |
| 78 webgl_checker_ = new GPUFeatureChecker( |
| 79 content::GPU_FEATURE_TYPE_WEBGL, |
| 80 base::Bind(&RequirementsProvider::IsWebGLAvailable, |
| 81 weak_factory_.GetWeakPtr())); |
| 82 webgl_checker_->CheckGPUFeatureAvailability(); |
| 83 } |
| 84 async_webgl_checks_.push_back( |
| 85 GPUFeatureRequest(extension->id(), service->AsWeakPtr())); |
| 86 } |
| 87 } else if (feature == "css3d") { |
| 88 if (checked_for_css3d_) { |
| 89 if (!css3d_supported_) { |
| 90 errors->push_back(ASCIIToUTF16(kCSS3dError)); |
| 91 } |
| 92 } else { |
| 93 *async = true; |
| 94 // If the check is not ongoing, request one. |
| 95 if (!css3d_checker_.get()) { |
| 96 css3d_checker_ = new GPUFeatureChecker( |
| 97 content::GPU_FEATURE_TYPE_ACCELERATED_COMPOSITING, |
| 98 base::Bind(&RequirementsProvider::IsCSS3dAvailable, |
| 99 weak_factory_.GetWeakPtr())); |
| 100 css3d_checker_->CheckGPUFeatureAvailability(); |
| 101 } |
| 102 async_css3d_checks_.push_back( |
| 103 GPUFeatureRequest(extension->id(), service->AsWeakPtr())); |
| 104 } |
| 105 } |
| 106 } |
| 107 } |
| 108 } |
| 109 } |
| 110 return errors->empty(); |
| 111 } |
| 112 |
| 113 void RequirementsProvider::IsWebGLAvailable(bool available) { |
| 114 webgl_supported_ = available; |
| 115 checked_for_webgl_ = true; |
| 116 |
| 117 // The original assumption of true is valid. |
| 118 if (webgl_supported_) { |
| 119 async_webgl_checks_.clear(); |
| 120 return; |
| 121 } |
| 122 |
| 123 // Answer all requests that are pending on the webgl check. |
| 124 GPUFeatureRequest request; |
| 125 while (!async_webgl_checks_.empty()) { |
| 126 request = async_webgl_checks_.front(); |
| 127 async_webgl_checks_.pop_front(); |
| 128 if (request.weak_service.get()) { |
| 129 request.weak_service->UnsupportedRequirements( |
| 130 request.extension_id, ASCIIToUTF16(kWebGlError), |
| 131 true /* disable the extension */); |
| 132 } |
| 133 } |
| 134 } |
| 135 |
| 136 void RequirementsProvider::IsCSS3dAvailable(bool available) { |
| 137 css3d_supported_ = available; |
| 138 checked_for_css3d_ = true; |
| 139 |
| 140 // The original assumption of true is valid. |
| 141 if (css3d_supported_) { |
| 142 async_css3d_checks_.clear(); |
| 143 return; |
| 144 } |
| 145 |
| 146 // Answer all requests that are pending on the css3d check. |
| 147 GPUFeatureRequest request; |
| 148 while (!async_css3d_checks_.empty()) { |
| 149 request = async_css3d_checks_.front(); |
| 150 async_css3d_checks_.pop_front(); |
| 151 if (request.weak_service.get()) { |
| 152 request.weak_service->UnsupportedRequirements( |
| 153 request.extension_id, ASCIIToUTF16(kCSS3dError), |
| 154 true /* disable the extension */); |
| 155 } |
| 156 } |
| 157 } |
| 158 |
| 159 RequirementsProvider::GPUFeatureRequest::GPUFeatureRequest() |
| 160 : extension_id(""), |
| 161 weak_service() { |
| 162 } |
| 163 |
| 164 RequirementsProvider::GPUFeatureRequest::GPUFeatureRequest( |
| 165 const std::string& id, base::WeakPtr<ExtensionService> service) |
| 166 : extension_id(id), |
| 167 weak_service(service) { |
| 168 } |
| 169 |
| 170 RequirementsProvider::GPUFeatureRequest::~GPUFeatureRequest() { |
| 171 } |
| 172 |
| 173 } // namespace extensions |
OLD | NEW |