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 "ios/web/public/browser_state.h" | 12 #include "ios/web/public/browser_state.h" |
13 #import "ios/web/web_state/js/page_script_util.h" | 13 #import "ios/web/web_state/js/page_script_util.h" |
14 #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" |
15 | 15 |
16 namespace web { | 16 namespace web { |
17 | 17 |
18 namespace { | 18 namespace { |
19 // A key used to associate a WKWebViewConfigurationProvider with a BrowserState. | 19 // A key used to associate a WKWebViewConfigurationProvider with a BrowserState. |
20 const char kWKWebViewConfigProviderKeyName[] = "wk_web_view_config_provider"; | 20 const char kWKWebViewConfigProviderKeyName[] = "wk_web_view_config_provider"; |
21 | 21 |
22 // Returns an autoreleased instance of WKUserScript to be added to | 22 // Returns an autoreleased instance of WKUserScript to be added to |
23 // configuration's userContentController. | 23 // configuration's userContentController. |
24 WKUserScript* InternalGetEarlyPageScript() { | 24 WKUserScript* InternalGetEarlyPageScript(BrowserState* browser_state) { |
25 return [[[WKUserScript alloc] | 25 return [[[WKUserScript alloc] |
26 initWithSource:GetEarlyPageScript() | 26 initWithSource:GetEarlyPageScript(browser_state) |
27 injectionTime:WKUserScriptInjectionTimeAtDocumentStart | 27 injectionTime:WKUserScriptInjectionTimeAtDocumentStart |
28 forMainFrameOnly:YES] autorelease]; | 28 forMainFrameOnly:YES] autorelease]; |
29 } | 29 } |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 // static | 33 // static |
34 WKWebViewConfigurationProvider& | 34 WKWebViewConfigurationProvider& |
35 WKWebViewConfigurationProvider::FromBrowserState(BrowserState* browser_state) { | 35 WKWebViewConfigurationProvider::FromBrowserState(BrowserState* browser_state) { |
36 DCHECK([NSThread isMainThread]); | 36 DCHECK([NSThread isMainThread]); |
37 DCHECK(browser_state); | 37 DCHECK(browser_state); |
38 if (!browser_state->GetUserData(kWKWebViewConfigProviderKeyName)) { | 38 if (!browser_state->GetUserData(kWKWebViewConfigProviderKeyName)) { |
39 bool is_off_the_record = browser_state->IsOffTheRecord(); | |
40 browser_state->SetUserData( | 39 browser_state->SetUserData( |
41 kWKWebViewConfigProviderKeyName, | 40 kWKWebViewConfigProviderKeyName, |
42 new WKWebViewConfigurationProvider(is_off_the_record)); | 41 new WKWebViewConfigurationProvider(browser_state)); |
43 } | 42 } |
44 return *(static_cast<WKWebViewConfigurationProvider*>( | 43 return *(static_cast<WKWebViewConfigurationProvider*>( |
45 browser_state->GetUserData(kWKWebViewConfigProviderKeyName))); | 44 browser_state->GetUserData(kWKWebViewConfigProviderKeyName))); |
46 } | 45 } |
47 | 46 |
48 WKWebViewConfigurationProvider::WKWebViewConfigurationProvider( | 47 WKWebViewConfigurationProvider::WKWebViewConfigurationProvider( |
49 bool is_off_the_record) | 48 BrowserState* browser_state) |
50 : is_off_the_record_(is_off_the_record) {} | 49 : browser_state_(browser_state) {} |
51 | 50 |
52 WKWebViewConfigurationProvider::~WKWebViewConfigurationProvider() { | 51 WKWebViewConfigurationProvider::~WKWebViewConfigurationProvider() { |
53 } | 52 } |
54 | 53 |
55 WKWebViewConfiguration* | 54 WKWebViewConfiguration* |
56 WKWebViewConfigurationProvider::GetWebViewConfiguration() { | 55 WKWebViewConfigurationProvider::GetWebViewConfiguration() { |
57 DCHECK([NSThread isMainThread]); | 56 DCHECK([NSThread isMainThread]); |
58 if (!configuration_) { | 57 if (!configuration_) { |
59 configuration_.reset([[WKWebViewConfiguration alloc] init]); | 58 configuration_.reset([[WKWebViewConfiguration alloc] init]); |
60 if (is_off_the_record_) { | 59 if (browser_state_->IsOffTheRecord()) { |
61 [configuration_ | 60 [configuration_ |
62 setWebsiteDataStore:[WKWebsiteDataStore nonPersistentDataStore]]; | 61 setWebsiteDataStore:[WKWebsiteDataStore nonPersistentDataStore]]; |
63 } | 62 } |
64 // API available on iOS 9, although doesn't appear to enable inline playback | 63 // API available on iOS 9, although doesn't appear to enable inline playback |
65 // Works as intended on iOS 10+ | 64 // Works as intended on iOS 10+ |
66 [configuration_ setAllowsInlineMediaPlayback:YES]; | 65 [configuration_ setAllowsInlineMediaPlayback:YES]; |
67 // setJavaScriptCanOpenWindowsAutomatically is required to support popups. | 66 // setJavaScriptCanOpenWindowsAutomatically is required to support popups. |
68 [[configuration_ preferences] setJavaScriptCanOpenWindowsAutomatically:YES]; | 67 [[configuration_ preferences] setJavaScriptCanOpenWindowsAutomatically:YES]; |
69 [[configuration_ userContentController] | 68 [[configuration_ userContentController] |
70 addUserScript:InternalGetEarlyPageScript()]; | 69 addUserScript:InternalGetEarlyPageScript(browser_state_)]; |
71 } | 70 } |
72 // Prevent callers from changing the internals of configuration. | 71 // Prevent callers from changing the internals of configuration. |
73 return [[configuration_ copy] autorelease]; | 72 return [[configuration_ copy] autorelease]; |
74 } | 73 } |
75 | 74 |
76 CRWWKScriptMessageRouter* | 75 CRWWKScriptMessageRouter* |
77 WKWebViewConfigurationProvider::GetScriptMessageRouter() { | 76 WKWebViewConfigurationProvider::GetScriptMessageRouter() { |
78 DCHECK([NSThread isMainThread]); | 77 DCHECK([NSThread isMainThread]); |
79 if (!router_) { | 78 if (!router_) { |
80 WKUserContentController* userContentController = | 79 WKUserContentController* userContentController = |
(...skipping 14 matching lines...) Expand all Loading... |
95 configuration_.reset(); | 94 configuration_.reset(); |
96 router_.reset(); | 95 router_.reset(); |
97 // Make sure that no one retains configuration, router, processPool. | 96 // Make sure that no one retains configuration, router, processPool. |
98 DCHECK(!weak_configuration); | 97 DCHECK(!weak_configuration); |
99 DCHECK(!weak_router); | 98 DCHECK(!weak_router); |
100 // TODO(crbug.com/522672): Enable this DCHECK. | 99 // TODO(crbug.com/522672): Enable this DCHECK. |
101 // DCHECK(!weak_process_pool); | 100 // DCHECK(!weak_process_pool); |
102 } | 101 } |
103 | 102 |
104 } // namespace web | 103 } // namespace web |
OLD | NEW |