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

Unified Diff: ios/web/web_state/js/crw_js_window_id_manager.mm

Issue 1107083002: Upstream more leaf files in ios/web/web_state (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@web-resync
Patch Set: Created 5 years, 8 months 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
« no previous file with comments | « ios/web/web_state/js/crw_js_window_id_manager.h ('k') | ios/web/web_state/js/resources/core.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/web/web_state/js/crw_js_window_id_manager.mm
diff --git a/ios/web/web_state/js/crw_js_window_id_manager.mm b/ios/web/web_state/js/crw_js_window_id_manager.mm
new file mode 100644
index 0000000000000000000000000000000000000000..d689990b2afc94d8b5648e79b8dc6b5da290c1d7
--- /dev/null
+++ b/ios/web/web_state/js/crw_js_window_id_manager.mm
@@ -0,0 +1,73 @@
+// Copyright 2014 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.
+
+#import "ios/web/web_state/js/crw_js_window_id_manager.h"
+
+#import "base/mac/scoped_nsobject.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/sys_string_conversions.h"
+#include "crypto/random.h"
+
+namespace {
+// Number of random bytes in unique key for window ID. The length of the
+// window ID will be twice this number, as it is hexadecimal encoded.
+const NSInteger kUniqueKeyLength = 16;
+} // namespace
+
+@interface CRWJSWindowIdManager () {
+ base::scoped_nsobject<NSString> _windowId;
+}
+
+// Returns a string of randomized ASCII characters.
+- (NSString*)generateUniqueKey;
+
+@end
+
+@implementation CRWJSWindowIdManager
+
+- (id)initWithReceiver:(CRWJSInjectionReceiver*)receiver {
+ self = [super initWithReceiver:receiver];
+ if (self) {
+ _windowId.reset([[self generateUniqueKey] retain]);
+ }
+ return self;
+}
+
+- (NSString*)windowId {
+ return _windowId;
+}
+
+- (void)setWindowId:(NSString*)value {
+ _windowId.reset([value copy]);
+}
+
+#pragma mark ProtectedMethods
+
+- (NSString*)scriptPath {
+ return @"window_id";
+}
+
+- (NSString*)presenceBeacon {
+ return @"__gCrWeb.windowIdObject";
+}
+
+// It is important to recreate the injection content on every injection, because
+// it cotains the randomly-generated page ID used for security checks.
+- (NSString*)injectionContent {
+ _windowId.reset([[self generateUniqueKey] retain]);
+ NSString* script = [super injectionContent];
+ return [script stringByReplacingOccurrencesOfString:@"$(WINDOW_ID)"
+ withString:_windowId];
+}
+
+#pragma mark - Private
+
+- (NSString*)generateUniqueKey {
+ char randomBytes[kUniqueKeyLength];
+ crypto::RandBytes(randomBytes, kUniqueKeyLength);
+ return
+ base::SysUTF8ToNSString(base::HexEncode(randomBytes, kUniqueKeyLength));
+}
+
+@end
« no previous file with comments | « ios/web/web_state/js/crw_js_window_id_manager.h ('k') | ios/web/web_state/js/resources/core.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698