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