You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiMeetView.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright @ 2017-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <CoreText/CoreText.h>
  17. #import <Intents/Intents.h>
  18. #include <mach/mach_time.h>
  19. #import <React/RCTAssert.h>
  20. #import <React/RCTLinkingManager.h>
  21. #import <React/RCTRootView.h>
  22. #import "JitsiMeetView+Private.h"
  23. #import "RCTBridgeWrapper.h"
  24. /**
  25. * A `RCTFatalHandler` implementation which swallows JavaScript errors. In the
  26. * Release configuration, React Native will (intentionally) raise an unhandled
  27. * `NSException` for an unhandled JavaScript error. This will effectively kill
  28. * the application. `_RCTFatal` is suitable to be in accord with the Web i.e.
  29. * not kill the application.
  30. */
  31. RCTFatalHandler _RCTFatal = ^(NSError *error) {
  32. id jsStackTrace = error.userInfo[RCTJSStackTraceKey];
  33. @try {
  34. NSString *name
  35. = [NSString stringWithFormat:@"%@: %@",
  36. RCTFatalExceptionName,
  37. error.localizedDescription];
  38. NSString *message
  39. = RCTFormatError(error.localizedDescription, jsStackTrace, 75);
  40. [NSException raise:name format:@"%@", message];
  41. } @catch (NSException *e) {
  42. if (!jsStackTrace) {
  43. @throw;
  44. }
  45. }
  46. };
  47. /**
  48. * Helper function to dynamically load custom fonts. The `UIAppFonts` key in the
  49. * plist file doesn't work for frameworks, so fonts have to be manually loaded.
  50. */
  51. void loadCustomFonts(Class clazz) {
  52. NSBundle *bundle = [NSBundle bundleForClass:clazz];
  53. NSArray *fonts = [bundle objectForInfoDictionaryKey:@"JitsiMeetFonts"];
  54. for (NSString *item in fonts) {
  55. NSString *fontName = [item stringByDeletingPathExtension];
  56. NSString *fontExt = [item pathExtension];
  57. NSString *fontPath = [bundle pathForResource:fontName ofType:fontExt];
  58. NSData *inData = [NSData dataWithContentsOfFile:fontPath];
  59. CFErrorRef error;
  60. CGDataProviderRef provider
  61. = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData);
  62. CGFontRef font = CGFontCreateWithDataProvider(provider);
  63. if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
  64. CFStringRef errorDescription = CFErrorCopyDescription(error);
  65. NSLog(@"Failed to load font: %@", errorDescription);
  66. CFRelease(errorDescription);
  67. }
  68. CFRelease(font);
  69. CFRelease(provider);
  70. }
  71. }
  72. /**
  73. * Helper function to register a fatal error handler for React. Our handler
  74. * won't kill the process, it will swallow JS errors and print stack traces
  75. * instead.
  76. */
  77. void registerFatalErrorHandler() {
  78. #if !DEBUG
  79. // In the Release configuration, React Native will (intentionally) raise an
  80. // unhandled `NSException` for an unhandled JavaScript error. This will
  81. // effectively kill the application. In accord with the Web, do not kill the
  82. // application.
  83. if (!RCTGetFatalHandler()) {
  84. RCTSetFatalHandler(_RCTFatal);
  85. }
  86. #endif
  87. }
  88. @interface JitsiMeetView() {
  89. /**
  90. * The unique identifier of this `JitsiMeetView` within the process for the
  91. * purposes of `ExternalAPI`. The name scope was inspired by postis which we
  92. * use on Web for the similar purposes of the iframe-based external API.
  93. */
  94. NSString *externalAPIScope;
  95. RCTRootView *rootView;
  96. }
  97. @end
  98. @implementation JitsiMeetView {
  99. NSNumber *_pictureInPictureEnabled;
  100. }
  101. @dynamic pictureInPictureEnabled;
  102. static RCTBridgeWrapper *bridgeWrapper;
  103. /**
  104. * Copy of the `launchOptions` dictionary that the application was started with.
  105. * It is required for the initial URL to be used if a (Universal) link was used
  106. * to launch a new instance of the application.
  107. */
  108. static NSDictionary *_launchOptions;
  109. /**
  110. * The `JitsiMeetView`s associated with their `ExternalAPI` scopes (i.e. unique
  111. * identifiers within the process).
  112. */
  113. static NSMapTable<NSString *, JitsiMeetView *> *views;
  114. + (BOOL)application:(UIApplication *)application
  115. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  116. // Store launch options, will be used when we create the bridge.
  117. _launchOptions = [launchOptions copy];
  118. return YES;
  119. }
  120. #pragma mark Linking delegate helpers
  121. // https://facebook.github.io/react-native/docs/linking.html
  122. + (BOOL)application:(UIApplication *)application
  123. continueUserActivity:(NSUserActivity *)userActivity
  124. restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler
  125. {
  126. NSString *activityType = userActivity.activityType;
  127. // XXX At least twice we received bug reports about malfunctioning loadURL
  128. // in the Jitsi Meet SDK while the Jitsi Meet app seemed to functioning as
  129. // expected in our testing. But that was to be expected because the app does
  130. // not exercise loadURL. In order to increase the test coverage of loadURL,
  131. // channel Universal linking through loadURL.
  132. if ([activityType isEqualToString:NSUserActivityTypeBrowsingWeb]
  133. && [self loadURLInViews:userActivity.webpageURL]) {
  134. return YES;
  135. }
  136. // Check for a CallKit intent.
  137. if ([activityType isEqualToString:@"INStartAudioCallIntent"]
  138. || [activityType isEqualToString:@"INStartVideoCallIntent"]) {
  139. INIntent *intent = userActivity.interaction.intent;
  140. NSArray<INPerson *> *contacts;
  141. NSString *url;
  142. BOOL startAudioOnly = NO;
  143. if ([intent isKindOfClass:[INStartAudioCallIntent class]]) {
  144. contacts = ((INStartAudioCallIntent *) intent).contacts;
  145. startAudioOnly = YES;
  146. } else if ([intent isKindOfClass:[INStartVideoCallIntent class]]) {
  147. contacts = ((INStartVideoCallIntent *) intent).contacts;
  148. }
  149. if (contacts && (url = contacts.firstObject.personHandle.value)) {
  150. // Load the URL contained in the handle.
  151. [self loadURLObjectInViews:@{
  152. @"config": @{
  153. @"startAudioOnly": @(startAudioOnly)
  154. },
  155. @"url": url
  156. }];
  157. return YES;
  158. }
  159. }
  160. return [RCTLinkingManager application:application
  161. continueUserActivity:userActivity
  162. restorationHandler:restorationHandler];
  163. }
  164. + (BOOL)application:(UIApplication *)application
  165. openURL:(NSURL *)url
  166. sourceApplication:(NSString *)sourceApplication
  167. annotation:(id)annotation {
  168. // XXX At least twice we received bug reports about malfunctioning loadURL
  169. // in the Jitsi Meet SDK while the Jitsi Meet app seemed to functioning as
  170. // expected in our testing. But that was to be expected because the app does
  171. // not exercise loadURL. In order to increase the test coverage of loadURL,
  172. // channel Universal linking through loadURL.
  173. if ([self loadURLInViews:url]) {
  174. return YES;
  175. }
  176. return [RCTLinkingManager application:application
  177. openURL:url
  178. sourceApplication:sourceApplication
  179. annotation:annotation];
  180. }
  181. #pragma mark Initializers
  182. - (instancetype)init {
  183. self = [super init];
  184. if (self) {
  185. [self initWithXXX];
  186. }
  187. return self;
  188. }
  189. - (instancetype)initWithCoder:(NSCoder *)coder {
  190. self = [super initWithCoder:coder];
  191. if (self) {
  192. [self initWithXXX];
  193. }
  194. return self;
  195. }
  196. - (instancetype)initWithFrame:(CGRect)frame {
  197. self = [super initWithFrame:frame];
  198. if (self) {
  199. [self initWithXXX];
  200. }
  201. return self;
  202. }
  203. #pragma mark API
  204. /**
  205. * Loads a specific `NSURL` which may identify a conference to join. If the
  206. * specified `NSURL` is `nil` and the Welcome page is enabled, the Welcome page
  207. * is displayed instead.
  208. *
  209. * @param url The `NSURL` to load which may identify a conference to join.
  210. */
  211. - (void)loadURL:(NSURL *)url {
  212. [self loadURLString:url ? url.absoluteString : nil];
  213. }
  214. /**
  215. * Loads a specific URL which may identify a conference to join. The URL is
  216. * specified in the form of an `NSDictionary` of properties which (1)
  217. * internally are sufficient to construct a URL `NSString` while (2) abstracting
  218. * the specifics of constructing the URL away from API clients/consumers. If the
  219. * specified URL is `nil` and the Welcome page is enabled, the Welcome page is
  220. * displayed instead.
  221. *
  222. * @param urlObject The URL to load which may identify a conference to join.
  223. */
  224. - (void)loadURLObject:(NSDictionary *)urlObject {
  225. NSMutableDictionary *props = [[NSMutableDictionary alloc] init];
  226. if (self.defaultURL) {
  227. props[@"defaultURL"] = [self.defaultURL absoluteString];
  228. }
  229. props[@"externalAPIScope"] = externalAPIScope;
  230. props[@"pictureInPictureEnabled"] = @(self.pictureInPictureEnabled);
  231. props[@"welcomePageEnabled"] = @(self.welcomePageEnabled);
  232. // XXX If urlObject is nil, then it must appear as undefined in the
  233. // JavaScript source code so that we check the launchOptions there.
  234. if (urlObject) {
  235. props[@"url"] = urlObject;
  236. }
  237. // XXX The method loadURLObject: is supposed to be imperative i.e. a second
  238. // invocation with one and the same URL is expected to join the respective
  239. // conference again if the first invocation was followed by leaving the
  240. // conference. However, React and, respectively,
  241. // appProperties/initialProperties are declarative expressions i.e. one and
  242. // the same URL will not trigger componentWillReceiveProps in the JavaScript
  243. // source code. The workaround implemented bellow introduces imperativeness
  244. // in React Component props by defining a unique value per loadURLObject:
  245. // invocation.
  246. props[@"timestamp"] = @(mach_absolute_time());
  247. if (rootView) {
  248. // Update props with the new URL.
  249. rootView.appProperties = props;
  250. } else {
  251. rootView
  252. = [[RCTRootView alloc] initWithBridge:bridgeWrapper.bridge
  253. moduleName:@"App"
  254. initialProperties:props];
  255. rootView.backgroundColor = self.backgroundColor;
  256. // Add rootView as a subview which completely covers this one.
  257. [rootView setFrame:[self bounds]];
  258. rootView.autoresizingMask
  259. = UIViewAutoresizingFlexibleWidth
  260. | UIViewAutoresizingFlexibleHeight;
  261. [self addSubview:rootView];
  262. }
  263. }
  264. /**
  265. * Loads a specific URL `NSString` which may identify a conference to
  266. * join. If the specified URL `NSString` is `nil` and the Welcome page is
  267. * enabled, the Welcome page is displayed instead.
  268. *
  269. * @param urlString The URL `NSString` to load which may identify a conference
  270. * to join.
  271. */
  272. - (void)loadURLString:(NSString *)urlString {
  273. [self loadURLObject:urlString ? @{ @"url": urlString } : nil];
  274. }
  275. #pragma pictureInPictureEnabled getter / setter
  276. - (void) setPictureInPictureEnabled:(BOOL)pictureInPictureEnabled {
  277. _pictureInPictureEnabled
  278. = [NSNumber numberWithBool:pictureInPictureEnabled];
  279. }
  280. - (BOOL) pictureInPictureEnabled {
  281. if (_pictureInPictureEnabled) {
  282. return [_pictureInPictureEnabled boolValue];
  283. }
  284. // The SDK/JitsiMeetView client/consumer did not explicitly enable/disable
  285. // Picture-in-Picture. However, we may automatically deduce their
  286. // intentions: we need the support of the client in order to implement
  287. // Picture-in-Picture on iOS (in contrast to Android) so if the client
  288. // appears to have provided the support then we can assume that they did it
  289. // with the intention to have Picture-in-Picture enabled.
  290. return self.delegate
  291. && [self.delegate respondsToSelector:@selector(enterPictureInPicture:)];
  292. }
  293. #pragma mark Private methods
  294. /**
  295. * Loads a specific `NSURL` in all existing `JitsiMeetView`s.
  296. *
  297. * @param url The `NSURL` to load in all existing `JitsiMeetView`s.
  298. * @return `YES` if the specified `url` was submitted for loading in at least
  299. * one `JitsiMeetView`; otherwise, `NO`.
  300. */
  301. + (BOOL)loadURLInViews:(NSURL *)url {
  302. return
  303. [self loadURLObjectInViews:url ? @{ @"url": url.absoluteString } : nil];
  304. }
  305. + (BOOL)loadURLObjectInViews:(NSDictionary *)urlObject {
  306. BOOL handled = NO;
  307. if (views) {
  308. for (NSString *externalAPIScope in views) {
  309. JitsiMeetView *view
  310. = [self viewForExternalAPIScope:externalAPIScope];
  311. if (view) {
  312. [view loadURLObject:urlObject];
  313. handled = YES;
  314. }
  315. }
  316. }
  317. return handled;
  318. }
  319. + (instancetype)viewForExternalAPIScope:(NSString *)externalAPIScope {
  320. return [views objectForKey:externalAPIScope];
  321. }
  322. /**
  323. * Internal initialization:
  324. *
  325. * - sets the background color
  326. * - creates the React bridge
  327. * - loads the necessary custom fonts
  328. * - registers a custom fatal error error handler for React
  329. */
  330. - (void)initWithXXX {
  331. static dispatch_once_t dispatchOncePredicate;
  332. dispatch_once(&dispatchOncePredicate, ^{
  333. // Initialize the static state of JitsiMeetView.
  334. bridgeWrapper
  335. = [[RCTBridgeWrapper alloc] initWithLaunchOptions:_launchOptions];
  336. views = [NSMapTable strongToWeakObjectsMapTable];
  337. // Dynamically load custom bundled fonts.
  338. loadCustomFonts(self.class);
  339. // Register a fatal error handler for React.
  340. registerFatalErrorHandler();
  341. });
  342. // Hook this JitsiMeetView into ExternalAPI.
  343. if (!externalAPIScope) {
  344. externalAPIScope = [NSUUID UUID].UUIDString;
  345. [views setObject:self forKey:externalAPIScope];
  346. }
  347. // Set a background color which is in accord with the JavaScript and Android
  348. // parts of the application and causes less perceived visual flicker than
  349. // the default background color.
  350. self.backgroundColor
  351. = [UIColor colorWithRed:.07f green:.07f blue:.07f alpha:1];
  352. }
  353. @end