OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/web_view_internal_creation_util.h" | 5 #import "ios/web/web_state/web_view_internal_creation_util.h" |
6 | 6 |
| 7 #import <objc/runtime.h> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #import "base/mac/scoped_nsobject.h" |
8 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
9 #import "ios/web/public/web_client.h" | 12 #import "ios/web/public/web_client.h" |
| 13 #import "ios/web/web_state/ui/crw_context_menu_controller.h" |
10 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" | 14 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" |
11 | 15 |
12 namespace web { | 16 namespace web { |
13 | 17 |
14 namespace { | 18 namespace { |
15 | 19 |
16 // Verifies the preconditions for creating a WKWebView. Must be called before | 20 // Verifies the preconditions for creating a WKWebView. Must be called before |
17 // a WKWebView is allocated. Not verifying the preconditions before creating | 21 // a WKWebView is allocated. Not verifying the preconditions before creating |
18 // a WKWebView will lead to undefined behavior. | 22 // a WKWebView will lead to undefined behavior. |
19 void VerifyWKWebViewCreationPreConditions( | 23 void VerifyWKWebViewCreationPreConditions( |
20 BrowserState* browser_state, | 24 BrowserState* browser_state, |
21 WKWebViewConfiguration* configuration) { | 25 WKWebViewConfiguration* configuration) { |
22 DCHECK(browser_state); | 26 DCHECK(browser_state); |
23 DCHECK(configuration); | 27 DCHECK(configuration); |
24 WKWebViewConfigurationProvider& config_provider = | 28 WKWebViewConfigurationProvider& config_provider = |
25 WKWebViewConfigurationProvider::FromBrowserState(browser_state); | 29 WKWebViewConfigurationProvider::FromBrowserState(browser_state); |
26 DCHECK_EQ([config_provider.GetWebViewConfiguration() processPool], | 30 DCHECK_EQ([config_provider.GetWebViewConfiguration() processPool], |
27 [configuration processPool]); | 31 [configuration processPool]); |
28 } | 32 } |
29 | 33 |
30 } // namespace | 34 } // namespace |
31 | 35 |
32 WKWebView* BuildWKWebView(CGRect frame, | 36 WKWebView* BuildWKWebView(CGRect frame, |
33 WKWebViewConfiguration* configuration, | 37 WKWebViewConfiguration* configuration, |
34 BrowserState* browser_state, | 38 BrowserState* browser_state, |
35 BOOL use_desktop_user_agent) { | 39 BOOL use_desktop_user_agent, |
| 40 id<CRWContextMenuDelegate> context_menu_delegate) { |
36 VerifyWKWebViewCreationPreConditions(browser_state, configuration); | 41 VerifyWKWebViewCreationPreConditions(browser_state, configuration); |
37 | 42 |
38 GetWebClient()->PreWebViewCreation(); | 43 GetWebClient()->PreWebViewCreation(); |
39 WKWebView* web_view = | 44 WKWebView* web_view = |
40 [[WKWebView alloc] initWithFrame:frame configuration:configuration]; | 45 [[WKWebView alloc] initWithFrame:frame configuration:configuration]; |
41 | 46 |
42 // Set the user agent. | 47 // Set the user agent. |
43 web_view.customUserAgent = base::SysUTF8ToNSString( | 48 web_view.customUserAgent = base::SysUTF8ToNSString( |
44 web::GetWebClient()->GetUserAgent(use_desktop_user_agent)); | 49 web::GetWebClient()->GetUserAgent(use_desktop_user_agent)); |
45 | 50 |
46 // By default the web view uses a very sluggish scroll speed. Set it to a more | 51 // By default the web view uses a very sluggish scroll speed. Set it to a more |
47 // reasonable value. | 52 // reasonable value. |
48 web_view.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal; | 53 web_view.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal; |
49 | 54 |
50 // Starting in iOS10, |allowsLinkPreview| defaults to YES. This should be | 55 // Starting in iOS10, |allowsLinkPreview| defaults to YES. This should be |
51 // disabled since the default implementation will open the link in Safari. | 56 // disabled since the default implementation will open the link in Safari. |
52 // TODO(crbug.com/622746): Remove once web// link preview implementation is | 57 // TODO(crbug.com/622746): Remove once web// link preview implementation is |
53 // created. | 58 // created. |
54 web_view.allowsLinkPreview = NO; | 59 web_view.allowsLinkPreview = NO; |
55 | 60 |
| 61 if (context_menu_delegate) { |
| 62 base::scoped_nsobject<CRWContextMenuController> context_menu_controller( |
| 63 [[CRWContextMenuController alloc] |
| 64 initWithWebView:web_view |
| 65 injectionEvaluator:nil |
| 66 delegate:context_menu_delegate]); |
| 67 objc_setAssociatedObject(web_view, context_menu_controller.get(), |
| 68 context_menu_controller.get(), |
| 69 OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 70 } |
| 71 |
56 return [web_view autorelease]; | 72 return [web_view autorelease]; |
57 } | 73 } |
58 | 74 |
59 WKWebView* BuildWKWebView(CGRect frame, | 75 WKWebView* BuildWKWebView(CGRect frame, |
60 WKWebViewConfiguration* configuration, | 76 WKWebViewConfiguration* configuration, |
| 77 BrowserState* browser_state, |
| 78 BOOL use_desktop_user_agent) { |
| 79 return BuildWKWebView(frame, configuration, browser_state, |
| 80 use_desktop_user_agent, nil); |
| 81 } |
| 82 |
| 83 WKWebView* BuildWKWebView(CGRect frame, |
| 84 WKWebViewConfiguration* configuration, |
61 BrowserState* browser_state) { | 85 BrowserState* browser_state) { |
62 BOOL use_desktop_user_agent = NO; | 86 BOOL use_desktop_user_agent = NO; |
63 return BuildWKWebView(frame, configuration, browser_state, | 87 return BuildWKWebView(frame, configuration, browser_state, |
64 use_desktop_user_agent); | 88 use_desktop_user_agent); |
65 } | 89 } |
66 | 90 |
67 } // namespace web | 91 } // namespace web |
OLD | NEW |