| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #import "ios/chrome/content_widget_extension/content_widget_view_controller.h" |
| 6 |
| 7 #import <NotificationCenter/NotificationCenter.h> |
| 8 |
| 9 #include "base/ios/ios_util.h" |
| 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "ios/chrome/common/app_group/app_group_constants.h" |
| 13 #include "ios/chrome/content_widget_extension/content_widget_view.h" |
| 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
| 19 namespace { |
| 20 // Using GURL in the extension is not wanted as it includes ICU which makes the |
| 21 // extension binary much larger; therefore, ios/chrome/common/x_callback_url.h |
| 22 // cannot be used. This class makes a very basic use of x-callback-url, so no |
| 23 // full implementation is required. |
| 24 NSString* const kXCallbackURLHost = @"x-callback-url"; |
| 25 } // namespace |
| 26 |
| 27 @interface ContentWidgetViewController () |
| 28 @property(nonatomic, weak) ContentWidgetView* widgetView; |
| 29 |
| 30 // Updates the widget with latest data. Returns whether any visual updates |
| 31 // occured. |
| 32 - (BOOL)updateWidget; |
| 33 // Opens the main application with the given |URL|. |
| 34 - (void)openAppWithURL:(NSString*)URL; |
| 35 @end |
| 36 |
| 37 @implementation ContentWidgetViewController |
| 38 |
| 39 @synthesize widgetView = _widgetView; |
| 40 |
| 41 #pragma mark - UIViewController |
| 42 |
| 43 - (void)viewDidLoad { |
| 44 [super viewDidLoad]; |
| 45 |
| 46 // A local variable is necessary here as the property is declared weak and the |
| 47 // object would be deallocated before being retained by the addSubview call. |
| 48 ContentWidgetView* widgetView = [[ContentWidgetView alloc] init]; |
| 49 self.widgetView = widgetView; |
| 50 [self.view addSubview:self.widgetView]; |
| 51 |
| 52 if (base::ios::IsRunningOnIOS10OrLater()) { |
| 53 self.extensionContext.widgetLargestAvailableDisplayMode = |
| 54 NCWidgetDisplayModeExpanded; |
| 55 } |
| 56 |
| 57 self.widgetView.translatesAutoresizingMaskIntoConstraints = NO; |
| 58 [NSLayoutConstraint activateConstraints:@[ |
| 59 [self.view.leadingAnchor |
| 60 constraintEqualToAnchor:self.widgetView.leadingAnchor], |
| 61 [self.view.trailingAnchor |
| 62 constraintEqualToAnchor:self.widgetView.trailingAnchor], |
| 63 [self.view.topAnchor constraintEqualToAnchor:self.widgetView.topAnchor], |
| 64 [self.view.bottomAnchor |
| 65 constraintEqualToAnchor:self.widgetView.bottomAnchor] |
| 66 ]]; |
| 67 } |
| 68 |
| 69 - (void)viewWillAppear:(BOOL)animated { |
| 70 [super viewWillAppear:animated]; |
| 71 [self updateWidget]; |
| 72 } |
| 73 |
| 74 - (void)widgetPerformUpdateWithCompletionHandler: |
| 75 (void (^)(NCUpdateResult))completionHandler { |
| 76 completionHandler([self updateWidget] ? NCUpdateResultNewData |
| 77 : NCUpdateResultNoData); |
| 78 } |
| 79 |
| 80 #pragma mark - NCWidgetProviding |
| 81 |
| 82 - (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode |
| 83 withMaximumSize:(CGSize)maxSize { |
| 84 BOOL isVariableHeight = (activeDisplayMode == NCWidgetDisplayModeExpanded); |
| 85 |
| 86 // If the widget's height is not variable, the preferredContentSize is the |
| 87 // maxSize. Widgets cannot be shrunk, and this ensures the view will lay |
| 88 // itself out according to the actual screen size. (This is only likely to |
| 89 // happen if the accessibility option for larger font is used.) If the widget |
| 90 // is not a fixed size, if the fitting size for the widget's contents is |
| 91 // larger than the maximum size for the current widget display mode, this |
| 92 // maximum size is used for the widget. Otherwise, the preferredContentSize is |
| 93 // set to the fitting size so that the widget gets the correct height. |
| 94 if (isVariableHeight) { |
| 95 CGSize fittingSize = [self.widgetView |
| 96 systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; |
| 97 if (fittingSize.height < maxSize.height) { |
| 98 self.preferredContentSize = fittingSize; |
| 99 return; |
| 100 } |
| 101 } |
| 102 self.preferredContentSize = maxSize; |
| 103 } |
| 104 |
| 105 // Implementing this method removes the leading edge inset for iOS version < 10. |
| 106 // TODO(crbug.com/720490): Remove implementation when dropping ios9 support. |
| 107 #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0 |
| 108 - (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets: |
| 109 (UIEdgeInsets)defaultMarginInsets { |
| 110 return UIEdgeInsetsZero; |
| 111 } |
| 112 #endif |
| 113 |
| 114 #pragma mark - internal |
| 115 |
| 116 - (BOOL)updateWidget { |
| 117 return NO; |
| 118 } |
| 119 |
| 120 - (void)openAppWithURL:(NSString*)URL { |
| 121 NSUserDefaults* sharedDefaults = |
| 122 [[NSUserDefaults alloc] initWithSuiteName:app_group::ApplicationGroup()]; |
| 123 NSString* defaultsKey = |
| 124 base::SysUTF8ToNSString(app_group::kChromeAppGroupCommandPreference); |
| 125 |
| 126 NSString* timePrefKey = |
| 127 base::SysUTF8ToNSString(app_group::kChromeAppGroupCommandTimePreference); |
| 128 NSString* appPrefKey = |
| 129 base::SysUTF8ToNSString(app_group::kChromeAppGroupCommandAppPreference); |
| 130 NSString* commandPrefKey = base::SysUTF8ToNSString( |
| 131 app_group::kChromeAppGroupCommandCommandPreference); |
| 132 NSString* paramPrefKey = base::SysUTF8ToNSString( |
| 133 app_group::kChromeAppGroupCommandParameterPreference); |
| 134 |
| 135 NSDictionary* commandDict = @{ |
| 136 timePrefKey : [NSDate date], |
| 137 appPrefKey : @"TodayExtension", |
| 138 commandPrefKey : |
| 139 base::SysUTF8ToNSString(app_group::kChromeAppGroupOpenURLCommand), |
| 140 paramPrefKey : URL, |
| 141 }; |
| 142 |
| 143 [sharedDefaults setObject:commandDict forKey:defaultsKey]; |
| 144 [sharedDefaults synchronize]; |
| 145 |
| 146 NSString* scheme = base::mac::ObjCCast<NSString>([[NSBundle mainBundle] |
| 147 objectForInfoDictionaryKey:@"KSChannelChromeScheme"]); |
| 148 if (!scheme) |
| 149 return; |
| 150 |
| 151 NSURLComponents* urlComponents = [NSURLComponents new]; |
| 152 urlComponents.scheme = scheme; |
| 153 urlComponents.host = kXCallbackURLHost; |
| 154 urlComponents.path = [@"/" |
| 155 stringByAppendingString:base::SysUTF8ToNSString( |
| 156 app_group::kChromeAppGroupXCallbackCommand)]; |
| 157 |
| 158 NSURL* openURL = [urlComponents URL]; |
| 159 [self.extensionContext openURL:openURL completionHandler:nil]; |
| 160 } |
| 161 |
| 162 @end |
| OLD | NEW |