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
3 // found in the LICENSE file.
5 #import "ios/chrome/browser/updatable_config/updatable_array.h"
7 #import "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
10 #import "ios/public/provider/chrome/browser/updatable_resource_provider.h"
12 @interface UpdatableArrayDelegate ()
13 // Private helper function to check that |path| is of plist type
14 // and read the content of |path| into |resourceArray_|.
15 - (void)setResourceArrayWithContentsOfFile:(NSString*)path;
18 @implementation UpdatableArrayDelegate {
19 base::scoped_nsobject<NSArray> _resourceArray;
22 - (void)loadDefaults:(id<UpdatableResourceBridge>)resource {
23 NSString* path = resource.descriptor.bundleResourcePath;
24 [self setResourceArrayWithContentsOfFile:path];
27 - (void)mergeUpdate:(id<UpdatableResourceBridge>)resource {
28 NSString* path = resource.descriptor.resourcePath;
29 if ([path isEqualToString:resource.descriptor.bundleResourcePath]) {
30 // There's no need to merge, because the only resource present is the one
31 // bundled with the app, and that was loaded by loadDefaults.
34 [self setResourceArrayWithContentsOfFile:path];
37 - (NSDictionary*)parseFileAt:(NSString*)path {
38 // Overrides this method with NOTREACHED() because default implementation in
39 // UpdatableResourceBridge is for NSDictionary only and results in opaque
40 // errors when the data file is of <array> type.
45 - (NSArray*)resourceArray {
46 return _resourceArray.get();
49 - (void)setResourceArrayWithContentsOfFile:(NSString*)path {
50 NSString* extension = [[path pathExtension] lowercaseString];
51 // Only plist file type is supported.
52 DCHECK([extension isEqualToString:@"plist"]);
53 _resourceArray.reset([[NSArray arrayWithContentsOfFile:path] retain]);
58 @implementation UpdatableArray
60 - (id<UpdatableResourceBridge>)newResource:(NSString*)resourceName {
61 base::scoped_nsobject<UpdatableArrayDelegate> delegate(
62 [[UpdatableArrayDelegate alloc] init]);
64 return ios::GetChromeBrowserProvider()
65 ->GetUpdatableResourceProvider()
66 ->CreateUpdatableResource(resourceName, delegate);
69 - (NSArray*)arrayFromConfig {
70 id delegate = [[self updatableResource] delegate];
72 DCHECK([delegate respondsToSelector:@selector(resourceArray)]);
73 id configData = [[[delegate resourceArray] retain] autorelease];
74 DCHECK([configData isKindOfClass:[NSArray class]]);