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