Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_view/public/cwv_web_view_configuration.h" | 5 #import "ios/web_view/public/cwv_web_view_configuration.h" |
| 6 #import "ios/web_view/internal/cwv_web_view_configuration_internal.h" | 6 #import "ios/web_view/internal/cwv_web_view_configuration_internal.h" |
| 7 | 7 |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/threading/thread_restrictions.h" | |
| 10 #include "components/translate/core/browser/translate_download_manager.h" | |
| 11 #include "ios/web/public/app/web_main.h" | |
| 8 #import "ios/web_view/internal/cwv_user_content_controller_internal.h" | 12 #import "ios/web_view/internal/cwv_user_content_controller_internal.h" |
| 9 #import "ios/web_view/internal/web_view_browser_state.h" | 13 #import "ios/web_view/internal/web_view_browser_state.h" |
| 10 #import "ios/web_view/internal/web_view_web_client.h" | 14 #import "ios/web_view/internal/web_view_web_client.h" |
| 15 #import "ios/web_view/internal/web_view_web_main_delegate.h" | |
| 16 #include "ui/base/l10n/l10n_util_mac.h" | |
| 11 | 17 |
| 12 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 13 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
| 14 #endif | 20 #endif |
| 15 | 21 |
| 16 @interface CWVWebViewConfiguration () | 22 @interface CWVWebViewConfiguration () { |
| 23 // The BrowserState for this configuration. | |
| 24 std::unique_ptr<ios_web_view::WebViewBrowserState> _browserState; | |
| 25 } | |
| 26 | |
| 17 // Initialize configuration with the specified browser state. | 27 // Initialize configuration with the specified browser state. |
|
Eugene But (OOO till 7-30)
2017/04/11 17:52:42
Would you mind fixing this comment s/Initialize/In
michaeldo
2017/04/11 21:57:03
Done.
| |
| 18 - (instancetype)initWithBrowserState: | 28 - (instancetype)initOffTheRecord:(BOOL)offTheRecord; |
|
Eugene But (OOO till 7-30)
2017/04/11 17:52:42
I think that initializer should always start with
michaeldo
2017/04/11 21:57:03
I agree. Done.
| |
| 19 (ios_web_view::WebViewBrowserState*)browserState; | |
| 20 @end | 29 @end |
| 21 | 30 |
| 22 @implementation CWVWebViewConfiguration { | 31 // The web client associated with this configuration. |
| 23 // TODO(crbug.com/690182): CWVWebViewConfiguration should own _browserState. | 32 static std::unique_ptr<ios_web_view::WebViewWebClient> webClient; |
|
Eugene But (OOO till 7-30)
2017/04/11 17:52:41
Is there a reason for making this global? It is us
michaeldo
2017/04/11 21:57:02
WebClient is assumed to be static in web::WebClien
Eugene But (OOO till 7-30)
2017/04/12 00:46:17
My proposal was this:
+ (void)initialize {
stati
Eugene But (OOO till 7-30)
2017/04/12 17:48:27
Please take a look at this comment
michaeldo
2017/04/12 18:22:55
Sorry, yes you're correct. I forgot to move this a
| |
| 24 ios_web_view::WebViewBrowserState* _browserState; | 33 |
| 25 } | 34 @implementation CWVWebViewConfiguration |
| 26 | 35 |
| 27 @synthesize userContentController = _userContentController; | 36 @synthesize userContentController = _userContentController; |
| 28 | 37 |
| 29 + (instancetype)defaultConfiguration { | 38 + (instancetype)defaultConfiguration { |
| 30 static dispatch_once_t once; | 39 return [[CWVWebViewConfiguration alloc] initOffTheRecord:false]; |
|
Eugene But (OOO till 7-30)
2017/04/11 17:52:41
Do you want to keep old code ([self class]) to all
michaeldo
2017/04/11 21:57:03
Done.
| |
| 31 static CWVWebViewConfiguration* configuration; | |
| 32 dispatch_once(&once, ^{ | |
| 33 ios_web_view::WebViewWebClient* client = | |
| 34 static_cast<ios_web_view::WebViewWebClient*>(web::GetWebClient()); | |
| 35 configuration = [[self alloc] initWithBrowserState:client->browser_state()]; | |
| 36 }); | |
| 37 return configuration; | |
| 38 } | 40 } |
| 39 | 41 |
| 40 + (instancetype)incognitoConfiguration { | 42 + (instancetype)incognitoConfiguration { |
| 41 static dispatch_once_t once; | 43 return [[CWVWebViewConfiguration alloc] initOffTheRecord:true]; |
| 42 static CWVWebViewConfiguration* configuration; | |
| 43 dispatch_once(&once, ^{ | |
| 44 ios_web_view::WebViewWebClient* client = | |
| 45 static_cast<ios_web_view::WebViewWebClient*>(web::GetWebClient()); | |
| 46 configuration = [[self alloc] | |
| 47 initWithBrowserState:client->off_the_record_browser_state()]; | |
| 48 }); | |
| 49 return configuration; | |
| 50 } | 44 } |
| 51 | 45 |
| 52 - (instancetype)initWithBrowserState: | 46 - (instancetype)initOffTheRecord:(BOOL)offTheRecord { |
| 53 (ios_web_view::WebViewBrowserState*)browserState { | |
| 54 self = [super init]; | 47 self = [super init]; |
| 55 if (self) { | 48 if (self) { |
| 56 _browserState = browserState; | 49 static std::unique_ptr<ios_web_view::WebViewWebMainDelegate> |
|
Eugene But (OOO till 7-30)
2017/04/11 17:52:42
Do you want to move globals initialization to +[CW
michaeldo
2017/04/11 21:57:02
Done.
| |
| 50 webMainDelegate; | |
| 51 static std::unique_ptr<web::WebMain> webMain; | |
| 52 static dispatch_once_t onceToken; | |
| 53 dispatch_once(&onceToken, ^{ | |
| 54 web::SetWebClient([[self class] webClient]); | |
| 55 | |
| 56 webMainDelegate = | |
| 57 base::MakeUnique<ios_web_view::WebViewWebMainDelegate>(); | |
| 58 web::WebMainParams params(webMainDelegate.get()); | |
| 59 webMain = base::MakeUnique<web::WebMain>(params); | |
| 60 }); | |
| 61 | |
| 62 // IO access is required to setup the browser state. In Chrome, this is | |
| 63 // already allowed during thread startup. However, startup time of | |
| 64 // ChromeWebView is not predetermined, so IO access is temporarily allowed. | |
| 65 bool wasIOAllowed = base::ThreadRestrictions::SetIOAllowed(true); | |
| 66 | |
| 67 _browserState = | |
| 68 base::MakeUnique<ios_web_view::WebViewBrowserState>(offTheRecord); | |
| 69 | |
| 70 // Initialize translate. | |
| 71 translate::TranslateDownloadManager* downloadManager = | |
| 72 translate::TranslateDownloadManager::GetInstance(); | |
| 73 downloadManager->set_request_context(_browserState->GetRequestContext()); | |
| 74 // TODO(crbug.com/679895): Bring up application locale correctly. | |
| 75 downloadManager->set_application_locale(l10n_util::GetLocaleOverride()); | |
| 76 downloadManager->language_list()->SetResourceRequestsAllowed(true); | |
| 77 | |
| 78 base::ThreadRestrictions::SetIOAllowed(wasIOAllowed); | |
| 79 | |
| 57 _userContentController = | 80 _userContentController = |
| 58 [[CWVUserContentController alloc] initWithConfiguration:self]; | 81 [[CWVUserContentController alloc] initWithConfiguration:self]; |
| 59 } | 82 } |
| 60 return self; | 83 return self; |
| 61 } | 84 } |
| 62 | 85 |
| 63 - (BOOL)isPersistent { | 86 - (BOOL)isPersistent { |
| 64 return !_browserState->IsOffTheRecord(); | 87 return !_browserState->IsOffTheRecord(); |
| 65 } | 88 } |
| 66 | 89 |
| 67 - (ios_web_view::WebViewBrowserState*)browserState { | 90 - (ios_web_view::WebViewBrowserState*)browserState { |
| 68 return _browserState; | 91 return _browserState.get(); |
| 92 } | |
| 93 | |
| 94 + (ios_web_view::WebViewWebClient*)webClient { | |
|
Eugene But (OOO till 7-30)
2017/04/11 17:52:41
I don't feel that WebClient conceptually belongs t
michaeldo
2017/04/11 21:57:03
I agree it's odd here. I can actually delete this
| |
| 95 webClient = base::MakeUnique<ios_web_view::WebViewWebClient>(); | |
|
Eugene But (OOO till 7-30)
2017/04/11 17:52:42
This will recreate |webClient| every time when thi
michaeldo
2017/04/11 21:57:03
This was my mistake. Moved to static initialize me
| |
| 96 return webClient.get(); | |
| 69 } | 97 } |
| 70 | 98 |
| 71 // NSCopying | 99 // NSCopying |
| 72 | 100 |
| 73 - (id)copyWithZone:(NSZone*)zone { | 101 - (id)copyWithZone:(NSZone*)zone { |
| 74 return [[[self class] allocWithZone:zone] initWithBrowserState:_browserState]; | 102 return [[[self class] allocWithZone:zone] |
| 103 initOffTheRecord:_browserState->IsOffTheRecord()]; | |
| 75 } | 104 } |
| 76 | 105 |
| 77 @end | 106 @end |
| OLD | NEW |