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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #include <mach/mach_time.h>
  18. @import Intents;
  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. static RCTBridgeWrapper *bridgeWrapper;
  100. /**
  101. * Copy of the `launchOptions` dictionary that the application was started with.
  102. * It is required for the initial URL to be used if a (Universal) link was used
  103. * to launch a new instance of the application.
  104. */
  105. static NSDictionary *_launchOptions;
  106. /**
  107. * The `JitsiMeetView`s associated with their `ExternalAPI` scopes (i.e. unique
  108. * identifiers within the process).
  109. */
  110. static NSMapTable<NSString *, JitsiMeetView *> *views;
  111. + (BOOL)application:(UIApplication *)application
  112. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  113. // Store launch options, will be used when we create the bridge.
  114. _launchOptions = [launchOptions copy];
  115. return YES;
  116. }
  117. #pragma mark Linking delegate helpers
  118. // https://facebook.github.io/react-native/docs/linking.html
  119. + (BOOL)application:(UIApplication *)application
  120. continueUserActivity:(NSUserActivity *)userActivity
  121. restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler
  122. {
  123. NSString *activityType = userActivity.activityType;
  124. // XXX At least twice we received bug reports about malfunctioning loadURL
  125. // in the Jitsi Meet SDK while the Jitsi Meet app seemed to functioning as
  126. // expected in our testing. But that was to be expected because the app does
  127. // not exercise loadURL. In order to increase the test coverage of loadURL,
  128. // channel Universal linking through loadURL.
  129. if ([activityType isEqualToString:NSUserActivityTypeBrowsingWeb]
  130. && [self loadURLInViews:userActivity.webpageURL]) {
  131. return YES;
  132. }
  133. // Check for a CallKit intent.
  134. if ([activityType isEqualToString:@"INStartAudioCallIntent"]
  135. || [activityType isEqualToString:@"INStartVideoCallIntent"]) {
  136. INIntent *intent = userActivity.interaction.intent;
  137. NSArray<INPerson *> *contacts;
  138. NSString *url;
  139. BOOL startAudioOnly = NO;
  140. if ([intent isKindOfClass:[INStartAudioCallIntent class]]) {
  141. contacts = ((INStartAudioCallIntent *) intent).contacts;
  142. startAudioOnly = YES;
  143. } else if ([intent isKindOfClass:[INStartVideoCallIntent class]]) {
  144. contacts = ((INStartVideoCallIntent *) intent).contacts;
  145. }
  146. if (contacts && (url = contacts.firstObject.personHandle.value)) {
  147. // Load the URL contained in the handle.
  148. [self loadURLObjectInViews:@{
  149. @"config": @{
  150. @"startAudioOnly": @(startAudioOnly)
  151. },
  152. @"url": url
  153. }];
  154. return YES;
  155. }
  156. }
  157. return [RCTLinkingManager application:application
  158. continueUserActivity:userActivity
  159. restorationHandler:restorationHandler];
  160. }
  161. + (BOOL)application:(UIApplication *)application
  162. openURL:(NSURL *)url
  163. sourceApplication:(NSString *)sourceApplication
  164. annotation:(id)annotation {
  165. // XXX At least twice we received bug reports about malfunctioning loadURL
  166. // in the Jitsi Meet SDK while the Jitsi Meet app seemed to functioning as
  167. // expected in our testing. But that was to be expected because the app does
  168. // not exercise loadURL. In order to increase the test coverage of loadURL,
  169. // channel Universal linking through loadURL.
  170. if ([self loadURLInViews:url]) {
  171. return YES;
  172. }
  173. return [RCTLinkingManager application:application
  174. openURL:url
  175. sourceApplication:sourceApplication
  176. annotation:annotation];
  177. }
  178. #pragma mark Initializers
  179. - (instancetype)init {
  180. self = [super init];
  181. if (self) {
  182. [self initWithXXX];
  183. }
  184. return self;
  185. }
  186. - (instancetype)initWithCoder:(NSCoder *)coder {
  187. self = [super initWithCoder:coder];
  188. if (self) {
  189. [self initWithXXX];
  190. }
  191. return self;
  192. }
  193. - (instancetype)initWithFrame:(CGRect)frame {
  194. self = [super initWithFrame:frame];
  195. if (self) {
  196. [self initWithXXX];
  197. }
  198. return self;
  199. }
  200. #pragma mark API
  201. /**
  202. * Loads a specific `NSURL` which may identify a conference to join. If the
  203. * specified `NSURL` is `nil` and the Welcome page is enabled, the Welcome page
  204. * is displayed instead.
  205. *
  206. * @param url The `NSURL` to load which may identify a conference to join.
  207. */
  208. - (void)loadURL:(NSURL *)url {
  209. [self loadURLString:url ? url.absoluteString : nil];
  210. }
  211. /**
  212. * Loads a specific URL which may identify a conference to join. The URL is
  213. * specified in the form of an `NSDictionary` of properties which (1)
  214. * internally are sufficient to construct a URL `NSString` while (2) abstracting
  215. * the specifics of constructing the URL away from API clients/consumers. If the
  216. * specified URL is `nil` and the Welcome page is enabled, the Welcome page is
  217. * displayed instead.
  218. *
  219. * @param urlObject The URL to load which may identify a conference to join.
  220. */
  221. - (void)loadURLObject:(NSDictionary *)urlObject {
  222. NSMutableDictionary *props = [[NSMutableDictionary alloc] init];
  223. if (self.defaultURL) {
  224. props[@"defaultURL"] = [self.defaultURL absoluteString];
  225. }
  226. props[@"externalAPIScope"] = externalAPIScope;
  227. props[@"welcomePageEnabled"] = @(self.welcomePageEnabled);
  228. // XXX If urlObject is nil, then it must appear as undefined in the
  229. // JavaScript source code so that we check the launchOptions there.
  230. if (urlObject) {
  231. props[@"url"] = urlObject;
  232. }
  233. // XXX The method loadURLObject: is supposed to be imperative i.e. a second
  234. // invocation with one and the same URL is expected to join the respective
  235. // conference again if the first invocation was followed by leaving the
  236. // conference. However, React and, respectively,
  237. // appProperties/initialProperties are declarative expressions i.e. one and
  238. // the same URL will not trigger componentWillReceiveProps in the JavaScript
  239. // source code. The workaround implemented bellow introduces imperativeness
  240. // in React Component props by defining a unique value per loadURLObject:
  241. // invocation.
  242. props[@"timestamp"] = @(mach_absolute_time());
  243. if (rootView) {
  244. // Update props with the new URL.
  245. rootView.appProperties = props;
  246. } else {
  247. rootView
  248. = [[RCTRootView alloc] initWithBridge:bridgeWrapper.bridge
  249. moduleName:@"App"
  250. initialProperties:props];
  251. rootView.backgroundColor = self.backgroundColor;
  252. // Add rootView as a subview which completely covers this one.
  253. [rootView setFrame:[self bounds]];
  254. [self addSubview:rootView];
  255. }
  256. }
  257. /**
  258. * Loads a specific URL `NSString` which may identify a conference to
  259. * join. If the specified URL `NSString` is `nil` and the Welcome page is
  260. * enabled, the Welcome page is displayed instead.
  261. *
  262. * @param urlString The URL `NSString` to load which may identify a conference
  263. * to join.
  264. */
  265. - (void)loadURLString:(NSString *)urlString {
  266. [self loadURLObject:urlString ? @{ @"url": urlString } : nil];
  267. }
  268. #pragma mark Private methods
  269. /**
  270. * Loads a specific `NSURL` in all existing `JitsiMeetView`s.
  271. *
  272. * @param url The `NSURL` to load in all existing `JitsiMeetView`s.
  273. * @return `YES` if the specified `url` was submitted for loading in at least
  274. * one `JitsiMeetView`; otherwise, `NO`.
  275. */
  276. + (BOOL)loadURLInViews:(NSURL *)url {
  277. return
  278. [self loadURLObjectInViews:url ? @{ @"url": url.absoluteString } : nil];
  279. }
  280. + (BOOL)loadURLObjectInViews:(NSDictionary *)urlObject {
  281. BOOL handled = NO;
  282. if (views) {
  283. for (NSString *externalAPIScope in views) {
  284. JitsiMeetView *view
  285. = [self viewForExternalAPIScope:externalAPIScope];
  286. if (view) {
  287. [view loadURLObject:urlObject];
  288. handled = YES;
  289. }
  290. }
  291. }
  292. return handled;
  293. }
  294. + (instancetype)viewForExternalAPIScope:(NSString *)externalAPIScope {
  295. return [views objectForKey:externalAPIScope];
  296. }
  297. /**
  298. * Internal initialization:
  299. *
  300. * - sets the background color
  301. * - creates the React bridge
  302. * - loads the necessary custom fonts
  303. * - registers a custom fatal error error handler for React
  304. */
  305. - (void)initWithXXX {
  306. static dispatch_once_t dispatchOncePredicate;
  307. dispatch_once(&dispatchOncePredicate, ^{
  308. // Initialize the static state of JitsiMeetView.
  309. bridgeWrapper
  310. = [[RCTBridgeWrapper alloc] initWithLaunchOptions:_launchOptions];
  311. views = [NSMapTable strongToWeakObjectsMapTable];
  312. // Dynamically load custom bundled fonts.
  313. loadCustomFonts(self.class);
  314. // Register a fatal error handler for React.
  315. registerFatalErrorHandler();
  316. });
  317. // Hook this JitsiMeetView into ExternalAPI.
  318. if (!externalAPIScope) {
  319. externalAPIScope = [NSUUID UUID].UUIDString;
  320. [views setObject:self forKey:externalAPIScope];
  321. }
  322. // Set a background color which is in accord with the JavaScript and Android
  323. // parts of the application and causes less perceived visual flicker than
  324. // the default background color.
  325. self.backgroundColor
  326. = [UIColor colorWithRed:.07f green:.07f blue:.07f alpha:1];
  327. }
  328. @end