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