diff options
author | sdefresne <sdefresne@chromium.org> | 2015-04-23 09:08:35 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-23 16:09:00 +0000 |
commit | b38f53c6d9742117bc607c7f58379b056fdbfbc4 (patch) | |
tree | 3b57c27d02d2f57526530b98c1e6d97c0af0c7eb | |
parent | c10c0fae1356736246c2d647f1dc56eb6092b9e5 (diff) | |
download | chromium_src-b38f53c6d9742117bc607c7f58379b056fdbfbc4.zip chromium_src-b38f53c6d9742117bc607c7f58379b056fdbfbc4.tar.gz chromium_src-b38f53c6d9742117bc607c7f58379b056fdbfbc4.tar.bz2 |
[iOS] Upsteam UpdatableConfig
UpdatableConfig are configuration dictionaries or list that can be distributed
with the application but also updated from an URL.
BUG=429756
Review URL: https://codereview.chromium.org/1100343003
Cr-Commit-Position: refs/heads/master@{#326530}
12 files changed, 631 insertions, 0 deletions
diff --git a/ios/chrome/browser/updatable_config/updatable_array.h b/ios/chrome/browser/updatable_config/updatable_array.h new file mode 100644 index 0000000..2496327 --- /dev/null +++ b/ios/chrome/browser/updatable_config/updatable_array.h @@ -0,0 +1,32 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_ARRAY_H_ +#define IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_ARRAY_H_ + +#import "ios/chrome/browser/updatable_config/updatable_config_base.h" +#import "ios/public/provider/chrome/browser/updatable_resource_provider.h" + +// UpdatableResourceBridge supports data files of json or plist format but only +// allows dictionary types in the data. UpdatableArrayDelegate overrides the +// default file loader to accept plist file type with <array> data. +// This class is publicly declared here for unit tests to mock for testing +// and is not intended to be used elsewhere. +@interface UpdatableArrayDelegate : NSObject<UpdatableResourceDelegate> + +// The array object loaded from updatable resource. +@property(nonatomic, readonly) NSArray* resourceArray; + +@end + +@interface UpdatableArray : UpdatableConfigBase + +// Returns an autoreleased copy of the configuration array read from the +// file specified in the initializer. This implementation supports <array> +// data type specified as a plist only. +- (NSArray*)arrayFromConfig; + +@end + +#endif // IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_ARRAY_H_ diff --git a/ios/chrome/browser/updatable_config/updatable_array.mm b/ios/chrome/browser/updatable_config/updatable_array.mm new file mode 100644 index 0000000..ac772c8 --- /dev/null +++ b/ios/chrome/browser/updatable_config/updatable_array.mm @@ -0,0 +1,78 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/chrome/browser/updatable_config/updatable_array.h" + +#import "base/logging.h" +#include "base/mac/scoped_nsobject.h" +#include "ios/public/provider/chrome/browser/chrome_browser_provider.h" +#import "ios/public/provider/chrome/browser/updatable_resource_provider.h" + +@interface UpdatableArrayDelegate () +// Private helper function to check that |path| is of plist type +// and read the content of |path| into |resourceArray_|. +- (void)setResourceArrayWithContentsOfFile:(NSString*)path; +@end + +@implementation UpdatableArrayDelegate { + base::scoped_nsobject<NSArray> _resourceArray; +} + +- (void)loadDefaults:(id<UpdatableResourceBridge>)resource { + NSString* path = resource.descriptor.bundleResourcePath; + [self setResourceArrayWithContentsOfFile:path]; +} + +- (void)mergeUpdate:(id<UpdatableResourceBridge>)resource { + NSString* path = resource.descriptor.resourcePath; + if ([path isEqualToString:resource.descriptor.bundleResourcePath]) { + // There's no need to merge, because the only resource present is the one + // bundled with the app, and that was loaded by loadDefaults. + return; + } + [self setResourceArrayWithContentsOfFile:path]; +} + +- (NSDictionary*)parseFileAt:(NSString*)path { + // Overrides this method with NOTREACHED() because default implementation in + // UpdatableResourceBridge is for NSDictionary only and results in opaque + // errors when the data file is of <array> type. + NOTREACHED(); + return nil; +} + +- (NSArray*)resourceArray { + return _resourceArray.get(); +} + +- (void)setResourceArrayWithContentsOfFile:(NSString*)path { + NSString* extension = [[path pathExtension] lowercaseString]; + // Only plist file type is supported. + DCHECK([extension isEqualToString:@"plist"]); + _resourceArray.reset([[NSArray arrayWithContentsOfFile:path] retain]); +} + +@end + +@implementation UpdatableArray + +- (id<UpdatableResourceBridge>)newResource:(NSString*)resourceName { + base::scoped_nsobject<UpdatableArrayDelegate> delegate( + [[UpdatableArrayDelegate alloc] init]); + + return ios::GetChromeBrowserProvider() + ->GetUpdatableResourceProvider() + ->CreateUpdatableResource(resourceName, delegate); +} + +- (NSArray*)arrayFromConfig { + id delegate = [[self updatableResource] delegate]; + DCHECK(delegate); + DCHECK([delegate respondsToSelector:@selector(resourceArray)]); + id configData = [[[delegate resourceArray] retain] autorelease]; + DCHECK([configData isKindOfClass:[NSArray class]]); + return configData; +} + +@end diff --git a/ios/chrome/browser/updatable_config/updatable_config_base.h b/ios/chrome/browser/updatable_config/updatable_config_base.h new file mode 100644 index 0000000..c889495 --- /dev/null +++ b/ios/chrome/browser/updatable_config/updatable_config_base.h @@ -0,0 +1,63 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_CONFIG_BASE_H_ +#define IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_CONFIG_BASE_H_ + +#import <Foundation/Foundation.h> + +namespace net { +class URLRequestContextGetter; +} // namespace net + +@protocol UpdatableResourceBridge; + +// Abstract base class for configurations bundled in a .plist file in +// application bundle. Configuration is periodically updated by pulling +// in a new configuration file from gstatic.com. +// DO NOT use this class directly, but instantiate either UpdatableArray +// or UpdatableDictionary class instead. +// |startUpdate:| must be called for every subclass. +@interface UpdatableConfigBase : NSObject + +// Initializes a new Updatable Config object using .plist file named |plistName| +// which is in the application bundle. |appId| and |appVersion| are used to +// derive the URL to update this plist from gstatic.com. +// If |appId| is nil, a default value based on the application id from the +// application bundle will be provided. +// If |appVersion| is nil, a default value based on the application version +// number from the application bundle will be provided. +- (instancetype)initWithAppId:(NSString*)appId + version:(NSString*)appVersion + plist:(NSString*)plistName; + +// Starts periodic checks with server for updated version of +// configuration plists +- (void)startUpdate:(net::URLRequestContextGetter*)requestContextGetter; + +// Prevents any future update checks, and releases requestContextGetter. Should +// be called if the app is going to terminate. +- (void)stopUpdateChecks; + +// Performs any post-update operations for the updated data to take effect. +- (void)resourceDidUpdate:(NSNotification*)notification; + +// Subclasses must override this method to return a UpdatableResourceBridge +// object. The returned object determines whether the resource stored in the +// file |resourceName| is of plist, json, or some other data file format. +- (id<UpdatableResourceBridge>)newResource:(NSString*)resourceName; + +// Returns the internal resources object. This accessor is intended for +// use by subclasses of this class and for unit testing only. +// Note that this is really a readonly property for subclasses, but for +// unit testing, the ability to set the resource is also needed. +@property(nonatomic, retain) id<UpdatableResourceBridge> updatableResource; + +// Consistency check defaults to NO and is enabled only by Chrome application, +// not unit or kif tests. ++ (void)enableConsistencyCheck; + +@end + +#endif // IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_CONFIG_BASE_H_ diff --git a/ios/chrome/browser/updatable_config/updatable_config_base.mm b/ios/chrome/browser/updatable_config/updatable_config_base.mm new file mode 100644 index 0000000..b28fe21 --- /dev/null +++ b/ios/chrome/browser/updatable_config/updatable_config_base.mm @@ -0,0 +1,266 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/chrome/browser/updatable_config/updatable_config_base.h" + +#include "base/logging.h" +#import "base/mac/bind_objc_block.h" +#include "base/mac/scoped_nsobject.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#import "ios/public/provider/chrome/browser/chrome_browser_provider.h" +#import "ios/public/provider/chrome/browser/updatable_resource_provider.h" +#include "ios/web/public/web_thread.h" +#import "net/base/mac/url_conversions.h" +#include "net/http/http_status_code.h" +#include "net/url_request/url_fetcher.h" +#include "net/url_request/url_fetcher_delegate.h" +#include "net/url_request/url_request_context.h" +#include "net/url_request/url_request_context_getter.h" +#include "url/gurl.h" + +@interface UpdatableConfigBase () +// Returns the application ID to use for fetching updatable configuration. ++ (NSString*)defaultAppId; +// Fetches server-side updatable configuration plist. +- (void)checkUpdate; +#if !defined(NDEBUG) +// A method that will be executed on a delay if -startUpdate: was NOT called. +- (void)startUpdateNotCalled:(id)config; +// Schedules a call to -startUpdateNotCalled: for later to make sure that +// -startUpdate: will be called. +- (void)scheduleConsistencyCheck; +// Cancels the delayed call to -startUpdateNotCalled:. +- (void)cancelConsistencyCheck; +#endif // !defined(NDEBUG) +@end + +namespace { + +#if !defined(NDEBUG) +// Global flag to enable or disable debug check that -startUpdate: +// has been called. +BOOL g_consistency_check_enabled = NO; +#endif + +// Periodically fetch configuration updates from server. +const int64_t kPeriodicCheckInNanoseconds = 60 * 60 * 24 * NSEC_PER_SEC; + +// Class to fetch config update |url| and also act as the delegate to +// handle callbacks from URLFetcher. +class ConfigFetcher : public net::URLFetcherDelegate { + public: + ConfigFetcher(UpdatableConfigBase* owner, + id<UpdatableResourceDescriptorBridge> descriptor) + : owner_(owner), descriptor_(descriptor) {} + + // Starts fetching |url| for updated configuration. + void Fetch(const GURL& url, net::URLRequestContextGetter* context) { + fetcher_.reset(net::URLFetcher::Create(url, net::URLFetcher::GET, this)); + fetcher_->SetRequestContext(context); + fetcher_->Start(); + } + + void OnURLFetchComplete(const net::URLFetcher* fetcher) override { + DCHECK_EQ(fetcher_, fetcher); + NSData* responseData = nil; + if (fetcher_->GetResponseCode() == net::HTTP_OK) { + std::string response; + fetcher_->GetResponseAsString(&response); + responseData = + [NSData dataWithBytes:response.c_str() length:response.length()]; + } + fetcher_.reset(); + // If data was fetched, write the fetched data to local store in a + // separate thread. Then update the resource descriptor that configuration + // update is completed. Finally, schedule the next update check. + web::WebThread::PostBlockingPoolTask(FROM_HERE, base::BindBlock(^{ + BOOL updateSuccess = NO; + if (responseData) { + NSString* path = [descriptor_ updateResourcePath]; + updateSuccess = [responseData writeToFile:path atomically:YES]; + } + dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^() { + [descriptor_ updateCheckDidFinishWithSuccess:updateSuccess]; + }); + dispatch_after( + dispatch_time(DISPATCH_TIME_NOW, kPeriodicCheckInNanoseconds), + dispatch_get_main_queue(), ^{ + [owner_ checkUpdate]; + }); + })); + }; + + private: + UpdatableConfigBase* owner_; + id<UpdatableResourceDescriptorBridge> descriptor_; + scoped_ptr<net::URLFetcher> fetcher_; +}; + +} // namespace + +@implementation UpdatableConfigBase { + base::scoped_nsprotocol<id<UpdatableResourceBridge>> _updatableResource; + scoped_ptr<ConfigFetcher> _configFetcher; + scoped_refptr<net::URLRequestContextGetter> _requestContextGetter; +} + ++ (void)enableConsistencyCheck { +#if !defined(NDEBUG) + g_consistency_check_enabled = YES; +#endif +} + +// Overrides default designated initializer. +- (instancetype)init { + NOTREACHED(); + return nil; +} + +- (instancetype)initWithAppId:(NSString*)appId + version:(NSString*)appVersion + plist:(NSString*)plistName { + self = [super init]; + if (self) { + _updatableResource.reset([self newResource:plistName]); + // UpdatableResourceBridge initializes the appId to what is in the + // application bundle. The following overrides that with either the |appId| + // passed in or a default based on experimental settings if |appId| is nil. + if (!appId) + appId = [UpdatableConfigBase defaultAppId]; + [[_updatableResource descriptor] setApplicationIdentifier:appId]; + // Overrides the default application version if necessary. + if (appVersion) + [[_updatableResource descriptor] setApplicationVersion:appVersion]; + // [UpdatableResourceBridge -loadDefaults] updates the resource in + // two phases and is probably not MT safe. However, + // initWithAppId:version:plist: is called from a singleton's + // initialization loop and thus will not be called more than once. + // TODO(pkl): -loadDefaults accesses the file system to load in the + // plist. This should be done via PostBlockingPoolTask. + [_updatableResource loadDefaults]; + + NSString* notificationName = ios::GetChromeBrowserProvider() + ->GetUpdatableResourceProvider() + ->GetUpdateNotificationName(); + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(resourceDidUpdate:) + name:notificationName + object:[_updatableResource descriptor]]; + +#if !defined(NDEBUG) + [self scheduleConsistencyCheck]; +#endif + } + return self; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +#if !defined(NDEBUG) + [self cancelConsistencyCheck]; +#endif + [super dealloc]; +} + +- (void)startUpdate:(net::URLRequestContextGetter*)requestContextGetter { +#if !defined(NDEBUG) + [self cancelConsistencyCheck]; +#endif + _requestContextGetter = requestContextGetter; + [self checkUpdate]; +} + +- (void)stopUpdateChecks { + _requestContextGetter = nullptr; +} + +- (void)resourceDidUpdate:(NSNotification*)notification { + id sender = [notification object]; + DCHECK([_updatableResource descriptor] == sender); +} + +#pragma mark - +#pragma mark For Subclasses + +- (id<UpdatableResourceBridge>)newResource:(NSString*)resourceName { + // Subclasses must override this factory method. + NOTREACHED(); + return nil; +} + +- (id<UpdatableResourceBridge>)updatableResource { + return _updatableResource.get(); +} + +#pragma mark - +#pragma mark For Debug Compilations + +#if !defined(NDEBUG) +- (void)scheduleConsistencyCheck { + // Sets a delayed call that will cause a DCHECK if -startUpdate: + // was not called. + [self performSelector:@selector(startUpdateNotCalled:) + withObject:self + afterDelay:60.0]; +} + +- (void)cancelConsistencyCheck { + // Cancels the delayed error check since -startUpdate: has been called. + // Added for completeness since singletons should never be deallocated. + [NSObject + cancelPreviousPerformRequestsWithTarget:self + selector:@selector(startUpdateNotCalled:) + object:self]; +} + +- (void)startUpdateNotCalled:(id)config { + DCHECK(self == config); + if (!g_consistency_check_enabled) + return; + // Make sure that |startUpdate:| was called for this config. + NOTREACHED() << "startUpdate: was not called for " + << [[self description] UTF8String]; +} +#endif // !defined(NDEBUG) + +#pragma mark - +#pragma mark For Unit Testing + +- (void)setUpdatableResource:(id<UpdatableResourceBridge>)resource { + _updatableResource.reset([resource retain]); +} + +#pragma mark - +#pragma mark Private + ++ (NSString*)defaultAppId { + // During development and dogfooding, allow a different configuration + // update file to be used for testing. + NSString* flag = [[NSUserDefaults standardUserDefaults] + stringForKey:@"UpdatableConfigLocation"]; + if ([flag length]) { + if ([flag isEqualToString:@"Stable"]) + return @"com.google.chrome.ios"; + else if ([flag isEqualToString:@"Dogfood"]) + return @"com.google.chrome.ios.beta"; + else if ([flag isEqualToString:@"None"]) + return @"this.does.not.update"; + } + return [[NSBundle mainBundle] bundleIdentifier]; +} + +- (void)checkUpdate { + if (!_requestContextGetter.get()) + return; + if (!_configFetcher) { + _configFetcher.reset( + new ConfigFetcher(self, [_updatableResource descriptor])); + } + GURL url = net::GURLWithNSURL([[_updatableResource descriptor] updateURL]); + _configFetcher->Fetch(url, _requestContextGetter.get()); +} + +@end diff --git a/ios/chrome/browser/updatable_config/updatable_dictionary.h b/ios/chrome/browser/updatable_config/updatable_dictionary.h new file mode 100644 index 0000000..5331aed --- /dev/null +++ b/ios/chrome/browser/updatable_config/updatable_dictionary.h @@ -0,0 +1,19 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_DICTIONARY_H_ +#define IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_DICTIONARY_H_ + +#import "ios/chrome/browser/updatable_config/updatable_config_base.h" + +@interface UpdatableDictionary : UpdatableConfigBase + +// Returns an autoreleased copy of the configuration dictionary read from +// the file specified in the initializer. Configuration content must be +// of dictionary type. +- (NSDictionary*)dictionaryFromConfig; + +@end + +#endif // IOS_CHROME_BROWSER_UPDATABLE_CONFIG_UPDATABLE_DICTIONARY_H_ diff --git a/ios/chrome/browser/updatable_config/updatable_dictionary.mm b/ios/chrome/browser/updatable_config/updatable_dictionary.mm new file mode 100644 index 0000000..602ab97 --- /dev/null +++ b/ios/chrome/browser/updatable_config/updatable_dictionary.mm @@ -0,0 +1,25 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/chrome/browser/updatable_config/updatable_dictionary.h" + +#import "base/logging.h" +#include "ios/public/provider/chrome/browser/chrome_browser_provider.h" +#import "ios/public/provider/chrome/browser/updatable_resource_provider.h" + +@implementation UpdatableDictionary + +- (id<UpdatableResourceBridge>)newResource:(NSString*)resourceName { + return ios::GetChromeBrowserProvider() + ->GetUpdatableResourceProvider() + ->CreateUpdatableResource(resourceName, nil); +} + +- (NSDictionary*)dictionaryFromConfig { + id configData = [[[self.updatableResource resourceData] retain] autorelease]; + DCHECK([configData isKindOfClass:[NSDictionary class]]); + return configData; +} + +@end diff --git a/ios/chrome/ios_chrome.gyp b/ios/chrome/ios_chrome.gyp index 5163b2b..dbd68e1 100644 --- a/ios/chrome/ios_chrome.gyp +++ b/ios/chrome/ios_chrome.gyp @@ -193,6 +193,12 @@ 'browser/ui/uikit_ui_util.h', 'browser/ui/uikit_ui_util.mm', 'browser/ui/url_loader.h', + 'browser/updatable_config/updatable_array.h', + 'browser/updatable_config/updatable_array.mm', + 'browser/updatable_config/updatable_config_base.h', + 'browser/updatable_config/updatable_config_base.mm', + 'browser/updatable_config/updatable_dictionary.h', + 'browser/updatable_config/updatable_dictionary.mm', 'browser/web/dom_altering_lock.h', 'browser/web/dom_altering_lock.mm', 'browser/web_resource/ios_web_resource_service.cc', diff --git a/ios/provider/ios_provider_chrome.gyp b/ios/provider/ios_provider_chrome.gyp index d6e7cea..a81f9c0 100644 --- a/ios/provider/ios_provider_chrome.gyp +++ b/ios/provider/ios_provider_chrome.gyp @@ -17,6 +17,8 @@ '../public/provider/chrome/browser/string_provider.h', '../public/provider/chrome/browser/ui/infobar_view_delegate.h', '../public/provider/chrome/browser/ui/infobar_view_protocol.h', + '../public/provider/chrome/browser/updatable_resource_provider.h', + '../public/provider/chrome/browser/updatable_resource_provider.mm', ], 'dependencies': [ '../../base/base.gyp:base', diff --git a/ios/public/provider/chrome/browser/chrome_browser_provider.cc b/ios/public/provider/chrome/browser/chrome_browser_provider.cc index 25d98fd..2663375 100644 --- a/ios/public/provider/chrome/browser/chrome_browser_provider.cc +++ b/ios/public/provider/chrome/browser/chrome_browser_provider.cc @@ -35,6 +35,11 @@ PrefService* ChromeBrowserProvider::GetLocalState() { return nullptr; } +UpdatableResourceProvider* +ChromeBrowserProvider::GetUpdatableResourceProvider() { + return nullptr; +} + InfoBarViewPlaceholder* ChromeBrowserProvider::CreateInfoBarView() { return nullptr; } diff --git a/ios/public/provider/chrome/browser/chrome_browser_provider.h b/ios/public/provider/chrome/browser/chrome_browser_provider.h index bf7a1fe..bf24367 100644 --- a/ios/public/provider/chrome/browser/chrome_browser_provider.h +++ b/ios/public/provider/chrome/browser/chrome_browser_provider.h @@ -30,6 +30,7 @@ namespace ios { class ChromeBrowserProvider; class StringProvider; +class UpdatableResourceProvider; // Setter and getter for the provider. The provider should be set early, before // any browser code is called. @@ -47,6 +48,8 @@ class ChromeBrowserProvider { virtual net::URLRequestContextGetter* GetSystemURLRequestContext(); // Gets the local state. virtual PrefService* GetLocalState(); + // Returns an UpdatableResourceProvider instance. + virtual UpdatableResourceProvider* GetUpdatableResourceProvider(); // Returns an instance of an infobar view. The caller is responsible for // initializing the returned object and releasing it when appropriate. virtual InfoBarViewPlaceholder* CreateInfoBarView(); diff --git a/ios/public/provider/chrome/browser/updatable_resource_provider.h b/ios/public/provider/chrome/browser/updatable_resource_provider.h new file mode 100644 index 0000000..64993cd --- /dev/null +++ b/ios/public/provider/chrome/browser/updatable_resource_provider.h @@ -0,0 +1,107 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_PUBLIC_PROVIDER_CHROME_BROWSER_UPDATABLE_RESOURCE_PROVIDER_H_ +#define IOS_PUBLIC_PROVIDER_CHROME_BROWSER_UPDATABLE_RESOURCE_PROVIDER_H_ + +#import <Foundation/Foundation.h> + +#include "base/macros.h" + +@protocol UpdatableResourceDelegate; +@protocol UpdatableResourceDescriptorBridge; + +#pragma mark - UpdatableResourceBridge + +// UpdatableResourceBridge represents an updatable resource. +@protocol UpdatableResourceBridge<NSObject> + +// Returns the UpdatableResourceDescriptorBridge associated to this resource. +@property(nonatomic, readonly) id<UpdatableResourceDescriptorBridge> descriptor; + +// Returns the UpdatableResourceDelegate associated to this resource. +- (id<UpdatableResourceDelegate>)delegate; + +// The data provided by this resource. +- (NSDictionary*)resourceData; + +// Initializes this updatable resource with default values. +- (void)loadDefaults; +@end + +#pragma mark - UpdatableResourceDescriptorBridge + +// This class encapsulates identification and versioning information +// for an updatable resource. +@protocol UpdatableResourceDescriptorBridge<NSObject> + +@property(nonatomic, copy) NSString* applicationIdentifier; +@property(nonatomic, copy) NSString* applicationVersion; + +// URL where an update to this resource may be downloaded. +@property(nonatomic, readonly) NSURL* updateURL; + +// Search path for the default file. +@property(nonatomic, copy) NSString* bundleResourcePath; +// Search path for the downloaded file. +@property(nonatomic, copy) NSString* updateResourcePath; + +// This method must be called after an update check has been made. Either +// successfully or not. +- (void)updateCheckDidFinishWithSuccess:(BOOL)wasSuccessful; + +// Returns the full path of the latest and greatest version of the resource +// currently residing on the device. If there is no version of the resource +// currently stored on the device, returns |nil|. +- (NSString*)resourcePath; + +@end + +#pragma mark - UpdatableResourceDelegate + +// Delegate for UpdatableResourceBridge. +@protocol UpdatableResourceDelegate<NSObject> + +// This method can be reimplemented to override the behavior of the +// UpdatableResourceBridge method. +- (void)loadDefaults:(id<UpdatableResourceBridge>)resource; + +// Merges the latest values defined in the file into the values currently stored +// by the updatable resource. +- (void)mergeUpdate:(id<UpdatableResourceBridge>)resource; + +// Parses file at |path| and returns an NSDictionary with the values stored +// therein. +- (NSDictionary*)parseFileAt:(NSString*)path; +@end + +#pragma mark - UpdatableResourceProvider + +namespace ios { + +// Provider for UpdatableResourceBridge. +class UpdatableResourceProvider { + public: + UpdatableResourceProvider(); + virtual ~UpdatableResourceProvider(); + + // Returns the name of the notification that is sent through the notification + // center when a resource is updated. + virtual NSString* GetUpdateNotificationName(); + + // Creates a new UpdatableResourceBrige. + // The user is responsible for releasing it. + // The UpdatableResourceBridge takes ownership of the delegate. + // |delegate| may be nil. + virtual id<UpdatableResourceBridge> CreateUpdatableResource( + NSString* resource_identifier, + id<UpdatableResourceDelegate> delegate); + + private: + DISALLOW_COPY_AND_ASSIGN(UpdatableResourceProvider); +}; + +} // namespace ios + +#endif // IOS_PUBLIC_PROVIDER_CHROME_BROWSER_UPDATABLE_RESOURCE_PROVIDER_H_ diff --git a/ios/public/provider/chrome/browser/updatable_resource_provider.mm b/ios/public/provider/chrome/browser/updatable_resource_provider.mm new file mode 100644 index 0000000..7172695 --- /dev/null +++ b/ios/public/provider/chrome/browser/updatable_resource_provider.mm @@ -0,0 +1,25 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/public/provider/chrome/browser/updatable_resource_provider.h" + +namespace ios { + +UpdatableResourceProvider::UpdatableResourceProvider() { +} + +UpdatableResourceProvider::~UpdatableResourceProvider() { +} + +NSString* UpdatableResourceProvider::GetUpdateNotificationName() { + return nil; +} + +id<UpdatableResourceBridge> UpdatableResourceProvider::CreateUpdatableResource( + NSString* resource_identifier, + id<UpdatableResourceDelegate> delegate) { + return nil; +} + +} // namespace ios |