[go: nahoru, domu]

[iOS] Add Manage Storage alert for Save to Photos

This CL adds a Manage Storage alert for Save to Photos which is shown to
the user when uploading an image fails because of the user being out of
storage. Also replaces AlertCoordinator with a simple UIAlertController
in SaveToPhotosCoordinator.

Fixed: 1524281
Change-Id: I5f7033d9f73823d537ff55a92236710a9621033b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5258411
Reviewed-by: Mark Cogan <marq@chromium.org>
Commit-Queue: Quentin Pubert <qpubert@google.com>
Reviewed-by: Olivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1259866}
diff --git a/ios/chrome/browser/ui/save_to_photos/save_to_photos_mediator.mm b/ios/chrome/browser/ui/save_to_photos/save_to_photos_mediator.mm
index c84533c..c332e37 100644
--- a/ios/chrome/browser/ui/save_to_photos/save_to_photos_mediator.mm
+++ b/ios/chrome/browser/ui/save_to_photos/save_to_photos_mediator.mm
@@ -10,13 +10,19 @@
 #import "base/memory/raw_ptr.h"
 #import "base/metrics/histogram_functions.h"
 #import "base/metrics/histogram_macros.h"
+#import "base/metrics/user_metrics.h"
+#import "base/metrics/user_metrics_action.h"
 #import "base/strings/sys_string_conversions.h"
 #import "components/prefs/pref_service.h"
 #import "components/signin/public/base/signin_metrics.h"
 #import "components/strings/grit/components_strings.h"
+#import "ios/chrome/browser/drive/model/manage_storage_url_util.h"
 #import "ios/chrome/browser/photos/model/photos_metrics.h"
 #import "ios/chrome/browser/photos/model/photos_service.h"
 #import "ios/chrome/browser/shared/model/prefs/pref_names.h"
+#import "ios/chrome/browser/shared/public/commands/application_commands.h"
+#import "ios/chrome/browser/shared/public/commands/manage_storage_alert_commands.h"
+#import "ios/chrome/browser/shared/public/commands/open_new_tab_command.h"
 #import "ios/chrome/browser/signin/model/authentication_service.h"
 #import "ios/chrome/browser/signin/model/chrome_account_manager_service.h"
 #import "ios/chrome/browser/signin/model/system_identity.h"
@@ -84,6 +90,8 @@
   raw_ptr<PrefService> _prefService;
   raw_ptr<ChromeAccountManagerService> _accountManagerService;
   raw_ptr<signin::IdentityManager> _identityManager;
+  id<ManageStorageAlertCommands> _manageStorageAlertHandler;
+  id<ApplicationCommands> _applicationHandler;
   NSString* _imageName;
   NSData* _imageData;
   BOOL _userTappedSuccessSnackbarButton;
@@ -96,21 +104,29 @@
 
 #pragma mark - Initialization
 
-- (instancetype)
-    initWithPhotosService:(PhotosService*)photosService
-              prefService:(PrefService*)prefService
-    accountManagerService:(ChromeAccountManagerService*)accountManagerService
-          identityManager:(signin::IdentityManager*)identityManager {
+- (instancetype)initWithPhotosService:(PhotosService*)photosService
+                          prefService:(PrefService*)prefService
+                accountManagerService:
+                    (ChromeAccountManagerService*)accountManagerService
+                      identityManager:(signin::IdentityManager*)identityManager
+            manageStorageAlertHandler:
+                (id<ManageStorageAlertCommands>)manageStorageAlertHandler
+                   applicationHandler:
+                       (id<ApplicationCommands>)applicationHandler {
   self = [super init];
   if (self) {
+    CHECK(photosService);
+    CHECK(prefService);
+    CHECK(accountManagerService);
+    CHECK(identityManager);
+    CHECK(manageStorageAlertHandler);
+    CHECK(applicationHandler);
     _photosService = photosService;
     _prefService = prefService;
     _accountManagerService = accountManagerService;
     _identityManager = identityManager;
-    CHECK(_photosService);
-    CHECK(_prefService);
-    CHECK(_accountManagerService);
-    CHECK(_identityManager);
+    _manageStorageAlertHandler = manageStorageAlertHandler;
+    _applicationHandler = applicationHandler;
   }
   return self;
 }
@@ -224,6 +240,18 @@
   _identity = nil;
 }
 
+- (void)showManageStorageForIdentity:(id<SystemIdentity>)identity {
+  base::RecordAction(
+      base::UserMetricsAction("MobileSaveToPhotosManageStorage"));
+  // The uploading identity's user email is used to switch to the uploading
+  // account before loading the "Manage Storage" web page.
+  GURL manageStorageURL = GenerateManageDriveStorageUrl(
+      base::SysNSStringToUTF8(identity.userEmail));
+  OpenNewTabCommand* newTabCommand =
+      [OpenNewTabCommand commandWithURLFromChrome:manageStorageURL];
+  [_applicationHandler openURLInNewTab:newTabCommand];
+}
+
 #pragma mark - Private
 
 // Resume the process of saving the image once the data has been fetched.
@@ -348,6 +376,14 @@
                                   result.failure_type);
     __weak __typeof(self) weakSelf = self;
     [self.delegate stopValidationSpinnerForAccountPicker];
+    // If the user is out of storage, offer to manage their storage.
+    if (result.failure_type ==
+        PhotosServiceUploadFailureType::kUploadPhoto2NotEnoughStorage) {
+      [_manageStorageAlertHandler
+          showManageStorageAlertForIdentity:failureIdentity];
+      return;
+    }
+    // Otherwise let the user "Try Again" with the same account.
     [self showTryAgainOrCancelAlertWithTryAgainBlock:^{
       [weakSelf retryUploadImageWithIdentity:failureIdentity];
     }];