crw_web_view_content_view.mm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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
  3. // found in the LICENSE file.
  4. #import "ios/web/common/crw_web_view_content_view.h"
  5. #import <WebKit/WebKit.h>
  6. #include <cmath>
  7. #include <limits>
  8. #include "base/check.h"
  9. #include "base/notreached.h"
  10. #if !defined(__has_feature) || !__has_feature(objc_arc)
  11. #error "This file requires ARC support."
  12. #endif
  13. namespace {
  14. // Background color RGB values for the content view which is displayed when the
  15. // |_webView| is offset from the screen due to user interaction. Displaying this
  16. // background color is handled by UIWebView but not WKWebView, so it needs to be
  17. // set in CRWWebViewContentView to support both. The color value matches that
  18. // used by UIWebView.
  19. const CGFloat kBackgroundRGBComponents[] = {0.75f, 0.74f, 0.76f};
  20. } // namespace
  21. @implementation CRWWebViewContentView
  22. @synthesize contentOffset = _contentOffset;
  23. @synthesize contentInset = _contentInset;
  24. @synthesize scrollView = _scrollView;
  25. @synthesize shouldUseViewContentInset = _shouldUseViewContentInset;
  26. @synthesize viewportEdgesAffectedBySafeArea = _viewportEdgesAffectedBySafeArea;
  27. @synthesize viewportInsets = _viewportInsets;
  28. @synthesize webView = _webView;
  29. @synthesize fullscreenState = _fullscreenState;
  30. - (instancetype)initWithWebView:(UIView*)webView
  31. scrollView:(UIScrollView*)scrollView
  32. fullscreenState:(CrFullscreenState)fullscreenState {
  33. self = [super initWithFrame:CGRectZero];
  34. if (self) {
  35. DCHECK(webView);
  36. DCHECK(scrollView);
  37. DCHECK([scrollView isDescendantOfView:webView]);
  38. _webView = webView;
  39. _scrollView = scrollView;
  40. _fullscreenState = fullscreenState;
  41. }
  42. return self;
  43. }
  44. - (instancetype)initForTesting {
  45. return [super initWithFrame:CGRectZero];
  46. }
  47. - (instancetype)initWithCoder:(NSCoder*)decoder {
  48. NOTREACHED();
  49. return nil;
  50. }
  51. - (instancetype)initWithFrame:(CGRect)frame {
  52. NOTREACHED();
  53. return nil;
  54. }
  55. - (void)didMoveToSuperview {
  56. [super didMoveToSuperview];
  57. if (self.superview) {
  58. self.autoresizingMask =
  59. UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  60. [self addSubview:_webView];
  61. self.backgroundColor = [UIColor colorWithRed:kBackgroundRGBComponents[0]
  62. green:kBackgroundRGBComponents[1]
  63. blue:kBackgroundRGBComponents[2]
  64. alpha:1.0];
  65. }
  66. }
  67. - (BOOL)becomeFirstResponder {
  68. return [_webView becomeFirstResponder];
  69. }
  70. - (void)updateFullscreenState:(CrFullscreenState)fullscreenState {
  71. _fullscreenState = fullscreenState;
  72. }
  73. #pragma mark Layout
  74. - (void)setContentOffset:(CGPoint)contentOffset {
  75. if (CGPointEqualToPoint(_contentOffset, contentOffset))
  76. return;
  77. _contentOffset = contentOffset;
  78. [self setNeedsLayout];
  79. }
  80. - (UIEdgeInsets)contentInset {
  81. return self.shouldUseViewContentInset ? [_scrollView contentInset]
  82. : _contentInset;
  83. }
  84. - (void)setContentInset:(UIEdgeInsets)contentInset {
  85. UIEdgeInsets oldInsets = self.contentInset;
  86. CGFloat delta = std::fabs(oldInsets.top - contentInset.top) +
  87. std::fabs(oldInsets.left - contentInset.left) +
  88. std::fabs(oldInsets.bottom - contentInset.bottom) +
  89. std::fabs(oldInsets.right - contentInset.right);
  90. if (delta <= std::numeric_limits<CGFloat>::epsilon())
  91. return;
  92. _contentInset = contentInset;
  93. if (self.shouldUseViewContentInset) {
  94. [_scrollView setContentInset:contentInset];
  95. }
  96. }
  97. - (void)setShouldUseViewContentInset:(BOOL)shouldUseViewContentInset {
  98. if (_shouldUseViewContentInset != shouldUseViewContentInset) {
  99. UIEdgeInsets oldContentInset = self.contentInset;
  100. self.contentInset = UIEdgeInsetsZero;
  101. _shouldUseViewContentInset = shouldUseViewContentInset;
  102. self.contentInset = oldContentInset;
  103. }
  104. }
  105. #pragma mark - CRWViewportAdjusting
  106. // TODO(crbug.com/1064041): Implement.
  107. - (void)updateMinViewportInsets:(UIEdgeInsets)minInsets
  108. maxViewportInsets:(UIEdgeInsets)maxInsets {
  109. }
  110. @end