| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "core/experiments/Experiments.h" | |
| 6 | |
| 7 #include "core/dom/ElementTraversal.h" | |
| 8 #include "core/dom/ExceptionCode.h" | |
| 9 #include "core/html/HTMLHeadElement.h" | |
| 10 #include "core/html/HTMLMetaElement.h" | |
| 11 #include "platform/RuntimeEnabledFeatures.h" | |
| 12 #include "platform/weborigin/SecurityOrigin.h" | |
| 13 #include "public/platform/Platform.h" | |
| 14 #include "public/platform/WebApiKeyValidator.h" | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kExperimentsMetaName[] = "api-experiments"; | |
| 21 | |
| 22 String getCurrentOrigin(ExecutionContext* executionContext) | |
| 23 { | |
| 24 return executionContext->securityOrigin()->toString(); | |
| 25 } | |
| 26 | |
| 27 String getDisabledMessage(const String& apiName) | |
| 28 { | |
| 29 // TODO(chasej): Update message with URL to experiments site, when live | |
| 30 return "The '" + apiName + "' API is currently enabled in limited experiment
s. Please see [Chrome experiments website URL] for information on enabling this
experiment on your site."; | |
| 31 } | |
| 32 | |
| 33 bool hasValidAPIKey(ExecutionContext* executionContext, const String& apiName, S
tring* errorMessage, WebApiKeyValidator* validator) | |
| 34 { | |
| 35 bool foundAnyKey = false; | |
| 36 String origin = getCurrentOrigin(executionContext); | |
| 37 | |
| 38 // When in a document, the API key is provided in a meta tag | |
| 39 if (executionContext->isDocument()) { | |
| 40 HTMLHeadElement* head = toDocument(executionContext)->head(); | |
| 41 for (HTMLMetaElement* metaElement = head ? Traversal<HTMLMetaElement>::f
irstChild(*head) : 0; metaElement; metaElement = Traversal<HTMLMetaElement>::nex
tSibling(*metaElement)) { | |
| 42 if (equalIgnoringCase(metaElement->name(), kExperimentsMetaName)) { | |
| 43 foundAnyKey = true; | |
| 44 String keyString = metaElement->content(); | |
| 45 // Check with the validator service to verify the signature. | |
| 46 if (validator->validateApiKey(keyString, origin, apiName)) { | |
| 47 return true; | |
| 48 } | |
| 49 } | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 if (errorMessage) { | |
| 54 if (foundAnyKey) { | |
| 55 *errorMessage = "The provided key(s) are not valid for the '" + apiN
ame + "' API."; | |
| 56 } else { | |
| 57 *errorMessage = getDisabledMessage(apiName); | |
| 58 } | |
| 59 } | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 // static | |
| 66 bool Experiments::isApiEnabled(ExecutionContext* executionContext, const String&
apiName, String* errorMessage, WebApiKeyValidator* validator) | |
| 67 { | |
| 68 if (!RuntimeEnabledFeatures::experimentalFrameworkEnabled()) { | |
| 69 if (errorMessage) { | |
| 70 *errorMessage = "Experimental Framework is not enabled."; | |
| 71 } | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 if (!executionContext) { | |
| 76 ASSERT_NOT_REACHED(); | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 // Experiments are only enabled for secure origins | |
| 81 bool isSecure = errorMessage | |
| 82 ? executionContext->isSecureContext(*errorMessage) | |
| 83 : executionContext->isSecureContext(); | |
| 84 if (!isSecure) { | |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 if (!validator) { | |
| 89 validator = Platform::current()->apiKeyValidator(); | |
| 90 if (!validator) { | |
| 91 if (errorMessage) { | |
| 92 *errorMessage = "Experimental Framework is not enabled."; | |
| 93 } | |
| 94 return false; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 return hasValidAPIKey(executionContext, apiName, errorMessage, validator); | |
| 99 } | |
| 100 | |
| 101 // static | |
| 102 DOMException* Experiments::createApiDisabledException(const String& apiName) | |
| 103 { | |
| 104 return DOMException::create(NotSupportedError, getDisabledMessage(apiName)); | |
| 105 } | |
| 106 | |
| 107 } // namespace blink | |
| OLD | NEW |