Commit a1bf255b authored by Justin Cohen's avatar Justin Cohen Committed by Chromium LUCI CQ

ios: Revert to old saving method on iOS13.

This API seems to trigger a TCC violation on iOS13 but not iOS14.
Simply reverting on iOS13 for now.

Bug: 1159431
Change-Id: I7cf85099536aa9ae44345240bd1d8f1ca67f96a1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2625912
Commit-Queue: Justin Cohen <justincohen@chromium.org>
Auto-Submit: Justin Cohen <justincohen@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843147}
parent 2d7174dc
...@@ -7,8 +7,10 @@ ...@@ -7,8 +7,10 @@
#import <Photos/Photos.h> #import <Photos/Photos.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/feature_list.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/format_macros.h" #include "base/format_macros.h"
#include "base/ios/ios_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/task/thread_pool.h" #include "base/task/thread_pool.h"
...@@ -28,6 +30,15 @@ ...@@ -28,6 +30,15 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace {
// Kill switch guarding a workaround for TCC violations before iOS14. In case
// iOS14 starts triggering violations too. See crbug.com/1159431 for details.
const base::Feature kPhotoLibrarySaveImage{"PhotoLibrarySaveImage",
base::FEATURE_ENABLED_BY_DEFAULT};
} // namespace
@interface ImageSaver () @interface ImageSaver ()
// Base view controller for the alerts. // Base view controller for the alerts.
@property(nonatomic, weak) UIViewController* baseViewController; @property(nonatomic, weak) UIViewController* baseViewController;
...@@ -81,35 +92,33 @@ ...@@ -81,35 +92,33 @@
return; return;
} }
[self savePhoto:data]; if (base::FeatureList::IsEnabled(kPhotoLibrarySaveImage) &&
base::ios::IsRunningOnIOS14OrLater()) {
// Dump |data| into the photo library. Requires the usage of
// NSPhotoLibraryAddUsageDescription.
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
PHAssetResourceCreationOptions* options =
[[PHAssetResourceCreationOptions alloc] init];
[[PHAssetCreationRequest creationRequestForAsset]
addResourceWithType:PHAssetResourceTypePhoto
data:data
options:options];
}
completionHandler:^(BOOL success, NSError* error) {
[weakSelf image:savedImage
didFinishSavingWithError:error
contextInfo:nil];
}];
} else {
// Fallback for pre-iOS14.
UIImageWriteToSavedPhotosAlbum(
savedImage, weakSelf,
@selector(image:didFinishSavingWithError:contextInfo:), nullptr);
}
}); });
} }
// Dump |data| into the photo library. Requires the usage of
// NSPhotoLibraryAddUsageDescription.
- (void)savePhoto:(NSData*)data {
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
PHAssetResourceCreationOptions* options =
[[PHAssetResourceCreationOptions alloc] init];
[[PHAssetCreationRequest creationRequestForAsset]
addResourceWithType:PHAssetResourceTypePhoto
data:data
options:options];
}
completionHandler:^(BOOL success, NSError* error) {
if (error) {
// Saving photo failed, likely due to a permissions issue.
// This code may be executed outside of the main thread. Make sure to
// display the error on the main thread.
[self displayImageErrorAlertWithSettingsOnMainQueue];
} else {
// TODO(crbug.com/797277): Provide a way for the user to easily
// reach the photos app.
}
}];
}
// Called when Chrome has been denied access to add photos or videos and the // Called when Chrome has been denied access to add photos or videos and the
// user can change it. // user can change it.
// Shows a privacy alert on the main queue, allowing the user to go to Chrome's // Shows a privacy alert on the main queue, allowing the user to go to Chrome's
...@@ -185,4 +194,21 @@ ...@@ -185,4 +194,21 @@
}); });
} }
// Called after the system attempts to write the image to the saved photos
// album.
- (void)image:(UIImage*)image
didFinishSavingWithError:(NSError*)error
contextInfo:(void*)contextInfo {
// Was there an error?
if (error) {
// Saving photo failed, likely due to a permissions issue.
// This code may be execute outside of the main thread. Make sure to display
// the error on the main thread.
[self displayImageErrorAlertWithSettingsOnMainQueue];
} else {
// TODO(crbug.com/797277): Provide a way for the user to easily reach the
// photos app.
}
}
@end @end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment