Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Unified Diff: third_party/WebKit/Source/core/experiments/APIKey.cpp

Issue 1521063003: Add API Key parsing for experimental APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/experiments/APIKey.cpp
diff --git a/third_party/WebKit/Source/core/experiments/APIKey.cpp b/third_party/WebKit/Source/core/experiments/APIKey.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..30299a594a0e2341ef9a7a3aac1cf80d1020f346
--- /dev/null
+++ b/third_party/WebKit/Source/core/experiments/APIKey.cpp
@@ -0,0 +1,89 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/experiments/APIKey.h"
+
+#include "core/dom/ElementTraversal.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/frame/LocalDOMWindow.h"
+#include "core/html/HTMLHeadElement.h"
+#include "core/html/HTMLMetaElement.h"
+#include "platform/RuntimeEnabledFeatures.h"
+#include "platform/weborigin/SecurityOrigin.h"
+
+#include <string>
+
+namespace blink {
+
+PassRefPtrWillBeRawPtr<APIKey> APIKey::parse(const String& keyText)
+{
+ if (keyText.isEmpty()) {
+ return nullptr;
+ }
+
+ // API Key should resemble:
+ // signature:origin:apiName:expiry
+ Vector<String> parts;
+ keyText.split('|', parts);
+ if (parts.size() != 4) {
chasej 2015/12/15 19:43:41 Combined with the expiry integer check, this will
iclelland 2015/12/16 17:04:37 Acknowledged. For now, I've gone with the simplest
+ return nullptr;
+ }
+
+ const String& signature = parts[0];
+ const String& originText = parts[1];
+ const String& apiName = parts[2];
+ const String& expiryText = parts[3];
+
+ // Expiry field must be an integer
+ bool expiryIsValid = false;
+ uint64_t expiry = expiryText.toUInt64Strict(&expiryIsValid);
+ if (!expiryIsValid) {
+ return nullptr;
+ }
+
+ // Origin field must be a valid URL
+ KURL origin = KURL(KURL(), originText);
+ if (!origin.isValid()) {
+ return nullptr;
+ }
+
+ return adoptRefWillBeNoop(new APIKey(signature, origin, apiName, expiry));
+}
+
+APIKey::APIKey(const String& signature, const KURL& origin, const String& apiName, uint64_t expiry)
+ : m_signature(signature)
+ , m_origin(origin)
+ , m_apiName(apiName)
+ , m_expiry(expiry)
+{
+}
+
+bool APIKey::isValidNowForOrigin(const String& origin, const String& apiName, uint64_t now) const
+{
+ return validateOrigin(origin)
+ && validateApiName(apiName)
+ && validateDate(now);
+}
+
+bool APIKey::validateOrigin(const String& originText) const
+{
+ KURL origin = KURL(KURL(), originText);
+ if (!origin.isValid()) {
+ return false;
+ }
+ return origin == m_origin;
+}
+
+bool APIKey::validateApiName(const String& apiName) const
+{
+ return equalIgnoringCase(m_apiName, apiName);
+}
+
+bool APIKey::validateDate(uint64_t now) const
+{
+ return m_expiry > now;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698