OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/web_state/js/page_script_util.h" | 5 #import "ios/web/web_state/js/page_script_util.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/mac/bundle_locations.h" | 9 #include "base/mac/bundle_locations.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 << base::SysNSStringToUTF8(script_file_name) << ".js"; | 25 << base::SysNSStringToUTF8(script_file_name) << ".js"; |
26 NSError* error = nil; | 26 NSError* error = nil; |
27 NSString* content = [NSString stringWithContentsOfFile:path | 27 NSString* content = [NSString stringWithContentsOfFile:path |
28 encoding:NSUTF8StringEncoding | 28 encoding:NSUTF8StringEncoding |
29 error:&error]; | 29 error:&error]; |
30 DCHECK(!error) << "Error fetching script: " << [error.description UTF8String]; | 30 DCHECK(!error) << "Error fetching script: " << [error.description UTF8String]; |
31 DCHECK(content); | 31 DCHECK(content); |
32 return content; | 32 return content; |
33 } | 33 } |
34 | 34 |
35 NSString* GetEarlyPageScript() { | 35 NSString* GetEarlyPageScript(BrowserState* browser_state) { |
36 DCHECK(GetWebClient()); | 36 DCHECK(GetWebClient()); |
37 NSString* embedder_page_script = GetWebClient()->GetEarlyPageScript(); | 37 NSString* embedder_page_script = |
| 38 GetWebClient()->GetEarlyPageScript(browser_state); |
38 DCHECK(embedder_page_script); | 39 DCHECK(embedder_page_script); |
39 | 40 |
40 // Make sure that script is injected only once. For example, content of | 41 // Make sure that script is injected only once. For example, content of |
41 // WKUserScript can be injected into the same page multiple times | 42 // WKUserScript can be injected into the same page multiple times |
42 // without notifying WKNavigationDelegate (e.g. after window.document.write | 43 // without notifying WKNavigationDelegate (e.g. after window.document.write |
43 // JavaScript call). Injecting the script multiple times invalidates the | 44 // JavaScript call). Injecting the script multiple times invalidates the |
44 // __gCrWeb.windowId variable and will break the ability to send messages from | 45 // __gCrWeb.windowId variable and will break the ability to send messages from |
45 // JS to the native code. Wrapping injected script into "if (!injected)" check | 46 // JS to the native code. Wrapping injected script into "if (!injected)" check |
46 // prevents multiple injections into the same page. | 47 // prevents multiple injections into the same page. |
47 NSString* kScriptTemplate = @"if (typeof __gCrWeb !== 'object') { %@; %@ }"; | 48 NSString* kScriptTemplate = @"if (typeof __gCrWeb !== 'object') { %@; %@ }"; |
48 return [NSString stringWithFormat:kScriptTemplate, | 49 return [NSString stringWithFormat:kScriptTemplate, |
49 GetPageScript(@"web_bundle"), | 50 GetPageScript(@"web_bundle"), |
50 embedder_page_script]; | 51 embedder_page_script]; |
51 } | 52 } |
52 | 53 |
53 } // namespace web | 54 } // namespace web |
OLD | NEW |