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

Unified Diff: ios/web_view/internal/cwv_user_content_controller.mm

Issue 2764773002: Add CWVUserContentController which enables injecting JavaScripts. (Closed)
Patch Set: Rebase. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ios/web_view/internal/BUILD.gn ('k') | ios/web_view/internal/cwv_user_content_controller_internal.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/web_view/internal/cwv_user_content_controller.mm
diff --git a/ios/web_view/internal/cwv_user_content_controller.mm b/ios/web_view/internal/cwv_user_content_controller.mm
new file mode 100644
index 0000000000000000000000000000000000000000..edb92bf610303f25b058f2506bece6356996779f
--- /dev/null
+++ b/ios/web_view/internal/cwv_user_content_controller.mm
@@ -0,0 +1,62 @@
+// Copyright 2017 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_view/public/cwv_user_content_controller.h"
+#import "ios/web_view/internal/cwv_user_content_controller_internal.h"
+
+#import "ios/web_view/internal/cwv_web_view_configuration_internal.h"
+#include "ios/web_view/internal/web_view_browser_state.h"
+#import "ios/web_view/internal/web_view_early_page_script_provider.h"
+#import "ios/web_view/public/cwv_user_script.h"
+
+@interface CWVUserContentController ()
+@property(weak, nonatomic) CWVWebViewConfiguration* configuration;
+@end
+
+@implementation CWVUserContentController {
+ NSMutableArray<CWVUserScript*>* _userScripts;
+}
+
+@synthesize configuration = _configuration;
+
+- (nonnull instancetype)initWithConfiguration:
+ (nonnull __weak CWVWebViewConfiguration*)configuration {
+ self = [super init];
+ if (self) {
+ _configuration = configuration;
+ _userScripts = [[NSMutableArray alloc] init];
+ }
+ return self;
+}
+
+- (void)addUserScript:(nonnull CWVUserScript*)userScript {
+ [_userScripts addObject:userScript];
+ [self updateEarlyPageScript];
+}
+
+- (void)removeAllUserScripts {
+ [_userScripts removeAllObjects];
+ [self updateEarlyPageScript];
+}
+
+- (nonnull NSArray<CWVUserScript*>*)userScripts {
+ return _userScripts;
+}
+
+// Updates the early page script associated with the BrowserState with the
+// content of _userScripts.
+- (void)updateEarlyPageScript {
+ NSMutableString* joinedScript = [[NSMutableString alloc] init];
+ for (CWVUserScript* script in _userScripts) {
+ [joinedScript appendString:script.source];
+ // Inserts "\n" between scripts to make it safer to join multiple scripts,
+ // in case the first script doesn't end with ";" or "\n".
+ [joinedScript appendString:@"\n"];
+ }
+ ios_web_view::WebViewEarlyPageScriptProvider::FromBrowserState(
+ _configuration.browserState)
+ .SetScript(joinedScript);
+}
+
+@end
« no previous file with comments | « ios/web_view/internal/BUILD.gn ('k') | ios/web_view/internal/cwv_user_content_controller_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698