OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chrome/browser/voice/speech_input_locale_match_config.h" | 5 #import "ios/chrome/browser/voice/speech_input_locale_match_config.h" |
6 | 6 |
7 #import "base/mac/foundation_util.h" | 7 #import "base/mac/foundation_util.h" |
8 #import "base/mac/scoped_nsobject.h" | 8 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." |
| 11 #endif |
9 | 12 |
10 namespace { | 13 namespace { |
11 | 14 |
12 // Keys used in SpeechInputLocaleMatches.plist: | 15 // Keys used in SpeechInputLocaleMatches.plist: |
13 NSString* const kMatchedLocaleKey = @"Locale"; | 16 NSString* const kMatchedLocaleKey = @"Locale"; |
14 NSString* const kMatchingLocalesKey = @"MatchingLocales"; | 17 NSString* const kMatchingLocalesKey = @"MatchingLocales"; |
15 NSString* const kMatchingLanguagesKey = @"MatchingLanguages"; | 18 NSString* const kMatchingLanguagesKey = @"MatchingLanguages"; |
16 | 19 |
17 } // namespace | 20 } // namespace |
18 | 21 |
19 #pragma mark - SpeechInputLocaleMatchConfig | 22 #pragma mark - SpeechInputLocaleMatchConfig |
20 | 23 |
21 @interface SpeechInputLocaleMatchConfig () { | 24 @interface SpeechInputLocaleMatchConfig () |
22 // Backing object for the property of the same name. | |
23 base::scoped_nsobject<NSArray> _matches; | |
24 } | |
25 | 25 |
26 // Loads |_matches| from config file |plistFileName|. | 26 // Loads |_matches| from config file |plistFileName|. |
27 - (void)loadConfigFile:(NSString*)plistFileName; | 27 - (void)loadConfigFile:(NSString*)plistFileName; |
28 | 28 |
29 @end | 29 @end |
30 | 30 |
31 @implementation SpeechInputLocaleMatchConfig | 31 @implementation SpeechInputLocaleMatchConfig |
| 32 @synthesize matches = _matches; |
32 | 33 |
33 + (instancetype)sharedInstance { | 34 + (instancetype)sharedInstance { |
34 static SpeechInputLocaleMatchConfig* matchConfig; | 35 static SpeechInputLocaleMatchConfig* matchConfig; |
35 static dispatch_once_t onceToken; | 36 static dispatch_once_t onceToken; |
36 dispatch_once(&onceToken, ^{ | 37 dispatch_once(&onceToken, ^{ |
37 matchConfig = [[SpeechInputLocaleMatchConfig alloc] init]; | 38 matchConfig = [[SpeechInputLocaleMatchConfig alloc] init]; |
38 }); | 39 }); |
39 return matchConfig; | 40 return matchConfig; |
40 } | 41 } |
41 | 42 |
42 - (instancetype)init { | 43 - (instancetype)init { |
43 self = [super init]; | 44 self = [super init]; |
44 if (self) { | 45 if (self) { |
45 [self loadConfigFile:@"SpeechInputLocaleMatches"]; | 46 [self loadConfigFile:@"SpeechInputLocaleMatches"]; |
46 } | 47 } |
47 return self; | 48 return self; |
48 } | 49 } |
49 | 50 |
50 #pragma mark Accessors | |
51 | |
52 - (NSArray*)matches { | |
53 return _matches.get(); | |
54 } | |
55 | 51 |
56 #pragma mark - Private | 52 #pragma mark - Private |
57 | 53 |
58 - (void)loadConfigFile:(NSString*)plistFileName { | 54 - (void)loadConfigFile:(NSString*)plistFileName { |
59 NSString* path = [[NSBundle mainBundle] pathForResource:plistFileName | 55 NSString* path = [[NSBundle mainBundle] pathForResource:plistFileName |
60 ofType:@"plist" | 56 ofType:@"plist" |
61 inDirectory:@"gm-config/ANY"]; | 57 inDirectory:@"gm-config/ANY"]; |
62 NSArray* configData = [NSArray arrayWithContentsOfFile:path]; | 58 NSArray* configData = [NSArray arrayWithContentsOfFile:path]; |
63 NSMutableArray* matches = [NSMutableArray array]; | 59 NSMutableArray* matches = [NSMutableArray array]; |
64 for (id item in configData) { | 60 for (id item in configData) { |
65 NSDictionary* matchDict = base::mac::ObjCCastStrict<NSDictionary>(item); | 61 NSDictionary* matchDict = base::mac::ObjCCastStrict<NSDictionary>(item); |
66 base::scoped_nsobject<SpeechInputLocaleMatch> match( | 62 SpeechInputLocaleMatch* match = |
67 [[SpeechInputLocaleMatch alloc] initWithDictionary:matchDict]); | 63 [[SpeechInputLocaleMatch alloc] initWithDictionary:matchDict]; |
68 [matches addObject:match]; | 64 [matches addObject:match]; |
69 } | 65 } |
70 _matches.reset([matches copy]); | 66 _matches = [matches copy]; |
71 } | 67 } |
72 | 68 |
73 @end | 69 @end |
74 | 70 |
75 #pragma mark - SpeechInputLocaleMatch | 71 #pragma mark - SpeechInputLocaleMatch |
76 | 72 |
77 @interface SpeechInputLocaleMatch () { | |
78 // Backing objects for properties of the same name. | |
79 base::scoped_nsobject<NSString> _matchedLocaleCode; | |
80 base::scoped_nsobject<NSArray> _matchingLocaleCodes; | |
81 base::scoped_nsobject<NSArray> _matchingLanguages; | |
82 } | |
83 | |
84 @end | |
85 | |
86 @implementation SpeechInputLocaleMatch | 73 @implementation SpeechInputLocaleMatch |
| 74 @synthesize matchedLocaleCode = _matchedLocaleCode; |
| 75 @synthesize matchingLocaleCodes = _matchingLocaleCodes; |
| 76 @synthesize matchingLanguages = _matchingLanguages; |
87 | 77 |
88 - (instancetype)initWithDictionary:(NSDictionary*)matchDict { | 78 - (instancetype)initWithDictionary:(NSDictionary*)matchDict { |
89 if ((self = [super init])) { | 79 if ((self = [super init])) { |
90 _matchedLocaleCode.reset([matchDict[kMatchedLocaleKey] copy]); | 80 _matchedLocaleCode = [matchDict[kMatchedLocaleKey] copy]; |
91 _matchingLocaleCodes.reset([matchDict[kMatchingLocalesKey] copy]); | 81 _matchingLocaleCodes = [matchDict[kMatchingLocalesKey] copy]; |
92 _matchingLanguages.reset([matchDict[kMatchingLanguagesKey] copy]); | 82 _matchingLanguages = [matchDict[kMatchingLanguagesKey] copy]; |
93 } | 83 } |
94 return self; | 84 return self; |
95 } | 85 } |
96 | 86 |
97 #pragma mark Accessors | |
98 | |
99 - (NSString*)matchedLocaleCode { | |
100 return _matchedLocaleCode; | |
101 } | |
102 | |
103 - (NSArray*)matchingLocaleCodes { | |
104 return _matchingLocaleCodes; | |
105 } | |
106 | |
107 - (NSArray*)matchingLanguages { | |
108 return _matchingLanguages; | |
109 } | |
110 | |
111 @end | 87 @end |
OLD | NEW |