"Fossies" - the Fresh Open Source Software Archive

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