Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): resolve sandbox dir issue in debug mode #4025

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/ios/base/bridge/HippyBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
@property (nonatomic, copy, readonly) NSArray<NSURL *> *bundleURLs;

/// Path of sandbox directory
@property (nonatomic, strong) NSURL *sandboxDirectory;
@property (nonatomic, strong) NSString *sandboxDirectory;

/// Shared data between different rootViews on same bridge.
/// Set by HippyRootView when runHippyApplication.
Expand Down
12 changes: 7 additions & 5 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ @interface HippyBridge() {
BOOL _valid;
HippyBundleOperationQueue *_bundlesQueue;
NSMutableArray<NSURL *> *_bundleURLs;
NSURL *_sandboxDirectory;

std::shared_ptr<VFSUriLoader> _uriLoader;
std::shared_ptr<hippy::RootNode> _rootNode;
Expand Down Expand Up @@ -472,9 +471,10 @@ - (void)setUp {
[self loadPendingVendorBundleURLIfNeeded];

// Set the default sandbox directory
[self setSandboxDirectory:[_pendingLoadingVendorBundleURL URLByDeletingLastPathComponent]];
}
NSString *sandboxDir = [HippyUtils getBaseDirFromResourcePath:_pendingLoadingVendorBundleURL];
[self setSandboxDirectory:sandboxDir];

}

/// 加载初始化bridge时传入的Bundle URL
- (void)loadPendingVendorBundleURLIfNeeded {
Expand Down Expand Up @@ -1195,10 +1195,12 @@ - (void)registerModuleForFrameUpdates:(id<HippyBridgeModule>)module withModuleDa
[_displayLink registerModuleForFrameUpdates:module withModuleData:moduleData];
}

- (void)setSandboxDirectory:(NSURL *)sandboxDirectory {
- (void)setSandboxDirectory:(NSString *)sandboxDirectory {
if (![_sandboxDirectory isEqual:sandboxDirectory]) {
_sandboxDirectory = sandboxDirectory;
[self.javaScriptExecutor setSandboxDirectory:[sandboxDirectory absoluteString]];
if (sandboxDirectory) {
[self.javaScriptExecutor setSandboxDirectory:sandboxDirectory];
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion framework/ios/module/loader/HippyFileHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
return;
}

NSURL *absoluteURL = AbsoluteURLFromHippyFileURL(url, bridge.sandboxDirectory);
NSURL *absoluteURL = AbsoluteURLFromHippyFileURL(url, [NSURL URLWithString:bridge.sandboxDirectory]);
if ([absoluteURL isFileURL] || [absoluteURL isFileReferenceURL]) {
void (^opBlock)() = ^{
NSError *error;
Expand Down
6 changes: 6 additions & 0 deletions modules/ios/base/HippyUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ HIPPY_EXTERN NSStringEncoding HippyGetStringEncodingFromURLResponse(NSURLRespons
/// users may call it dynamically to determine the Hippy version number.
+ (NSString *)sdkVersion;

/// Get base directory from given file resource URL.
/// If it is a file URL, return the directory by deleting the last path component.
/// Otherwise, return the string truncated after the last slash.
/// - Parameter url: The given URL.
+ (NSString *)getBaseDirFromResourcePath:(NSURL *)url;

@end


Expand Down
17 changes: 17 additions & 0 deletions modules/ios/base/HippyUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -712,4 +712,21 @@ + (NSString *)sdkVersion {
return @(HIPPY_STR(HIPPY_VERSION));
}

+ (NSString *)getBaseDirFromResourcePath:(NSURL *)url {
NSString *urlString = url.absoluteString;
NSRange lastSlashRange = [urlString rangeOfString:@"/" options:NSBackwardsSearch];
if (lastSlashRange.location == NSNotFound) {
// Return the original string if no slash is found
return urlString;
}

if ([url isFileURL] || [url isFileReferenceURL]) {
// For file URLs, remove the last path component
return [url URLByDeletingLastPathComponent].absoluteString;
} else{
// For http/https URLs, truncate after the last slash in the base URL
return [urlString substringToIndex:lastSlashRange.location + 1];
}
}

@end
3 changes: 2 additions & 1 deletion renderer/native/ios/renderer/HippyRootView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#import "HippyBridge.h"
#import "Hippybridge+PerformanceAPI.h"
#import "HippyUIManager.h"
#import "HippyUtils.h"
#import "HippyDeviceBaseInfo.h"
#import "HippyTouchHandler.h"
#import "HippyJSExecutor.h"
Expand Down Expand Up @@ -158,7 +159,7 @@ - (instancetype)initWithBridge:(HippyBridge *)bridge
_hasBusinessBundleToLoad = YES;

// Set the default sandbox directory
[bridge setSandboxDirectory:[businessURL URLByDeletingLastPathComponent]];
[bridge setSandboxDirectory:[HippyUtils getBaseDirFromResourcePath:businessURL]];
}
if (self = [self initWithBridge:bridge
moduleName:moduleName
Expand Down
25 changes: 25 additions & 0 deletions tests/ios/HippyUtilsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,30 @@ - (void)testHippyURLWithString {

}

- (void)testGetBaseDirFromResourcePath {
// Test with an HTTP URL
NSURL *httpURL = [NSURL URLWithString:@"http://localhost:38989/index.bundle?platform=ios"];
NSString *expectedBaseHTTPURL = @"http://localhost:38989/";
NSString *baseHTTPURLString = [HippyUtils getBaseDirFromResourcePath:httpURL];
XCTAssertEqualObjects(baseHTTPURLString, expectedBaseHTTPURL, @"The base URL should be truncated after the last slash.");

// Test with a file URL
NSURL *fileURL = [NSURL fileURLWithPath:@"/Users/username/Documents/test.txt"];
NSString *expectedBaseFileURL = @"file:///Users/username/Documents/";
NSString *baseFileURLString = [HippyUtils getBaseDirFromResourcePath:fileURL];
XCTAssertEqualObjects(baseFileURLString, expectedBaseFileURL, @"The base file URL should be the directory path.");

// Test with a URL with no slashes
NSURL *noSlashesURL = [NSURL URLWithString:@"mailto:[email protected]"];
NSString *expectedNoSlashesURL = @"mailto:[email protected]";
NSString *baseNoSlashesURLString = [HippyUtils getBaseDirFromResourcePath:noSlashesURL];
XCTAssertEqualObjects(baseNoSlashesURLString, expectedNoSlashesURL, @"The URL with no slashes should be returned as is.");

// Test with a nil URL
NSURL *nilURL = nil;
NSString *baseNilURLString = [HippyUtils getBaseDirFromResourcePath:nilURL];
XCTAssertNil(baseNilURLString, @"The base URL should be nil for a nil input URL.");
}


@end
Loading