Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(696)

Side by Side Diff: ios/web_view/internal/cwv_user_content_controller.mm

Issue 2764773002: Add CWVUserContentController which enables injecting JavaScripts. (Closed)
Patch Set: Apply new comments. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 - (nullable instancetype)initWithConfiguration:
24 (nonnull __weak CWVWebViewConfiguration*)configuration {
25 self = [super init];
26 if (self) {
27 DCHECK(configuration);
28 _configuration = configuration;
29 _userScripts = [[NSMutableArray alloc] init];
30 }
31 return self;
32 }
33
34 - (void)addUserScript:(nonnull CWVUserScript*)userScript {
35 DCHECK(userScript);
36 [_userScripts addObject:userScript];
37 [self updateEarlyPageScript];
38 }
39
40 - (void)removeAllUserScripts {
41 [_userScripts removeAllObjects];
42 [self updateEarlyPageScript];
43 }
44
45 - (nonnull NSArray<CWVUserScript*>*)userScripts {
46 return _userScripts;
47 }
48
49 // Updates the early page script associated with the BrowserState with the
50 // content of _userScripts.
51 - (void)updateEarlyPageScript {
52 NSMutableString* joinedScript = [[NSMutableString alloc] init];
53 for (CWVUserScript* script in _userScripts) {
54 [joinedScript appendString:script.source];
55 // Inserts "\n" between scripts to make it safer to join multiple scripts,
56 // in case the first script doesn't end with ";" or "\n".
57 [joinedScript appendString:@"\n"];
58 }
59 ios_web_view::WebViewEarlyPageScriptProvider::FromBrowserState(
60 _configuration.browserState)
61 .SetScript(joinedScript);
62 }
63
64 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698