Port Android relocation packer to chromium build
[chromium-blink-merge.git] / ios / web / web_state / js / page_script_util.mm
blobeaf2cc90a810b05985a0c2df80852762beda6f94
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
3 // found in the LICENSE file.
5 #import "ios/web/web_state/js/page_script_util.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/mac/bundle_locations.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "ios/web/public/web_client.h"
13 namespace web {
15 namespace {
16 // Returns an autoreleased string containing the JavaScript to be injected into
17 // the web view as early as possible. Does not include embedder's script.
18 NSString* GetWebEarlyPageScript(WebViewType web_view_type) {
19   switch (web_view_type) {
20     case UI_WEB_VIEW_TYPE:
21       return GetPageScript(@"web_bundle_ui");
22     case WK_WEB_VIEW_TYPE:
23       return GetPageScript(@"web_bundle_wk");
24   }
25   NOTREACHED();
26   return nil;
28 }  // namespace
30 NSString* GetPageScript(NSString* script_file_name) {
31   DCHECK(script_file_name);
32   NSString* path =
33       [base::mac::FrameworkBundle() pathForResource:script_file_name
34                                              ofType:@"js"];
35   DCHECK(path) << "Script file not found: "
36                << base::SysNSStringToUTF8(script_file_name) << ".js";
37   NSError* error = nil;
38   NSString* content = [NSString stringWithContentsOfFile:path
39                                                 encoding:NSUTF8StringEncoding
40                                                    error:&error];
41   DCHECK(!error) << "Error fetching script: " << [error.description UTF8String];
42   DCHECK(content);
43   return content;
46 NSString* GetEarlyPageScript(WebViewType web_view_type) {
47   DCHECK(GetWebClient());
48   NSString* embedder_page_script =
49       GetWebClient()->GetEarlyPageScript(web_view_type);
50   DCHECK(embedder_page_script);
51   return [GetWebEarlyPageScript(web_view_type)
52       stringByAppendingString:embedder_page_script];
55 }  // namespace web