Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / ios / web / shell / view_controller.mm
blob16e78fe72955d84dc38ffc0b678983e3e20f934d
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
3 // found in the LICENSE file.
5 #import "ios/web/shell/view_controller.h"
7 #include "base/mac/objc_property_releaser.h"
8 #import "base/mac/scoped_nsobject.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "ios/net/cookies/cookie_store_ios.h"
12 #import "ios/net/crn_http_protocol_handler.h"
13 #import "ios/web/navigation/crw_session_controller.h"
14 #include "ios/web/navigation/web_load_params.h"
15 #import "ios/web/net/crw_url_verifying_protocol_handler.h"
16 #include "ios/web/net/request_tracker_factory_impl.h"
17 #import "ios/web/net/web_http_protocol_handler_delegate.h"
18 #include "ios/web/public/referrer.h"
19 #import "ios/web/public/web_controller_factory.h"
20 #include "ios/web/public/web_state/web_state.h"
21 #include "ios/web/public/web_view_util.h"
22 #include "ios/web/shell/shell_browser_state.h"
23 #include "ios/web/web_state/ui/crw_web_controller.h"
24 #include "ios/web/web_state/web_state_impl.h"
25 #include "ui/base/page_transition_types.h"
27 namespace {
28 // Returns true if WKWebView should be used instead of UIWebView.
29 // TODO(stuartmorgan): Decide on a better way to control this.
30 bool UseWKWebView() {
31 #if defined(FORCE_ENABLE_WKWEBVIEW)
32   return web::IsWKWebViewSupported();
33 #else
34   return false;
35 #endif
39 @interface ViewController () {
40   web::BrowserState* _browserState;
41   base::scoped_nsobject<CRWWebController> _webController;
42   scoped_ptr<web::RequestTrackerFactoryImpl> _requestTrackerFactory;
43   scoped_ptr<web::WebHTTPProtocolHandlerDelegate> _httpProtocolDelegate;
45   base::mac::ObjCPropertyReleaser _propertyReleaser_ViewController;
47 @property(nonatomic, readwrite, retain) UITextField* field;
48 @end
50 @implementation ViewController
52 @synthesize field = _field;
53 @synthesize containerView = _containerView;
54 @synthesize toolbarView = _toolbarView;
56 - (instancetype)initWithBrowserState:(web::BrowserState*)browserState {
57   self = [super initWithNibName:@"MainView" bundle:nil];
58   if (self) {
59     _propertyReleaser_ViewController.Init(self, [ViewController class]);
60     _browserState = browserState;
61   }
62   return self;
65 - (void)dealloc {
66   net::HTTPProtocolHandlerDelegate::SetInstance(nullptr);
67   net::RequestTracker::SetRequestTrackerFactory(nullptr);
68   [super dealloc];
71 - (void)viewDidLoad {
72   [super viewDidLoad];
74   // Set up the toolbar buttons.
75   UIButton* back = [UIButton buttonWithType:UIButtonTypeCustom];
76   [back setImage:[UIImage imageNamed:@"toolbar_back"]
77         forState:UIControlStateNormal];
78   [back setFrame:CGRectMake(0, 0, 44, 44)];
79   [back setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 4, 4)];
80   [back setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
81   [back addTarget:self
82                 action:@selector(back)
83       forControlEvents:UIControlEventTouchUpInside];
85   UIButton* forward = [UIButton buttonWithType:UIButtonTypeCustom];
86   [forward setImage:[UIImage imageNamed:@"toolbar_forward"]
87            forState:UIControlStateNormal];
88   [forward setFrame:CGRectMake(44, 0, 44, 44)];
89   [forward setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 4, 4)];
90   [forward setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
91   [forward addTarget:self
92                 action:@selector(forward)
93       forControlEvents:UIControlEventTouchUpInside];
95   base::scoped_nsobject<UITextField> field([[UITextField alloc]
96       initWithFrame:CGRectMake(88, 6, CGRectGetWidth([_toolbarView frame]) - 98,
97                                31)]);
98   [field setDelegate:self];
99   [field setBackground:[[UIImage imageNamed:@"textfield_background"]
100                            resizableImageWithCapInsets:UIEdgeInsetsMake(
101                                                            12, 12, 12, 12)]];
102   [field setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
103   [field setKeyboardType:UIKeyboardTypeWebSearch];
104   [field setAutocorrectionType:UITextAutocorrectionTypeNo];
105   [field setClearButtonMode:UITextFieldViewModeWhileEditing];
106   self.field = field;
108   [_toolbarView addSubview:back];
109   [_toolbarView addSubview:forward];
110   [_toolbarView addSubview:field];
112   // Set up the network stack before creating the WebState.
113   [self setUpNetworkStack];
115   scoped_ptr<web::WebStateImpl> webState(new web::WebStateImpl(_browserState));
116   webState->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, 0);
117   web::WebViewType webViewType =
118       UseWKWebView() ? web::WK_WEB_VIEW_TYPE : web::UI_WEB_VIEW_TYPE;
119   _webController.reset(web::CreateWebController(webViewType, webState.Pass()));
120   [_webController setDelegate:self];
121   [_webController setWebUsageEnabled:YES];
123   [[_webController view] setFrame:[_containerView bounds]];
124   [_containerView addSubview:[_webController view]];
126   web::WebLoadParams params(GURL("https://dev.chromium.org/"));
127   params.transition_type = ui::PAGE_TRANSITION_TYPED;
128   [_webController loadWithParams:params];
131 - (void)setUpNetworkStack {
132   // Disable the default cache.
133   [NSURLCache setSharedURLCache:nil];
135   _httpProtocolDelegate.reset(new web::WebHTTPProtocolHandlerDelegate(
136       _browserState->GetRequestContext()));
137   net::HTTPProtocolHandlerDelegate::SetInstance(_httpProtocolDelegate.get());
138   BOOL success = [NSURLProtocol registerClass:[CRNHTTPProtocolHandler class]];
139   DCHECK(success);
140   // The CRWURLVerifyingProtocolHandler is used to verify URL in the
141   // CRWWebController. It must be registered after the HttpProtocolHandler
142   // because handlers are called in the reverse order of declaration.
143   success =
144       [NSURLProtocol registerClass:[CRWURLVerifyingProtocolHandler class]];
145   DCHECK(success);
146   _requestTrackerFactory.reset(
147       new web::RequestTrackerFactoryImpl(std::string()));
148   net::RequestTracker::SetRequestTrackerFactory(_requestTrackerFactory.get());
149   net::CookieStoreIOS::SetCookiePolicy(net::CookieStoreIOS::ALLOW);
152 - (void)didReceiveMemoryWarning {
153   [super didReceiveMemoryWarning];
156 - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
157   if (bar == _toolbarView) {
158     return UIBarPositionTopAttached;
159   }
160   return UIBarPositionAny;
163 - (void)back {
164   if ([_webController canGoBack]) {
165     [_webController goBack];
166   }
169 - (void)forward {
170   if ([_webController canGoForward]) {
171     [_webController goForward];
172   }
175 - (BOOL)textFieldShouldReturn:(UITextField*)field {
176   GURL url = GURL(base::SysNSStringToUTF8([field text]));
178   // Do not try to load invalid URLs.
179   if (url.is_valid()) {
180     web::WebLoadParams params(url);
181     params.transition_type = ui::PAGE_TRANSITION_TYPED;
182     [_webController loadWithParams:params];
183   }
185   [field resignFirstResponder];
186   [self updateToolbar];
187   return YES;
190 - (void)updateToolbar {
191   // Do not update the URL if the text field is currently being edited.
192   if ([_field isFirstResponder]) {
193     return;
194   }
196   const GURL& url = [_webController webStateImpl]->GetVisibleURL();
197   [_field setText:base::SysUTF8ToNSString(url.spec())];
200 // -----------------------------------------------------------------------
201 #pragma mark Bikeshedding Implementation
203 // Overridden to allow this view controller to receive motion events by being
204 // first responder when no other views are.
205 - (BOOL)canBecomeFirstResponder {
206   return YES;
209 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event {
210   if (event.subtype == UIEventSubtypeMotionShake) {
211     [self updateToolbarColor];
212   }
215 - (void)updateToolbarColor {
216   // Cycle through the following set of colors:
217   NSArray* colors = @[
218     // Vanilla Blue.
219     [UIColor colorWithRed:0.337 green:0.467 blue:0.988 alpha:1.0],
220     // Vanilla Red.
221     [UIColor colorWithRed:0.898 green:0.110 blue:0.137 alpha:1.0],
222     // Blue Grey.
223     [UIColor colorWithRed:0.376 green:0.490 blue:0.545 alpha:1.0],
224     // Brown.
225     [UIColor colorWithRed:0.475 green:0.333 blue:0.282 alpha:1.0],
226     // Purple.
227     [UIColor colorWithRed:0.612 green:0.153 blue:0.690 alpha:1.0],
228     // Teal.
229     [UIColor colorWithRed:0.000 green:0.737 blue:0.831 alpha:1.0],
230     // Deep Orange.
231     [UIColor colorWithRed:1.000 green:0.341 blue:0.133 alpha:1.0],
232     // Indigo.
233     [UIColor colorWithRed:0.247 green:0.318 blue:0.710 alpha:1.0],
234     // Vanilla Green.
235     [UIColor colorWithRed:0.145 green:0.608 blue:0.141 alpha:1.0],
236     // Pinkerton.
237     [UIColor colorWithRed:0.914 green:0.118 blue:0.388 alpha:1.0],
238   ];
240   NSUInteger currentIndex = [colors indexOfObject:_toolbarView.barTintColor];
241   if (currentIndex == NSNotFound) {
242     currentIndex = 0;
243   }
244   NSUInteger newIndex = currentIndex + 1;
245   if (newIndex >= [colors count]) {
246     // TODO(rohitrao): Out of colors!  Consider prompting the user to pick their
247     // own color here.  Also consider allowing the user to choose the entire set
248     // of colors or allowing the user to choose color randomization.
249     newIndex = 0;
250   }
251   _toolbarView.barTintColor = [colors objectAtIndex:newIndex];
254 // -----------------------------------------------------------------------
255 // WebDelegate implementation.
257 - (void)webWillAddPendingURL:(const GURL&)url
258                   transition:(ui::PageTransition)transition {
260 - (void)webDidAddPendingURL {
261   [self updateToolbar];
263 - (void)webCancelStartLoadingRequest {
265 - (void)webDidStartLoadingURL:(const GURL&)currentUrl
266           shouldUpdateHistory:(BOOL)updateHistory {
267   [self updateToolbar];
269 - (void)webDidFinishWithURL:(const GURL&)url loadSuccess:(BOOL)loadSuccess {
270   [self updateToolbar];
273 - (CRWWebController*)webPageOrderedOpen:(const GURL&)url
274                                referrer:(const web::Referrer&)referrer
275                              windowName:(NSString*)windowName
276                            inBackground:(BOOL)inBackground {
277   return nil;
280 - (CRWWebController*)webPageOrderedOpenBlankWithReferrer:
281                          (const web::Referrer&)referrer
282                                             inBackground:(BOOL)inBackground {
283   return nil;
286 - (void)webPageOrderedClose {
288 - (void)goDelta:(int)delta {
290 - (void)openURLWithParams:(const web::WebState::OpenURLParams&)params {
292 - (BOOL)openExternalURL:(const GURL&)url {
293   return NO;
295 - (void)presentSSLError:(const net::SSLInfo&)info
296            forSSLStatus:(const web::SSLStatus&)status
297             recoverable:(BOOL)recoverable
298                callback:(SSLErrorCallback)shouldContinue {
300 - (void)presentSpoofingError {
302 - (void)webLoadCancelled:(const GURL&)url {
304 - (void)webDidUpdateHistoryStateWithPageURL:(const GURL&)pageUrl {
306 - (void)webController:(CRWWebController*)webController
307     retrievePlaceholderOverlayImage:(void (^)(UIImage*))block {
309 - (void)webController:(CRWWebController*)webController
310     onFormResubmissionForRequest:(NSURLRequest*)request
311                    continueBlock:(ProceduralBlock)continueBlock
312                      cancelBlock:(ProceduralBlock)cancelBlock {
314 - (void)webWillReload {
316 - (void)webWillInitiateLoadWithParams:(web::WebLoadParams&)params {
318 - (void)webDidUpdateSessionForLoadWithParams:(const web::WebLoadParams&)params
319                         wasInitialNavigation:(BOOL)initialNavigation {
321 - (void)webWillFinishHistoryNavigationFromEntry:(CRWSessionEntry*)fromEntry {
323 - (void)webWillGoDelta:(int)delta {
325 - (void)webDidPrepareForGoBack {
327 - (int)downloadImageAtUrl:(const GURL&)url
328             maxBitmapSize:(uint32_t)maxBitmapSize
329                  callback:
330                      (const web::WebState::ImageDownloadCallback&)callback {
331   return -1;
334 @end