OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/translate/ios/browser/js_translate_manager.h" | 5 #import "components/translate/ios/browser/js_translate_manager.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/mac/bundle_locations.h" | 12 #include "base/mac/bundle_locations.h" |
13 #import "base/mac/scoped_nsobject.h" | 13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
14 | 18 |
15 @implementation JsTranslateManager { | 19 @implementation JsTranslateManager { |
16 base::scoped_nsobject<NSString> _translationScript; | 20 NSString* _translationScript; |
17 } | 21 } |
18 | 22 |
19 - (NSString*)script { | 23 - (NSString*)script { |
20 return _translationScript.get(); | 24 return _translationScript; |
21 } | 25 } |
22 | 26 |
23 - (void)setScript:(NSString*)script { | 27 - (void)setScript:(NSString*)script { |
24 // The translation script uses performance.now() for metrics, which is not | 28 // The translation script uses performance.now() for metrics, which is not |
25 // supported except on iOS 8.0. To make the translation script work on these | 29 // supported except on iOS 8.0. To make the translation script work on these |
26 // iOS versions, add some JavaScript to |script| that defines an | 30 // iOS versions, add some JavaScript to |script| that defines an |
27 // implementation of performance.now(). | 31 // implementation of performance.now(). |
28 NSString* const kPerformancePlaceholder = | 32 NSString* const kPerformancePlaceholder = |
29 @"var performance = window['performance'] || {};" | 33 @"var performance = window['performance'] || {};" |
30 @"performance.now = performance['now'] ||" | 34 @"performance.now = performance['now'] ||" |
31 @"(function () { return Date.now(); });\n"; | 35 @"(function () { return Date.now(); });\n"; |
32 script = [kPerformancePlaceholder stringByAppendingString:script]; | 36 script = [kPerformancePlaceholder stringByAppendingString:script]; |
33 // TODO(shreyasv): This leads to some duplicate code from | 37 // TODO(shreyasv): This leads to some duplicate code from |
34 // CRWJSInjectionManager. Consider refactoring this to its own js injection | 38 // CRWJSInjectionManager. Consider refactoring this to its own js injection |
35 // manager. | 39 // manager. |
36 NSString* path = | 40 NSString* path = |
37 [base::mac::FrameworkBundle() pathForResource:@"translate_ios" | 41 [base::mac::FrameworkBundle() pathForResource:@"translate_ios" |
38 ofType:@"js"]; | 42 ofType:@"js"]; |
39 DCHECK(path); | 43 DCHECK(path); |
40 NSError* error = nil; | 44 NSError* error = nil; |
41 NSString* content = [NSString stringWithContentsOfFile:path | 45 NSString* content = [NSString stringWithContentsOfFile:path |
42 encoding:NSUTF8StringEncoding | 46 encoding:NSUTF8StringEncoding |
43 error:&error]; | 47 error:&error]; |
44 DCHECK(!error && [content length]); | 48 DCHECK(!error && [content length]); |
45 script = [script stringByAppendingString:content]; | 49 script = [script stringByAppendingString:content]; |
46 _translationScript.reset([script copy]); | 50 _translationScript = [script copy]; |
47 } | 51 } |
48 | 52 |
49 - (void)injectWaitUntilTranslateReadyScript { | 53 - (void)injectWaitUntilTranslateReadyScript { |
50 [self.receiver executeJavaScript:@"__gCrWeb.translate.checkTranslateReady()" | 54 [self.receiver executeJavaScript:@"__gCrWeb.translate.checkTranslateReady()" |
51 completionHandler:nil]; | 55 completionHandler:nil]; |
52 } | 56 } |
53 | 57 |
54 - (void)injectTranslateStatusScript { | 58 - (void)injectTranslateStatusScript { |
55 [self.receiver executeJavaScript:@"__gCrWeb.translate.checkTranslateStatus()" | 59 [self.receiver executeJavaScript:@"__gCrWeb.translate.checkTranslateStatus()" |
56 completionHandler:nil]; | 60 completionHandler:nil]; |
(...skipping 11 matching lines...) Expand all Loading... |
68 DCHECK([self hasBeenInjected]); | 72 DCHECK([self hasBeenInjected]); |
69 [self.receiver executeJavaScript:@"cr.googleTranslate.revert()" | 73 [self.receiver executeJavaScript:@"cr.googleTranslate.revert()" |
70 completionHandler:nil]; | 74 completionHandler:nil]; |
71 } | 75 } |
72 | 76 |
73 #pragma mark - | 77 #pragma mark - |
74 #pragma mark CRWJSInjectionManager methods | 78 #pragma mark CRWJSInjectionManager methods |
75 | 79 |
76 - (NSString*)injectionContent { | 80 - (NSString*)injectionContent { |
77 DCHECK(_translationScript); | 81 DCHECK(_translationScript); |
78 return _translationScript.autorelease(); | 82 NSString* translationScript = _translationScript; |
| 83 _translationScript = nil; |
| 84 return translationScript; |
79 } | 85 } |
80 | 86 |
81 @end | 87 @end |
OLD | NEW |