"Fossies" - the Fresh Open Source Software Archive

Member "jitsi-meet-6444/ios/sdk/src/RCTBridgeWrapper.m" (8 Aug 2022, 4237 Bytes) of package /linux/misc/jitsi-meet-6444.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Matlab source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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 
   17 #include "RCTBridgeWrapper.h"
   18 
   19 /**
   20  * Wrapper around RCTBridge which also implements the RCTBridgeDelegate methods,
   21  * allowing us to specify where the bundles are loaded from.
   22  */
   23 @implementation RCTBridgeWrapper
   24 
   25 - (instancetype)init {
   26     self = [super init];
   27     if (self) {
   28         _bridge
   29             = [[RCTBridge alloc] initWithDelegate:self
   30                                     launchOptions:nil];
   31     }
   32 
   33     return self;
   34 }
   35 
   36 - (void)invalidate {
   37     [_bridge invalidate];
   38 }
   39 
   40 #pragma mark helper methods for getting the packager URL
   41 
   42 #if DEBUG
   43 static NSURL *serverRootWithHost(NSString *host) {
   44     return
   45         [NSURL URLWithString:
   46                 [NSString stringWithFormat:@"http://%@:8081/", host]];
   47 }
   48 
   49 - (BOOL)isPackagerRunning:(NSString *)host {
   50     NSURL *url = [serverRootWithHost(host) URLByAppendingPathComponent:@"status"];
   51 
   52     NSURLSession *session = [NSURLSession sharedSession];
   53     NSURLRequest *request = [NSURLRequest requestWithURL:url];
   54     __block NSURLResponse *response;
   55     __block NSData *data;
   56 
   57     dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
   58     [[session dataTaskWithRequest:request
   59                 completionHandler:^(NSData *d,
   60                                     NSURLResponse *res,
   61                                     __unused NSError *err) {
   62                     data = d;
   63                     response = res;
   64                     dispatch_semaphore_signal(semaphore);
   65                 }] resume];
   66     dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
   67 
   68     NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
   69     return [status isEqualToString:@"packager-status:running"];
   70 }
   71 
   72 - (NSString *)guessPackagerHost {
   73     static NSString *ipGuess;
   74     static dispatch_once_t dispatchOncePredicate;
   75 
   76     dispatch_once(&dispatchOncePredicate, ^{
   77         NSString *ipPath
   78             = [[NSBundle bundleForClass:self.class] pathForResource:@"ip"
   79                                                              ofType:@"txt"];
   80 
   81         ipGuess
   82             = [[NSString stringWithContentsOfFile:ipPath
   83                                          encoding:NSUTF8StringEncoding
   84                                             error:nil]
   85                     stringByTrimmingCharactersInSet:
   86                             [NSCharacterSet newlineCharacterSet]];
   87     });
   88 
   89     NSString *host = ipGuess ?: @"localhost";
   90 
   91     if ([self isPackagerRunning:host]) {
   92         return host;
   93     }
   94 
   95     return nil;
   96 }
   97 #endif
   98 
   99 #pragma mark RCTBridgeDelegate methods
  100 
  101 - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  102 #if DEBUG
  103     // In debug mode, try to fetch the bundle from the packager, or fallback to
  104     // the one inside the framework. The IP address for the packager host is
  105     // fetched from the ip.txt file inside the framework.
  106     //
  107     // This duplicates some functionality present in RCTBundleURLProvider, but
  108     // that mode is not designed to work inside a framework, because all
  109     // resources are loaded from the main bundle.
  110     NSString *host = [self guessPackagerHost];
  111 
  112     if (host != nil) {
  113         NSString *path = @"/index.bundle";
  114         NSString *query = @"platform=ios&dev=true&minify=false";
  115         NSURLComponents *components
  116             = [NSURLComponents componentsWithURL:serverRootWithHost(host)
  117                          resolvingAgainstBaseURL:NO];
  118 
  119         components.path = path;
  120         components.query = query;
  121 
  122         return components.URL;
  123     }
  124 #endif
  125 
  126     return [[NSBundle bundleForClass:self.class] URLForResource:@"main"
  127                                                   withExtension:@"jsbundle"];
  128 }
  129 
  130 @end