OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ui/wk_web_view_configuration_provider.h" | 5 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 #import <WebKit/WebKit.h> | 8 #import <WebKit/WebKit.h> |
9 | 9 |
10 #import "base/ios/weak_nsobject.h" | 10 #import "base/ios/weak_nsobject.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "ios/web/public/browser_state.h" | 13 #include "ios/web/public/browser_state.h" |
14 #import "ios/web/web_state/js/page_script_util.h" | 14 #import "ios/web/web_state/js/page_script_util.h" |
15 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" | 15 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" |
16 | 16 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 18 #error "This file requires ARC support." |
| 19 #endif |
| 20 |
17 namespace web { | 21 namespace web { |
18 | 22 |
19 namespace { | 23 namespace { |
20 // A key used to associate a WKWebViewConfigurationProvider with a BrowserState. | 24 // A key used to associate a WKWebViewConfigurationProvider with a BrowserState. |
21 const char kWKWebViewConfigProviderKeyName[] = "wk_web_view_config_provider"; | 25 const char kWKWebViewConfigProviderKeyName[] = "wk_web_view_config_provider"; |
22 | 26 |
23 // Returns an autoreleased instance of WKUserScript to be added to | 27 // Returns an autoreleased instance of WKUserScript to be added to |
24 // configuration's userContentController. | 28 // configuration's userContentController. |
25 WKUserScript* InternalGetEarlyPageScript(BrowserState* browser_state) { | 29 WKUserScript* InternalGetEarlyPageScript(BrowserState* browser_state) { |
26 return [[[WKUserScript alloc] | 30 return [[WKUserScript alloc] |
27 initWithSource:GetEarlyPageScript(browser_state) | 31 initWithSource:GetEarlyPageScript(browser_state) |
28 injectionTime:WKUserScriptInjectionTimeAtDocumentStart | 32 injectionTime:WKUserScriptInjectionTimeAtDocumentStart |
29 forMainFrameOnly:YES] autorelease]; | 33 forMainFrameOnly:YES]; |
30 } | 34 } |
31 | 35 |
32 } // namespace | 36 } // namespace |
33 | 37 |
34 // static | 38 // static |
35 WKWebViewConfigurationProvider& | 39 WKWebViewConfigurationProvider& |
36 WKWebViewConfigurationProvider::FromBrowserState(BrowserState* browser_state) { | 40 WKWebViewConfigurationProvider::FromBrowserState(BrowserState* browser_state) { |
37 DCHECK([NSThread isMainThread]); | 41 DCHECK([NSThread isMainThread]); |
38 DCHECK(browser_state); | 42 DCHECK(browser_state); |
39 if (!browser_state->GetUserData(kWKWebViewConfigProviderKeyName)) { | 43 if (!browser_state->GetUserData(kWKWebViewConfigProviderKeyName)) { |
(...skipping 23 matching lines...) Expand all Loading... |
63 } | 67 } |
64 // API available on iOS 9, although doesn't appear to enable inline playback | 68 // API available on iOS 9, although doesn't appear to enable inline playback |
65 // Works as intended on iOS 10+ | 69 // Works as intended on iOS 10+ |
66 [configuration_ setAllowsInlineMediaPlayback:YES]; | 70 [configuration_ setAllowsInlineMediaPlayback:YES]; |
67 // setJavaScriptCanOpenWindowsAutomatically is required to support popups. | 71 // setJavaScriptCanOpenWindowsAutomatically is required to support popups. |
68 [[configuration_ preferences] setJavaScriptCanOpenWindowsAutomatically:YES]; | 72 [[configuration_ preferences] setJavaScriptCanOpenWindowsAutomatically:YES]; |
69 [[configuration_ userContentController] | 73 [[configuration_ userContentController] |
70 addUserScript:InternalGetEarlyPageScript(browser_state_)]; | 74 addUserScript:InternalGetEarlyPageScript(browser_state_)]; |
71 } | 75 } |
72 // Prevent callers from changing the internals of configuration. | 76 // Prevent callers from changing the internals of configuration. |
73 return [[configuration_ copy] autorelease]; | 77 return [configuration_ copy]; |
74 } | 78 } |
75 | 79 |
76 CRWWKScriptMessageRouter* | 80 CRWWKScriptMessageRouter* |
77 WKWebViewConfigurationProvider::GetScriptMessageRouter() { | 81 WKWebViewConfigurationProvider::GetScriptMessageRouter() { |
78 DCHECK([NSThread isMainThread]); | 82 DCHECK([NSThread isMainThread]); |
79 if (!router_) { | 83 if (!router_) { |
80 WKUserContentController* userContentController = | 84 WKUserContentController* userContentController = |
81 [GetWebViewConfiguration() userContentController]; | 85 [GetWebViewConfiguration() userContentController]; |
82 router_.reset([[CRWWKScriptMessageRouter alloc] | 86 router_.reset([[CRWWKScriptMessageRouter alloc] |
83 initWithUserContentController:userContentController]); | 87 initWithUserContentController:userContentController]); |
84 } | 88 } |
85 return router_; | 89 return router_; |
86 } | 90 } |
87 | 91 |
88 void WKWebViewConfigurationProvider::Purge() { | 92 void WKWebViewConfigurationProvider::Purge() { |
89 DCHECK([NSThread isMainThread]); | 93 DCHECK([NSThread isMainThread]); |
90 #if DCHECK_IS_ON() | 94 #if DCHECK_IS_ON() |
91 base::WeakNSObject<id> weak_configuration(configuration_); | 95 base::WeakNSObject<id> weak_configuration; |
92 base::WeakNSObject<id> weak_router(router_); | 96 base::WeakNSObject<id> weak_router; |
93 base::WeakNSObject<id> weak_process_pool([configuration_ processPool]); | 97 // It's expected that the weak objects auto-nil by the end of this method. |
| 98 // They should not be in any autoreleasepool that might keep them alive. |
| 99 @autoreleasepool { |
| 100 weak_configuration.reset(configuration_); |
| 101 weak_router.reset(router_); |
| 102 } |
| 103 // TODO(crbug.com/522672): See commented out DCHECK below. |
| 104 // __weak id weak_process_pool = [configuration_ processPool]; |
94 #endif // DCHECK_IS_ON() | 105 #endif // DCHECK_IS_ON() |
95 configuration_.reset(); | 106 configuration_.reset(); |
96 router_.reset(); | 107 router_.reset(); |
97 // Make sure that no one retains configuration, router, processPool. | 108 // Make sure that no one retains configuration, router, processPool. |
98 #if DCHECK_IS_ON() | 109 #if DCHECK_IS_ON() |
99 DCHECK(!weak_configuration); | 110 DCHECK(!weak_configuration); |
100 DCHECK(!weak_router); | 111 DCHECK(!weak_router); |
101 // TODO(crbug.com/522672): Enable this DCHECK. | 112 // TODO(crbug.com/522672): Enable this DCHECK. |
102 // DCHECK(!weak_process_pool); | 113 // DCHECK(!weak_process_pool); |
103 #endif // DCHECK_IS_ON() | 114 #endif // DCHECK_IS_ON() |
104 } | 115 } |
105 | 116 |
106 } // namespace web | 117 } // namespace web |
OLD | NEW |