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" | 7 #import "base/ios/weak_nsobject.h" |
8 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 _windowID.reset([[self class] newUniqueKey]); | 41 _windowID.reset([[self class] newUniqueKey]); |
42 } | 42 } |
43 return self; | 43 return self; |
44 } | 44 } |
45 | 45 |
46 - (void)inject { | 46 - (void)inject { |
47 _windowID.reset([[self class] newUniqueKey]); | 47 _windowID.reset([[self class] newUniqueKey]); |
48 NSString* script = [web::GetPageScript(@"window_id") | 48 NSString* script = [web::GetPageScript(@"window_id") |
49 stringByReplacingOccurrencesOfString:@"$(WINDOW_ID)" | 49 stringByReplacingOccurrencesOfString:@"$(WINDOW_ID)" |
50 withString:_windowID]; | 50 withString:_windowID]; |
| 51 // WKUserScript may not be injected yet. Make windowID script return boolean |
| 52 // indicating whether the injection was successfull. |
| 53 NSString* scriptWithResult = [NSString |
| 54 stringWithFormat:@"if (!window.__gCrWeb) {false; } else { %@; true; }", |
| 55 script]; |
51 | 56 |
52 base::WeakNSObject<CRWJSWindowIDManager> weakSelf(self); | 57 base::WeakNSObject<CRWJSWindowIDManager> weakSelf(self); |
53 [_webView evaluateJavaScript:script | 58 [_webView evaluateJavaScript:scriptWithResult |
54 completionHandler:^(id result, NSError* error) { | 59 completionHandler:^(id result, NSError* error) { |
55 // TODO(crbug.com/628832): Refactor retry logic. | 60 if (error) { |
56 if (error.code == WKErrorJavaScriptExceptionOccurred) { | 61 DCHECK(error.code == WKErrorWebViewInvalidated || |
57 // This can happen if WKUserScript has not been injected yet. | 62 error.code == WKErrorWebContentProcessTerminated); |
58 // Retry if that's the case, because windowID injection is | 63 return; |
59 // critical for the system to function. | 64 } |
| 65 |
| 66 DCHECK_EQ(CFBooleanGetTypeID(), CFGetTypeID(result)); |
| 67 if (![result boolValue]) { |
| 68 // WKUserScript has not been injected yet. Retry window id |
| 69 // injection, because it is critical for the system to |
| 70 // function. |
60 [weakSelf inject]; | 71 [weakSelf inject]; |
61 } | 72 } |
62 }]; | 73 }]; |
63 } | 74 } |
64 | 75 |
65 #pragma mark - Private | 76 #pragma mark - Private |
66 | 77 |
67 + (NSString*)newUniqueKey { | 78 + (NSString*)newUniqueKey { |
68 char randomBytes[kUniqueKeyLength]; | 79 char randomBytes[kUniqueKeyLength]; |
69 crypto::RandBytes(randomBytes, kUniqueKeyLength); | 80 crypto::RandBytes(randomBytes, kUniqueKeyLength); |
70 std::string result = base::HexEncode(randomBytes, kUniqueKeyLength); | 81 std::string result = base::HexEncode(randomBytes, kUniqueKeyLength); |
71 return [base::SysUTF8ToNSString(result) retain]; | 82 return [base::SysUTF8ToNSString(result) retain]; |
72 } | 83 } |
73 | 84 |
74 @end | 85 @end |
OLD | NEW |