| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/web/web_state/js/crw_js_window_id_manager.h" | 5 #import "ios/web/web_state/js/crw_js_window_id_manager.h" |
| 6 | 6 |
| 7 #import "base/ios/weak_nsobject.h" | |
| 8 #import "base/mac/scoped_nsobject.h" | 7 #import "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 11 #include "crypto/random.h" | 10 #include "crypto/random.h" |
| 12 #import "ios/web/web_state/js/page_script_util.h" | 11 #import "ios/web/web_state/js/page_script_util.h" |
| 13 | 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 14 namespace { | 17 namespace { |
| 15 // Number of random bytes in unique key for window ID. The length of the | 18 // Number of random bytes in unique key for window ID. The length of the |
| 16 // window ID will be twice this number, as it is hexadecimal encoded. | 19 // window ID will be twice this number, as it is hexadecimal encoded. |
| 17 const size_t kUniqueKeyLength = 16; | 20 const size_t kUniqueKeyLength = 16; |
| 18 } // namespace | 21 } // namespace |
| 19 | 22 |
| 20 @interface CRWJSWindowIDManager () { | 23 @interface CRWJSWindowIDManager () { |
| 21 // Web view used for script evaluation to inject window ID. | 24 // Web view used for script evaluation to inject window ID. |
| 22 base::scoped_nsobject<WKWebView> _webView; | 25 base::scoped_nsobject<WKWebView> _webView; |
| 23 // Backs up property with the same name. | 26 // Backs up property with the same name. |
| 24 base::scoped_nsobject<NSString> _windowID; | 27 base::scoped_nsobject<NSString> _windowID; |
| 25 } | 28 } |
| 26 | 29 |
| 27 // Returns a string of randomized ASCII characters. | 30 // Returns a string of randomized ASCII characters. |
| 28 + (NSString*)newUniqueKey; | 31 + (NSString*)newUniqueKey; |
| 29 | 32 |
| 30 @end | 33 @end |
| 31 | 34 |
| 32 @implementation CRWJSWindowIDManager | 35 @implementation CRWJSWindowIDManager |
| 33 | 36 |
| 34 - (NSString*)windowID { | 37 - (NSString*)windowID { |
| 35 return _windowID; | 38 return _windowID; |
| 36 } | 39 } |
| 37 | 40 |
| 38 - (instancetype)initWithWebView:(WKWebView*)webView { | 41 - (instancetype)initWithWebView:(WKWebView*)webView { |
| 39 if ((self = [super init])) { | 42 if ((self = [super init])) { |
| 40 _webView.reset([webView retain]); | 43 _webView.reset(webView); |
| 41 _windowID.reset([[self class] newUniqueKey]); | 44 _windowID.reset([[self class] newUniqueKey]); |
| 42 } | 45 } |
| 43 return self; | 46 return self; |
| 44 } | 47 } |
| 45 | 48 |
| 46 - (void)inject { | 49 - (void)inject { |
| 47 _windowID.reset([[self class] newUniqueKey]); | 50 _windowID.reset([[self class] newUniqueKey]); |
| 48 NSString* script = [web::GetPageScript(@"window_id") | 51 NSString* script = [web::GetPageScript(@"window_id") |
| 49 stringByReplacingOccurrencesOfString:@"$(WINDOW_ID)" | 52 stringByReplacingOccurrencesOfString:@"$(WINDOW_ID)" |
| 50 withString:_windowID]; | 53 withString:_windowID]; |
| 51 // WKUserScript may not be injected yet. Make windowID script return boolean | 54 // WKUserScript may not be injected yet. Make windowID script return boolean |
| 52 // indicating whether the injection was successfull. | 55 // indicating whether the injection was successfull. |
| 53 NSString* scriptWithResult = [NSString | 56 NSString* scriptWithResult = [NSString |
| 54 stringWithFormat:@"if (!window.__gCrWeb) {false; } else { %@; true; }", | 57 stringWithFormat:@"if (!window.__gCrWeb) {false; } else { %@; true; }", |
| 55 script]; | 58 script]; |
| 56 | 59 |
| 57 base::WeakNSObject<CRWJSWindowIDManager> weakSelf(self); | 60 __weak CRWJSWindowIDManager* weakSelf = self; |
| 58 [_webView evaluateJavaScript:scriptWithResult | 61 [_webView evaluateJavaScript:scriptWithResult |
| 59 completionHandler:^(id result, NSError* error) { | 62 completionHandler:^(id result, NSError* error) { |
| 60 if (error) { | 63 if (error) { |
| 61 DCHECK(error.code == WKErrorWebViewInvalidated || | 64 DCHECK(error.code == WKErrorWebViewInvalidated || |
| 62 error.code == WKErrorWebContentProcessTerminated); | 65 error.code == WKErrorWebContentProcessTerminated); |
| 63 return; | 66 return; |
| 64 } | 67 } |
| 65 | 68 |
| 66 DCHECK_EQ(CFBooleanGetTypeID(), CFGetTypeID(result)); | 69 DCHECK_EQ(CFBooleanGetTypeID(), |
| 70 CFGetTypeID((__bridge CFTypeRef)result)); |
| 67 if (![result boolValue]) { | 71 if (![result boolValue]) { |
| 68 // WKUserScript has not been injected yet. Retry window id | 72 // WKUserScript has not been injected yet. Retry window id |
| 69 // injection, because it is critical for the system to | 73 // injection, because it is critical for the system to |
| 70 // function. | 74 // function. |
| 71 [weakSelf inject]; | 75 [weakSelf inject]; |
| 72 } | 76 } |
| 73 }]; | 77 }]; |
| 74 } | 78 } |
| 75 | 79 |
| 76 #pragma mark - Private | 80 #pragma mark - Private |
| 77 | 81 |
| 78 + (NSString*)newUniqueKey { | 82 + (NSString*)newUniqueKey { |
| 79 char randomBytes[kUniqueKeyLength]; | 83 char randomBytes[kUniqueKeyLength]; |
| 80 crypto::RandBytes(randomBytes, kUniqueKeyLength); | 84 crypto::RandBytes(randomBytes, kUniqueKeyLength); |
| 81 std::string result = base::HexEncode(randomBytes, kUniqueKeyLength); | 85 std::string result = base::HexEncode(randomBytes, kUniqueKeyLength); |
| 82 return [base::SysUTF8ToNSString(result) retain]; | 86 return base::SysUTF8ToNSString(result); |
| 83 } | 87 } |
| 84 | 88 |
| 85 @end | 89 @end |
| OLD | NEW |