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 | |
21 namespace web { | 17 namespace web { |
22 | 18 |
23 namespace { | 19 namespace { |
24 // A key used to associate a WKWebViewConfigurationProvider with a BrowserState. | 20 // A key used to associate a WKWebViewConfigurationProvider with a BrowserState. |
25 const char kWKWebViewConfigProviderKeyName[] = "wk_web_view_config_provider"; | 21 const char kWKWebViewConfigProviderKeyName[] = "wk_web_view_config_provider"; |
26 | 22 |
27 // Returns an autoreleased instance of WKUserScript to be added to | 23 // Returns an autoreleased instance of WKUserScript to be added to |
28 // configuration's userContentController. | 24 // configuration's userContentController. |
29 WKUserScript* InternalGetEarlyPageScript(BrowserState* browser_state) { | 25 WKUserScript* InternalGetEarlyPageScript(BrowserState* browser_state) { |
30 return [[WKUserScript alloc] | 26 return [[[WKUserScript alloc] |
31 initWithSource:GetEarlyPageScript(browser_state) | 27 initWithSource:GetEarlyPageScript(browser_state) |
32 injectionTime:WKUserScriptInjectionTimeAtDocumentStart | 28 injectionTime:WKUserScriptInjectionTimeAtDocumentStart |
33 forMainFrameOnly:YES]; | 29 forMainFrameOnly:YES] autorelease]; |
34 } | 30 } |
35 | 31 |
36 } // namespace | 32 } // namespace |
37 | 33 |
38 // static | 34 // static |
39 WKWebViewConfigurationProvider& | 35 WKWebViewConfigurationProvider& |
40 WKWebViewConfigurationProvider::FromBrowserState(BrowserState* browser_state) { | 36 WKWebViewConfigurationProvider::FromBrowserState(BrowserState* browser_state) { |
41 DCHECK([NSThread isMainThread]); | 37 DCHECK([NSThread isMainThread]); |
42 DCHECK(browser_state); | 38 DCHECK(browser_state); |
43 if (!browser_state->GetUserData(kWKWebViewConfigProviderKeyName)) { | 39 if (!browser_state->GetUserData(kWKWebViewConfigProviderKeyName)) { |
(...skipping 23 matching lines...) Expand all Loading... |
67 } | 63 } |
68 // API available on iOS 9, although doesn't appear to enable inline playback | 64 // API available on iOS 9, although doesn't appear to enable inline playback |
69 // Works as intended on iOS 10+ | 65 // Works as intended on iOS 10+ |
70 [configuration_ setAllowsInlineMediaPlayback:YES]; | 66 [configuration_ setAllowsInlineMediaPlayback:YES]; |
71 // setJavaScriptCanOpenWindowsAutomatically is required to support popups. | 67 // setJavaScriptCanOpenWindowsAutomatically is required to support popups. |
72 [[configuration_ preferences] setJavaScriptCanOpenWindowsAutomatically:YES]; | 68 [[configuration_ preferences] setJavaScriptCanOpenWindowsAutomatically:YES]; |
73 [[configuration_ userContentController] | 69 [[configuration_ userContentController] |
74 addUserScript:InternalGetEarlyPageScript(browser_state_)]; | 70 addUserScript:InternalGetEarlyPageScript(browser_state_)]; |
75 } | 71 } |
76 // Prevent callers from changing the internals of configuration. | 72 // Prevent callers from changing the internals of configuration. |
77 return [configuration_ copy]; | 73 return [[configuration_ copy] autorelease]; |
78 } | 74 } |
79 | 75 |
80 CRWWKScriptMessageRouter* | 76 CRWWKScriptMessageRouter* |
81 WKWebViewConfigurationProvider::GetScriptMessageRouter() { | 77 WKWebViewConfigurationProvider::GetScriptMessageRouter() { |
82 DCHECK([NSThread isMainThread]); | 78 DCHECK([NSThread isMainThread]); |
83 if (!router_) { | 79 if (!router_) { |
84 WKUserContentController* userContentController = | 80 WKUserContentController* userContentController = |
85 [GetWebViewConfiguration() userContentController]; | 81 [GetWebViewConfiguration() userContentController]; |
86 router_.reset([[CRWWKScriptMessageRouter alloc] | 82 router_.reset([[CRWWKScriptMessageRouter alloc] |
87 initWithUserContentController:userContentController]); | 83 initWithUserContentController:userContentController]); |
88 } | 84 } |
89 return router_; | 85 return router_; |
90 } | 86 } |
91 | 87 |
92 void WKWebViewConfigurationProvider::Purge() { | 88 void WKWebViewConfigurationProvider::Purge() { |
93 DCHECK([NSThread isMainThread]); | 89 DCHECK([NSThread isMainThread]); |
94 #if DCHECK_IS_ON() | 90 #if DCHECK_IS_ON() |
95 base::WeakNSObject<id> weak_configuration; | 91 base::WeakNSObject<id> weak_configuration(configuration_); |
96 base::WeakNSObject<id> weak_router; | 92 base::WeakNSObject<id> weak_router(router_); |
97 // It's expected that the weak objects auto-nil by the end of this method. | 93 base::WeakNSObject<id> weak_process_pool([configuration_ processPool]); |
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]; | |
105 #endif // DCHECK_IS_ON() | 94 #endif // DCHECK_IS_ON() |
106 configuration_.reset(); | 95 configuration_.reset(); |
107 router_.reset(); | 96 router_.reset(); |
108 // Make sure that no one retains configuration, router, processPool. | 97 // Make sure that no one retains configuration, router, processPool. |
109 #if DCHECK_IS_ON() | 98 #if DCHECK_IS_ON() |
110 DCHECK(!weak_configuration); | 99 DCHECK(!weak_configuration); |
111 DCHECK(!weak_router); | 100 DCHECK(!weak_router); |
112 // TODO(crbug.com/522672): Enable this DCHECK. | 101 // TODO(crbug.com/522672): Enable this DCHECK. |
113 // DCHECK(!weak_process_pool); | 102 // DCHECK(!weak_process_pool); |
114 #endif // DCHECK_IS_ON() | 103 #endif // DCHECK_IS_ON() |
115 } | 104 } |
116 | 105 |
117 } // namespace web | 106 } // namespace web |
OLD | NEW |