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

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

Issue 2764773002: Add CWVUserContentController which enables injecting JavaScripts. (Closed)
Patch Set: Add license. 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
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..479b4b6037b87cd377f909d468f5897198327d06
--- /dev/null
+++ b/ios/web_view/internal/cwv_user_content_controller.mm
@@ -0,0 +1,52 @@
+// 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.
michaeldo 2017/03/21 16:12:34 Please add new line after license.
Hiroshi Ichikawa 2017/03/22 04:52:52 Done.
+#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"
+
+@implementation CWVUserContentController {
+ __weak CWVWebViewConfiguration* _configuration;
michaeldo 2017/03/21 16:12:34 Please use properties here Ref: https://google.gi
Hiroshi Ichikawa 2017/03/22 04:52:52 Done.
+ NSMutableArray<CWVUserScript*>* _userScripts;
+}
+
+@synthesize userScripts = _userScripts;
+
+- (instancetype)initWithConfiguration:
+ (__weak CWVWebViewConfiguration*)configuration {
+ self = [super init];
+ if (self) {
+ _configuration = configuration;
Eugene But (OOO till 7-30) 2017/03/21 16:47:55 nit: Consider adding DCHECK(configuration)
Hiroshi Ichikawa 2017/03/22 04:52:52 Done. But I also added nonnull annotation for |con
michaeldo 2017/03/22 23:03:32 We commonly use DCHECKS where we expect a nonnull
Eugene But (OOO till 7-30) 2017/03/22 23:09:50 That's a good point. Maybe DCHECKs are indeed unne
Hiroshi Ichikawa 2017/03/23 02:48:05 Thanks. Deleted the DCHECKs.
+ _userScripts = [[NSMutableArray alloc] init];
+ }
+ return self;
+}
+
+- (void)addUserScript:(CWVUserScript*)userScript {
+ [_userScripts addObject:userScript];
Eugene But (OOO till 7-30) 2017/03/21 16:47:55 nit: Consider adding DCHECK(userScript)
Hiroshi Ichikawa 2017/03/22 04:52:52 Done.
+ [self updateEarlyPageScript];
+}
+
+- (void)removeAllUserScripts {
+ [_userScripts removeAllObjects];
+ [self updateEarlyPageScript];
+}
+
+- (void)updateEarlyPageScript {
Eugene But (OOO till 7-30) 2017/03/21 16:47:55 nit: Could you please add comment.
Hiroshi Ichikawa 2017/03/22 04:52:52 Done.
+ NSMutableString* joinedScript = [NSMutableString string];
michaeldo 2017/03/21 16:12:34 please use alloc/init instead of "string"
Hiroshi Ichikawa 2017/03/22 04:52:52 Done. Just curious, why? Is it just a preferred st
michaeldo 2017/03/22 23:03:32 Correct, I suggested this because our style prefer
Hiroshi Ichikawa 2017/03/23 02:48:05 Acknowledged.
+ for (CWVUserScript* script in _userScripts) {
+ [joinedScript appendString:script.source];
Eugene But (OOO till 7-30) 2017/03/21 16:47:55 This can potentially be a performance problem. Do
Hiroshi Ichikawa 2017/03/22 04:52:52 Done. I believe it would be rare that it becomes a
+ // 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

Powered by Google App Engine
This is Rietveld 408576698