| 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 #include "base/ios/ios_util.h" | 10 #include "base/ios/ios_util.h" |
| 11 #import "base/ios/weak_nsobject.h" | 11 #import "base/ios/weak_nsobject.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.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() { | 29 WKUserScript* InternalGetEarlyPageScript() { |
| 26 return [[[WKUserScript alloc] | 30 return [[WKUserScript alloc] |
| 27 initWithSource:GetEarlyPageScript() | 31 initWithSource:GetEarlyPageScript() |
| 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 10 matching lines...) Expand all Loading... |
| 50 bool is_off_the_record) | 54 bool is_off_the_record) |
| 51 : is_off_the_record_(is_off_the_record) {} | 55 : is_off_the_record_(is_off_the_record) {} |
| 52 | 56 |
| 53 WKWebViewConfigurationProvider::~WKWebViewConfigurationProvider() { | 57 WKWebViewConfigurationProvider::~WKWebViewConfigurationProvider() { |
| 54 } | 58 } |
| 55 | 59 |
| 56 WKWebViewConfiguration* | 60 WKWebViewConfiguration* |
| 57 WKWebViewConfigurationProvider::GetWebViewConfiguration() { | 61 WKWebViewConfigurationProvider::GetWebViewConfiguration() { |
| 58 DCHECK([NSThread isMainThread]); | 62 DCHECK([NSThread isMainThread]); |
| 59 if (!configuration_) { | 63 if (!configuration_) { |
| 60 configuration_.reset([[WKWebViewConfiguration alloc] init]); | 64 configuration_ = [[WKWebViewConfiguration alloc] init]; |
| 61 if (is_off_the_record_) { | 65 if (is_off_the_record_) { |
| 62 [configuration_ | 66 [configuration_ |
| 63 setWebsiteDataStore:[WKWebsiteDataStore nonPersistentDataStore]]; | 67 setWebsiteDataStore:[WKWebsiteDataStore nonPersistentDataStore]]; |
| 64 } | 68 } |
| 65 // TODO(crbug.com/620878) Remove these guards after moving to iOS10 SDK. | 69 // TODO(crbug.com/620878) Remove these guards after moving to iOS10 SDK. |
| 66 #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 | 70 #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 |
| 67 if (base::ios::IsRunningOnIOS10OrLater()) { | 71 if (base::ios::IsRunningOnIOS10OrLater()) { |
| 68 [configuration_ setDataDetectorTypes:WKDataDetectorTypeCalendarEvent | | 72 [configuration_ setDataDetectorTypes:WKDataDetectorTypeCalendarEvent | |
| 69 WKDataDetectorTypeFlightNumber]; | 73 WKDataDetectorTypeFlightNumber]; |
| 70 } | 74 } |
| 71 #endif | 75 #endif |
| 72 // API available on iOS 9, although doesn't appear to enable inline playback | 76 // API available on iOS 9, although doesn't appear to enable inline playback |
| 73 // Works as intended on iOS 10+ | 77 // Works as intended on iOS 10+ |
| 74 [configuration_ setAllowsInlineMediaPlayback:YES]; | 78 [configuration_ setAllowsInlineMediaPlayback:YES]; |
| 75 // setJavaScriptCanOpenWindowsAutomatically is required to support popups. | 79 // setJavaScriptCanOpenWindowsAutomatically is required to support popups. |
| 76 [[configuration_ preferences] setJavaScriptCanOpenWindowsAutomatically:YES]; | 80 [[configuration_ preferences] setJavaScriptCanOpenWindowsAutomatically:YES]; |
| 77 [[configuration_ userContentController] | 81 [[configuration_ userContentController] |
| 78 addUserScript:InternalGetEarlyPageScript()]; | 82 addUserScript:InternalGetEarlyPageScript()]; |
| 79 } | 83 } |
| 80 // Prevent callers from changing the internals of configuration. | 84 // Prevent callers from changing the internals of configuration. |
| 81 return [[configuration_ copy] autorelease]; | 85 return [configuration_ copy]; |
| 82 } | 86 } |
| 83 | 87 |
| 84 CRWWKScriptMessageRouter* | 88 CRWWKScriptMessageRouter* |
| 85 WKWebViewConfigurationProvider::GetScriptMessageRouter() { | 89 WKWebViewConfigurationProvider::GetScriptMessageRouter() { |
| 86 DCHECK([NSThread isMainThread]); | 90 DCHECK([NSThread isMainThread]); |
| 87 if (!router_) { | 91 if (!router_) { |
| 88 WKUserContentController* userContentController = | 92 WKUserContentController* userContentController = |
| 89 [GetWebViewConfiguration() userContentController]; | 93 [GetWebViewConfiguration() userContentController]; |
| 90 router_.reset([[CRWWKScriptMessageRouter alloc] | 94 router_ = [[CRWWKScriptMessageRouter alloc] |
| 91 initWithUserContentController:userContentController]); | 95 initWithUserContentController:userContentController]; |
| 92 } | 96 } |
| 93 return router_; | 97 return router_; |
| 94 } | 98 } |
| 95 | 99 |
| 96 void WKWebViewConfigurationProvider::Purge() { | 100 void WKWebViewConfigurationProvider::Purge() { |
| 97 DCHECK([NSThread isMainThread]); | 101 DCHECK([NSThread isMainThread]); |
| 98 #if !defined(NDEBUG) || !defined(DCHECK_ALWAYS_ON) // Matches DCHECK_IS_ON. | 102 #if !defined(NDEBUG) || !defined(DCHECK_ALWAYS_ON) // Matches DCHECK_IS_ON. |
| 99 base::WeakNSObject<id> weak_configuration(configuration_); | 103 __weak id weak_configuration = configuration_; |
| 100 base::WeakNSObject<id> weak_router(router_); | 104 __weak id weak_router = router_; |
| 101 base::WeakNSObject<id> weak_process_pool([configuration_ processPool]); | 105 __weak id weak_process_pool = [configuration_ processPool]; |
| 106 // TODO(crbug.com/522672): Remove this pragma. |
| 107 #pragma unused(weak_process_pool) |
| 102 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 108 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 103 configuration_.reset(); | 109 configuration_ = nil; |
| 104 router_.reset(); | 110 router_ = nil; |
| 105 // Make sure that no one retains configuration, router, processPool. | 111 // Make sure that no one retains configuration, router, processPool. |
| 106 DCHECK(!weak_configuration); | 112 DCHECK(!weak_configuration); |
| 107 DCHECK(!weak_router); | 113 DCHECK(!weak_router); |
| 108 // TODO(crbug.com/522672): Enable this DCHECK. | 114 // TODO(crbug.com/522672): Enable this DCHECK. |
| 109 // DCHECK(!weak_process_pool); | 115 // DCHECK(!weak_process_pool); |
| 110 } | 116 } |
| 111 | 117 |
| 112 } // namespace web | 118 } // namespace web |
| OLD | NEW |