OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #import "ios/web/web_state/js/crw_js_window_id_manager.h" |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "crypto/random.h" |
| 11 |
| 12 namespace { |
| 13 // Number of random bytes in unique key for window ID. The length of the |
| 14 // window ID will be twice this number, as it is hexadecimal encoded. |
| 15 const NSInteger kUniqueKeyLength = 16; |
| 16 } // namespace |
| 17 |
| 18 @interface CRWJSWindowIdManager () { |
| 19 base::scoped_nsobject<NSString> _windowId; |
| 20 } |
| 21 |
| 22 // Returns a string of randomized ASCII characters. |
| 23 - (NSString*)generateUniqueKey; |
| 24 |
| 25 @end |
| 26 |
| 27 @implementation CRWJSWindowIdManager |
| 28 |
| 29 - (id)initWithReceiver:(CRWJSInjectionReceiver*)receiver { |
| 30 self = [super initWithReceiver:receiver]; |
| 31 if (self) { |
| 32 _windowId.reset([[self generateUniqueKey] retain]); |
| 33 } |
| 34 return self; |
| 35 } |
| 36 |
| 37 - (NSString*)windowId { |
| 38 return _windowId; |
| 39 } |
| 40 |
| 41 - (void)setWindowId:(NSString*)value { |
| 42 _windowId.reset([value copy]); |
| 43 } |
| 44 |
| 45 #pragma mark ProtectedMethods |
| 46 |
| 47 - (NSString*)scriptPath { |
| 48 return @"window_id"; |
| 49 } |
| 50 |
| 51 - (NSString*)presenceBeacon { |
| 52 return @"__gCrWeb.windowIdObject"; |
| 53 } |
| 54 |
| 55 // It is important to recreate the injection content on every injection, because |
| 56 // it cotains the randomly-generated page ID used for security checks. |
| 57 - (NSString*)injectionContent { |
| 58 _windowId.reset([[self generateUniqueKey] retain]); |
| 59 NSString* script = [super injectionContent]; |
| 60 return [script stringByReplacingOccurrencesOfString:@"$(WINDOW_ID)" |
| 61 withString:_windowId]; |
| 62 } |
| 63 |
| 64 #pragma mark - Private |
| 65 |
| 66 - (NSString*)generateUniqueKey { |
| 67 char randomBytes[kUniqueKeyLength]; |
| 68 crypto::RandBytes(randomBytes, kUniqueKeyLength); |
| 69 return |
| 70 base::SysUTF8ToNSString(base::HexEncode(randomBytes, kUniqueKeyLength)); |
| 71 } |
| 72 |
| 73 @end |
OLD | NEW |