OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/web_view/public/cwv_user_content_controller.h" |
| 6 #import "ios/web_view/internal/cwv_user_content_controller_internal.h" |
| 7 |
| 8 #import "ios/web_view/internal/cwv_web_view_configuration_internal.h" |
| 9 #include "ios/web_view/internal/web_view_browser_state.h" |
| 10 #import "ios/web_view/internal/web_view_early_page_script_provider.h" |
| 11 #import "ios/web_view/public/cwv_user_script.h" |
| 12 |
| 13 @interface CWVUserContentController () |
| 14 @property(weak, nonatomic) CWVWebViewConfiguration* configuration; |
| 15 @end |
| 16 |
| 17 @implementation CWVUserContentController { |
| 18 NSMutableArray<CWVUserScript*>* _userScripts; |
| 19 } |
| 20 |
| 21 @synthesize configuration = _configuration; |
| 22 |
| 23 - (nonnull instancetype)initWithConfiguration: |
| 24 (nonnull __weak CWVWebViewConfiguration*)configuration { |
| 25 self = [super init]; |
| 26 if (self) { |
| 27 _configuration = configuration; |
| 28 _userScripts = [[NSMutableArray alloc] init]; |
| 29 } |
| 30 return self; |
| 31 } |
| 32 |
| 33 - (void)addUserScript:(nonnull CWVUserScript*)userScript { |
| 34 [_userScripts addObject:userScript]; |
| 35 [self updateEarlyPageScript]; |
| 36 } |
| 37 |
| 38 - (void)removeAllUserScripts { |
| 39 [_userScripts removeAllObjects]; |
| 40 [self updateEarlyPageScript]; |
| 41 } |
| 42 |
| 43 - (nonnull NSArray<CWVUserScript*>*)userScripts { |
| 44 return _userScripts; |
| 45 } |
| 46 |
| 47 // Updates the early page script associated with the BrowserState with the |
| 48 // content of _userScripts. |
| 49 - (void)updateEarlyPageScript { |
| 50 NSMutableString* joinedScript = [[NSMutableString alloc] init]; |
| 51 for (CWVUserScript* script in _userScripts) { |
| 52 [joinedScript appendString:script.source]; |
| 53 // Inserts "\n" between scripts to make it safer to join multiple scripts, |
| 54 // in case the first script doesn't end with ";" or "\n". |
| 55 [joinedScript appendString:@"\n"]; |
| 56 } |
| 57 ios_web_view::WebViewEarlyPageScriptProvider::FromBrowserState( |
| 58 _configuration.browserState) |
| 59 .SetScript(joinedScript); |
| 60 } |
| 61 |
| 62 @end |
OLD | NEW |