| 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 "ios/chrome/browser/installation_notifier.h" | 5 #import "ios/chrome/browser/installation_notifier.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/ios/weak_nsobject.h" | |
| 13 #include "base/logging.h" | 12 #include "base/logging.h" |
| 14 #include "base/mac/scoped_nsobject.h" | |
| 15 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 16 #include "ios/web/public/web_thread.h" | 14 #include "ios/web/public/web_thread.h" |
| 17 #include "net/base/backoff_entry.h" | 15 #include "net/base/backoff_entry.h" |
| 18 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 19 | 17 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." |
| 20 #endif |
| 21 |
| 20 namespace { | 22 namespace { |
| 21 const net::BackoffEntry::Policy kPollingBackoffPolicy = { | 23 const net::BackoffEntry::Policy kPollingBackoffPolicy = { |
| 22 0, // Number of errors to ignore. | 24 0, // Number of errors to ignore. |
| 23 1 * 1000, // Initial delay in milliseconds. | 25 1 * 1000, // Initial delay in milliseconds. |
| 24 1.5, // Multiply factor. | 26 1.5, // Multiply factor. |
| 25 0.1, // Jitter factor. | 27 0.1, // Jitter factor. |
| 26 60 * 1000, // Maximum backoff in milliseconds. | 28 60 * 1000, // Maximum backoff in milliseconds. |
| 27 -1, // Entry lifetime. | 29 -1, // Entry lifetime. |
| 28 false // Always use initial delay. | 30 false // Always use initial delay. |
| 29 }; | 31 }; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 57 @interface InstallationNotifier (Testing) | 59 @interface InstallationNotifier (Testing) |
| 58 // Sets the dispatcher. | 60 // Sets the dispatcher. |
| 59 - (void)setDispatcher:(id<DispatcherProtocol>)dispatcher; | 61 - (void)setDispatcher:(id<DispatcherProtocol>)dispatcher; |
| 60 // Sets the UIApplication used to determine if a scheme can be opened by an | 62 // Sets the UIApplication used to determine if a scheme can be opened by an |
| 61 // application. | 63 // application. |
| 62 - (void)setSharedApplication:(UIApplication*)sharedApplication; | 64 - (void)setSharedApplication:(UIApplication*)sharedApplication; |
| 63 @end | 65 @end |
| 64 | 66 |
| 65 @implementation InstallationNotifier { | 67 @implementation InstallationNotifier { |
| 66 std::unique_ptr<net::BackoffEntry> _backoffEntry; | 68 std::unique_ptr<net::BackoffEntry> _backoffEntry; |
| 67 base::scoped_nsprotocol<id<DispatcherProtocol>> _dispatcher; | 69 id<DispatcherProtocol> _dispatcher; |
| 68 // Dictionary mapping URL schemes to mutable sets of observers. | 70 // Dictionary mapping URL schemes to mutable sets of observers. |
| 69 base::scoped_nsobject<NSMutableDictionary> _installedAppObservers; | 71 NSMutableDictionary* _installedAppObservers; |
| 70 NSNotificationCenter* _notificationCenter; // Weak. | 72 __weak NSNotificationCenter* _notificationCenter; |
| 71 | 73 |
| 72 // This object can be a fake application in unittests. | 74 // This object can be a fake application in unittests. |
| 73 UIApplication* sharedApplication_; // Weak. | 75 __weak UIApplication* sharedApplication_; |
| 74 } | 76 } |
| 75 | 77 |
| 76 @synthesize lastCreatedBlockId = lastCreatedBlockId_; | 78 @synthesize lastCreatedBlockId = lastCreatedBlockId_; |
| 77 | 79 |
| 78 + (InstallationNotifier*)sharedInstance { | 80 + (InstallationNotifier*)sharedInstance { |
| 79 static InstallationNotifier* instance = [[InstallationNotifier alloc] init]; | 81 static InstallationNotifier* instance = [[InstallationNotifier alloc] init]; |
| 80 return instance; | 82 return instance; |
| 81 } | 83 } |
| 82 | 84 |
| 83 - (instancetype)init { | 85 - (instancetype)init { |
| 84 self = [super init]; | 86 self = [super init]; |
| 85 if (self) { | 87 if (self) { |
| 86 lastCreatedBlockId_ = 0; | 88 lastCreatedBlockId_ = 0; |
| 87 _dispatcher.reset([[DefaultDispatcher alloc] init]); | 89 _dispatcher = [[DefaultDispatcher alloc] init]; |
| 88 _installedAppObservers.reset([[NSMutableDictionary alloc] init]); | 90 _installedAppObservers = [[NSMutableDictionary alloc] init]; |
| 89 _notificationCenter = [NSNotificationCenter defaultCenter]; | 91 _notificationCenter = [NSNotificationCenter defaultCenter]; |
| 90 sharedApplication_ = [UIApplication sharedApplication]; | 92 sharedApplication_ = [UIApplication sharedApplication]; |
| 91 _backoffEntry.reset(new net::BackoffEntry([self backOffPolicy])); | 93 _backoffEntry.reset(new net::BackoffEntry([self backOffPolicy])); |
| 92 } | 94 } |
| 93 return self; | 95 return self; |
| 94 } | 96 } |
| 95 | 97 |
| 96 - (void)registerForInstallationNotifications:(id)observer | 98 - (void)registerForInstallationNotifications:(id)observer |
| 97 withSelector:(SEL)notificationSelector | 99 withSelector:(SEL)notificationSelector |
| 98 forScheme:(NSString*)scheme { | 100 forScheme:(NSString*)scheme { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 110 if (![scheme length]) | 112 if (![scheme length]) |
| 111 return; | 113 return; |
| 112 DCHECK([observer respondsToSelector:notificationSelector]); | 114 DCHECK([observer respondsToSelector:notificationSelector]); |
| 113 DCHECK([scheme rangeOfString:@":"].location == NSNotFound); | 115 DCHECK([scheme rangeOfString:@":"].location == NSNotFound); |
| 114 // A strong reference would prevent the observer from unregistering itself | 116 // A strong reference would prevent the observer from unregistering itself |
| 115 // from its dealloc method, because the dealloc itself would never be called. | 117 // from its dealloc method, because the dealloc itself would never be called. |
| 116 NSValue* weakReferenceToObserver = | 118 NSValue* weakReferenceToObserver = |
| 117 [NSValue valueWithNonretainedObject:observer]; | 119 [NSValue valueWithNonretainedObject:observer]; |
| 118 NSMutableSet* observers = [_installedAppObservers objectForKey:scheme]; | 120 NSMutableSet* observers = [_installedAppObservers objectForKey:scheme]; |
| 119 if (!observers) | 121 if (!observers) |
| 120 observers = [[[NSMutableSet alloc] init] autorelease]; | 122 observers = [[NSMutableSet alloc] init]; |
| 121 if ([observers containsObject:weakReferenceToObserver]) | 123 if ([observers containsObject:weakReferenceToObserver]) |
| 122 return; | 124 return; |
| 123 [observers addObject:weakReferenceToObserver]; | 125 [observers addObject:weakReferenceToObserver]; |
| 124 [_installedAppObservers setObject:observers forKey:scheme]; | 126 [_installedAppObservers setObject:observers forKey:scheme]; |
| 125 [_notificationCenter addObserver:observer | 127 [_notificationCenter addObserver:observer |
| 126 selector:notificationSelector | 128 selector:notificationSelector |
| 127 name:scheme | 129 name:scheme |
| 128 object:self]; | 130 object:self]; |
| 129 _backoffEntry->Reset(); | 131 _backoffEntry->Reset(); |
| 130 if (poll) | 132 if (poll) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 154 _backoffEntry->Reset(); | 156 _backoffEntry->Reset(); |
| 155 [self pollForTheInstallationOfApps]; | 157 [self pollForTheInstallationOfApps]; |
| 156 } | 158 } |
| 157 | 159 |
| 158 - (void)dispatchInstallationNotifierBlock { | 160 - (void)dispatchInstallationNotifierBlock { |
| 159 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 161 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 160 int blockId = ++lastCreatedBlockId_; | 162 int blockId = ++lastCreatedBlockId_; |
| 161 _backoffEntry->InformOfRequest(false); | 163 _backoffEntry->InformOfRequest(false); |
| 162 int64_t delayInNSec = | 164 int64_t delayInNSec = |
| 163 _backoffEntry->GetTimeUntilRelease().InMicroseconds() * NSEC_PER_USEC; | 165 _backoffEntry->GetTimeUntilRelease().InMicroseconds() * NSEC_PER_USEC; |
| 164 base::WeakNSObject<InstallationNotifier> weakSelf(self); | 166 __weak InstallationNotifier* weakSelf = self; |
| 165 [_dispatcher dispatchAfter:delayInNSec | 167 [_dispatcher dispatchAfter:delayInNSec |
| 166 withBlock:^{ | 168 withBlock:^{ |
| 167 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 169 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 168 base::scoped_nsobject<InstallationNotifier> strongSelf( | 170 InstallationNotifier* strongSelf = weakSelf; |
| 169 [weakSelf retain]); | |
| 170 if (blockId == [strongSelf lastCreatedBlockId]) { | 171 if (blockId == [strongSelf lastCreatedBlockId]) { |
| 171 [strongSelf pollForTheInstallationOfApps]; | 172 [strongSelf pollForTheInstallationOfApps]; |
| 172 } | 173 } |
| 173 }]; | 174 }]; |
| 174 } | 175 } |
| 175 | 176 |
| 176 - (void)pollForTheInstallationOfApps { | 177 - (void)pollForTheInstallationOfApps { |
| 177 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 178 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 178 __block BOOL keepPolling = NO; | 179 __block BOOL keepPolling = NO; |
| 179 NSMutableSet* keysToDelete = [NSMutableSet set]; | 180 NSMutableSet* keysToDelete = [NSMutableSet set]; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 205 } | 206 } |
| 206 | 207 |
| 207 - (net::BackoffEntry::Policy const*)backOffPolicy { | 208 - (net::BackoffEntry::Policy const*)backOffPolicy { |
| 208 return &kPollingBackoffPolicy; | 209 return &kPollingBackoffPolicy; |
| 209 } | 210 } |
| 210 | 211 |
| 211 #pragma mark - | 212 #pragma mark - |
| 212 #pragma mark Testing setters | 213 #pragma mark Testing setters |
| 213 | 214 |
| 214 - (void)setDispatcher:(id<DispatcherProtocol>)dispatcher { | 215 - (void)setDispatcher:(id<DispatcherProtocol>)dispatcher { |
| 215 _dispatcher.reset(dispatcher); | 216 _dispatcher = dispatcher; |
| 216 } | 217 } |
| 217 | 218 |
| 218 - (void)setSharedApplication:(id)sharedApplication { | 219 - (void)setSharedApplication:(id)sharedApplication { |
| 219 // Verify that the test application object responds to all the selectors that | 220 // Verify that the test application object responds to all the selectors that |
| 220 // will be called on it. | 221 // will be called on it. |
| 221 CHECK([sharedApplication respondsToSelector:@selector(canOpenURL:)]); | 222 CHECK([sharedApplication respondsToSelector:@selector(canOpenURL:)]); |
| 222 sharedApplication_ = (UIApplication*)sharedApplication; | 223 sharedApplication_ = (UIApplication*)sharedApplication; |
| 223 } | 224 } |
| 224 | 225 |
| 225 @end | 226 @end |
| OLD | NEW |