summaryrefslogtreecommitdiffstats
path: root/ios/web/web_state/web_view_internal_creation_util.mm
blob: c68c6fdcfc1bc15404676b5de239933f019a70d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/web/web_state/web_view_internal_creation_util.h"

#import <objc/runtime.h>
#import <WebKit/WebKit.h>

#include "base/logging.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/web/alloc_with_zone_interceptor.h"
#include "ios/web/public/active_state_manager.h"
#include "ios/web/public/browser_state.h"
#import "ios/web/public/browsing_data_partition.h"
#include "ios/web/public/web_client.h"
#import "ios/web/public/web_view_counter.h"
#import "ios/web/public/web_view_creation_util.h"
#import "ios/web/weak_nsobject_counter.h"
#import "ios/web/web_state/ui/crw_wk_simple_web_view_controller.h"
#import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
#import "ios/web/web_view_counter_impl.h"

#if !defined(NDEBUG)

namespace {
// Returns the counter of all the active WKWebViews.
// DEPRECATED. Please use web::WebViewCounter instead.
// TODO(shreyasv): Remove this once all callers have stopped using it.
// crbug.com/480507
web::WeakNSObjectCounter& GetActiveWKWebViewCounter() {
  static web::WeakNSObjectCounter active_wk_web_view_counter;
  return active_wk_web_view_counter;
}

// Decides if web views can be created.
bool gAllowWebViewCreation = NO;

// Decides if web views are associated with an ActiveStateManager which is
// active.
bool gWebViewsNeedActiveStateManager = NO;

}  // namespace

@interface WKWebView (CRWAdditions)
@end

@implementation WKWebView (CRWAdditions)

+ (void)load {
  id (^allocator)(Class klass, NSZone* zone) = ^id(Class klass, NSZone* zone) {
    if (web::IsWebViewAllocInitAllowed()) {
      return NSAllocateObject(klass, 0, zone);
    }
    // You have hit this because you are trying to create a WKWebView directly.
    // Please use one of the web::CreateWKWKWebView methods that vend a
    // WKWebView instead.
    NOTREACHED();
    return nil;
  };
  web::AddAllocWithZoneMethod([WKWebView class], allocator);
}

@end

#endif  // !defined(NDEBUG)

namespace web {

namespace {

// Verifies the preconditions for creating a WKWebView. Must be called before
// a WKWebView is allocated. Not verifying the preconditions before creating
// a WKWebView will lead to undefined behavior.
void VerifyWKWebViewCreationPreConditions(
    BrowserState* browser_state,
    WKWebViewConfiguration* configuration) {
  DCHECK(browser_state);
  DCHECK(configuration);
  DCHECK(web::BrowsingDataPartition::IsSynchronized());
  WKWebViewConfigurationProvider& config_provider =
      WKWebViewConfigurationProvider::FromBrowserState(browser_state);
  DCHECK_EQ([config_provider.GetWebViewConfiguration() processPool],
            [configuration processPool]);
}

// Called before a WKWebView is created.
void PreWKWebViewCreation(BrowserState* browser_state) {
  DCHECK(browser_state);
  DCHECK(GetWebClient());
  GetWebClient()->PreWebViewCreation();

#if !defined(NDEBUG)
  if (IsWebViewAllocInitAllowed() && gWebViewsNeedActiveStateManager) {
    DCHECK(BrowserState::GetActiveStateManager(browser_state)->IsActive());
  }
#endif
}

// Called after the WKWebView |web_view| is created.
void PostWKWebViewCreation(WKWebView* web_view, BrowserState* browser_state) {
  DCHECK(web_view);

#if !defined(NDEBUG)
  GetActiveWKWebViewCounter().Insert(web_view);
#endif

  WebViewCounterImpl* web_view_counter =
      WebViewCounterImpl::FromBrowserState(browser_state);
  DCHECK(web_view_counter);
  web_view_counter->InsertWKWebView(web_view);

  // TODO(stuartmorgan): Figure out how to make this work; two different client
  // methods for the two web view types?
  // web::GetWebClient()->PostWebViewCreation(result);
}

}  // namespace

UIWebView* CreateWebView(CGRect frame) {
  DCHECK(web::GetWebClient());
  web::GetWebClient()->PreWebViewCreation();

  UIWebView* result = [[UIWebView alloc] initWithFrame:frame];

  // Disable data detector types. Safari does the same.
  [result setDataDetectorTypes:UIDataDetectorTypeNone];
  [result setScalesPageToFit:YES];

  // By default UIWebView uses a very sluggish scroll speed. Set it to a more
  // reasonable value.
  result.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;

  web::GetWebClient()->PostWebViewCreation(result);

  return result;
}

WKWebView* CreateWKWebView(CGRect frame,
                           WKWebViewConfiguration* configuration,
                           BrowserState* browser_state,
                           BOOL use_desktop_user_agent) {
  WKWebView* web_view = CreateWKWebView(frame, configuration, browser_state);
  DCHECK(web::GetWebClient());
  web_view.customUserAgent = base::SysUTF8ToNSString(
      web::GetWebClient()->GetUserAgent(use_desktop_user_agent));
  return web_view;
}

WKWebView* CreateWKWebView(CGRect frame,
                           WKWebViewConfiguration* configuration,
                           BrowserState* browser_state) {
  VerifyWKWebViewCreationPreConditions(browser_state, configuration);

  PreWKWebViewCreation(browser_state);
#if !defined(NDEBUG)
  bool previous_allow_web_view_creation_value = gAllowWebViewCreation;
  gAllowWebViewCreation = true;
#endif
  WKWebView* result =
      [[WKWebView alloc] initWithFrame:frame configuration:configuration];
#if !defined(NDEBUG)
  gAllowWebViewCreation = previous_allow_web_view_creation_value;
#endif
  PostWKWebViewCreation(result, browser_state);

  // By default the web view uses a very sluggish scroll speed. Set it to a more
  // reasonable value.
  result.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;

  return result;
}

NSUInteger GetActiveWKWebViewsCount() {
#if defined(NDEBUG)
  // This should not be used in release builds.
  CHECK(0);
  return 0;
#else
  return GetActiveWKWebViewCounter().Size();
#endif
}

id<CRWSimpleWebViewController> CreateSimpleWebViewController(
    CGRect frame,
    BrowserState* browser_state) {
  DCHECK(web::BrowsingDataPartition::IsSynchronized());
  base::scoped_nsobject<WKWebView> web_view(
      web::CreateWKWebView(frame, browser_state));
  return [[CRWWKSimpleWebViewController alloc] initWithWKWebView:web_view];
}

#if !defined(NDEBUG)
bool IsWebViewAllocInitAllowed() {
  static dispatch_once_t once_token = 0;
  dispatch_once(&once_token, ^{
    DCHECK(GetWebClient());
    gAllowWebViewCreation = GetWebClient()->AllowWebViewAllocInit();
    if (!gAllowWebViewCreation) {
      gWebViewsNeedActiveStateManager =
          GetWebClient()->WebViewsNeedActiveStateManager();
    }
  });
  return gAllowWebViewCreation;
}
#endif

}  // namespace web