Skip to content

[SR-4906] Separate cookieStorage by application #1009

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

Merged
merged 1 commit into from
May 29, 2017

Conversation

mamabusi
Copy link
Contributor

No description provided.

@@ -227,18 +227,22 @@ class TestNSHTTPCookieStorage: XCTestCase {
storage.setCookie(testCookie)
XCTAssertEqual(storage.cookies!.count, 3)
var destPath: String
//let bundleName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
//TODO: Above api returns nil on Linux when this code is introduced. Hence, using the below workaround.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like an expected failure. A macOS command line tool won't necessarily have an Info.plist with the bundle display name either. I don't think you should check in this commented-out code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On linux, NSBundle.infoDictionary:

 infoDictionary: [String : Any]? {
        let cfDict: CFDictionary? = CFBundleGetInfoDictionary(_bundle)

I printed the cfDict and saw the below values:

Optional<Dictionary<AnyHashable, Any>>
Key: CFBundleShortVersionString Value: 1.0
Key: CFBundleDevelopmentRegion Value: en
Key: NSHumanReadableCopyright Value: Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
Key: CFBundleInfoDictionaryVersion Value: 6.0
Key: CFBundleName Value: TestFoundation
Key: CFBundleIdentifier Value: org.swift.TestFoundation
Key: CFBundleVersion Value: 1
Key: CFBundleNumericVersion Value: 16809984
Key: NSPrincipalClass Value:
Key: CFBundleExecutable Value: TestFoundation
Key: CFBundlePackageType Value: APPL
Key: CFBundleSignature Value: ????

It does have the Bundle name and other details, whereas the cast
return dict as! [String : Any]
fails. Thats why it looks like a bridging issue to me on Linux.

I’ve removed the commented code.

//Needs replacement once fixed.
let bundlePath = Bundle.main.bundlePath
var bundlePathComponents = bundlePath.components(separatedBy: "/")
let bundleName: String = "/" + bundlePathComponents[bundlePathComponents.count - 1]
Copy link
Contributor

@mattrajca mattrajca May 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't do the right thing for executables ("command line tools" created in Xcode). The last component will just be the parent directory.

The : String is not necessary as it will be inferred.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also simplify bundlePathComponents[bundlePathComponents.count - 1] to bundlePathComponents.last and safely unwrap the optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Searched around to check if there is any other way I can get the Product name details of the command line, but in vain. Any ideas please?

Yep, removing the explicit String.

bundlePathComponents.last it is. Thanks!

} else {
destPath = NSHomeDirectory() + "/.local/share/.cookies.shared"
destPath = NSHomeDirectory() + "/.local/share" + bundleName + "/.cookies.shared"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be an / after /.local/share?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"/" is appended to the bundleName during initialisation

var bundleName = "/" + bundlePath.components(separatedBy: "/").last!

return FileManager.default.currentDirectoryPath + fileName
//if we were unable to create the desired directory, create the cookie file
//in a subFolder (named after the bundle) of the `pwd`
return FileManager.default.currentDirectoryPath + "/" + bundleName + fileName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a subfolder, shouldn't there be a / between the bundleName and fileName?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename already has "/" prepended.

@@ -38,7 +38,7 @@ open class HTTPCookieStorage: NSObject {

private static var sharedStorage: HTTPCookieStorage?
private static var sharedCookieStorages: [String: HTTPCookieStorage] = [:] //for group storage containers

private var bundleName: String!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why store this value? Seems like a good candidate for a computed property (and you get to avoid the implicitly-unwrapped optional if you do that).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I have modified it. I compute the ‘bundleName’ now and pass it to the ‘filePath’ private function for reuse.

var bundlePathComponents = bundlePath.components(separatedBy: "/")
let bundleDesc: String = bundlePathComponents[bundlePathComponents.count - 1]
bundleName = bundleDesc
if let range = bundleDesc.range(of: ".", options: String.CompareOptions.backwards, range: nil, locale: nil) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a deletingPathExtension property on NSString you can use if that's what you're trying to do here.

https://developer.apple.com/reference/foundation/nsstring/1418214-deletingpathextension

You don't do this in the code below that also retrieves the bundle name though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NSString.deletingPathExtension is marked unavailable
now.

@mamabusi mamabusi force-pushed the appSpecificSharedCookies branch from 8c437e6 to 678ac50 Compare May 25, 2017 10:11
@mamabusi mamabusi force-pushed the appSpecificSharedCookies branch from 678ac50 to a660ad4 Compare May 25, 2017 10:12
@mamabusi
Copy link
Contributor Author

@mattrajca Thanks for your time on this. I have addressed the review comments. Please take a look.

@@ -47,7 +46,13 @@ open class HTTPCookieStorage: NSObject {
allCookies = [:]
cookieAcceptPolicy = .always
super.init()
cookieFilePath = filePath(path: _CFXDGCreateDataHomePath()._swiftObject, fileName: "/.cookies." + cookieStorageName)
let bundlePath = Bundle.main.bundlePath
var bundleName = bundlePath.components(separatedBy: "/").last!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mamabusi Are we sure this will never have a nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pushkarnk Yes, 'bundlePath' is always a non-nil String that will ensure that the value returned by 'bundlePath.components(separatedBy: "/")' can be safely unwrapped.

@pushkarnk
Copy link
Member

Looks good to me.

@pushkarnk
Copy link
Member

@swift-ci please test and merge

@swift-ci swift-ci merged commit a6a4e79 into swiftlang:master May 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants