Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/browser_view_controller.h" | 5 #import "ios/chrome/browser/ui/browser_view_controller.h" |
| 6 | 6 |
| 7 #import <AssetsLibrary/AssetsLibrary.h> | 7 #import <AssetsLibrary/AssetsLibrary.h> |
| 8 #import <MobileCoreServices/MobileCoreServices.h> | 8 #import <MobileCoreServices/MobileCoreServices.h> |
| 9 #import <PassKit/PassKit.h> | 9 #import <PassKit/PassKit.h> |
| 10 #import <Photos/Photos.h> | 10 #import <Photos/Photos.h> |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 #include "base/mac/bundle_locations.h" | 26 #include "base/mac/bundle_locations.h" |
| 27 #include "base/mac/foundation_util.h" | 27 #include "base/mac/foundation_util.h" |
| 28 #include "base/mac/objc_property_releaser.h" | 28 #include "base/mac/objc_property_releaser.h" |
| 29 #import "base/mac/scoped_block.h" | 29 #import "base/mac/scoped_block.h" |
| 30 #import "base/mac/scoped_nsobject.h" | 30 #import "base/mac/scoped_nsobject.h" |
| 31 #include "base/macros.h" | 31 #include "base/macros.h" |
| 32 #include "base/memory/ptr_util.h" | 32 #include "base/memory/ptr_util.h" |
| 33 #include "base/metrics/histogram.h" | 33 #include "base/metrics/histogram.h" |
| 34 #include "base/metrics/user_metrics.h" | 34 #include "base/metrics/user_metrics.h" |
| 35 #include "base/metrics/user_metrics_action.h" | 35 #include "base/metrics/user_metrics_action.h" |
| 36 #include "base/strings/string_piece.h" | |
| 37 #include "base/strings/string_util.h" | |
| 38 #include "base/strings/sys_string_conversions.h" | 36 #include "base/strings/sys_string_conversions.h" |
| 39 #include "components/bookmarks/browser/base_bookmark_model_observer.h" | 37 #include "components/bookmarks/browser/base_bookmark_model_observer.h" |
| 40 #include "components/bookmarks/browser/bookmark_model.h" | 38 #include "components/bookmarks/browser/bookmark_model.h" |
| 41 #include "components/infobars/core/infobar_manager.h" | 39 #include "components/infobars/core/infobar_manager.h" |
| 42 #include "components/prefs/pref_service.h" | 40 #include "components/prefs/pref_service.h" |
| 43 #include "components/reading_list/core/reading_list_switches.h" | 41 #include "components/reading_list/core/reading_list_switches.h" |
| 44 #include "components/reading_list/ios/reading_list_model.h" | 42 #include "components/reading_list/ios/reading_list_model.h" |
| 45 #include "components/search_engines/search_engines_pref_names.h" | 43 #include "components/search_engines/search_engines_pref_names.h" |
| 46 #include "components/search_engines/template_url_service.h" | 44 #include "components/search_engines/template_url_service.h" |
| 47 #include "components/sessions/core/tab_restore_service_helper.h" | 45 #include "components/sessions/core/tab_restore_service_helper.h" |
| (...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 683 - (void)showErrorAlert:(int)titleMessageId message:(int)messageId; | 681 - (void)showErrorAlert:(int)titleMessageId message:(int)messageId; |
| 684 // Helper method displaying an alert with the given title and message. | 682 // Helper method displaying an alert with the given title and message. |
| 685 // Dismisses previous alert if it has not been dismissed yet. | 683 // Dismisses previous alert if it has not been dismissed yet. |
| 686 - (void)showErrorAlertWithStringTitle:(NSString*)title | 684 - (void)showErrorAlertWithStringTitle:(NSString*)title |
| 687 message:(NSString*)message; | 685 message:(NSString*)message; |
| 688 // Shows a self-dismissing snackbar displaying |message|. | 686 // Shows a self-dismissing snackbar displaying |message|. |
| 689 - (void)showSnackbar:(NSString*)message; | 687 - (void)showSnackbar:(NSString*)message; |
| 690 // Induces an intentional crash in the browser process. | 688 // Induces an intentional crash in the browser process. |
| 691 - (void)induceBrowserCrash; | 689 - (void)induceBrowserCrash; |
| 692 // Saves the image or display error message, based on privacy settings. | 690 // Saves the image or display error message, based on privacy settings. |
| 693 - (void)managePermissionAndSaveImage:(NSData*)data fileName:(NSString*)fileName; | 691 - (void)managePermissionAndSaveImage:(NSData*)data; |
| 694 // Saves the image. In order to keep the metadata of the image, the image is | 692 // Saves the image. In order to keep the metadata of the image, the image is |
| 695 // saved as a temporary file on disk then saved in photos. | 693 // saved as a temporary file on disk then saved in photos. |
| 696 // This should be called on FILE thread. | 694 // This should be called on FILE thread. |
| 697 - (void)saveImage:(NSData*)data fileName:(NSString*)fileName; | 695 - (void)saveImage:(NSData*)data; |
| 698 // Called when Chrome has been denied access to the photos or videos and the | 696 // Called when Chrome has been denied access to the photos or videos and the |
| 699 // user can change it. | 697 // user can change it. |
| 700 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's | 698 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's |
| 701 // settings. Dismiss previous alert if it has not been dismissed yet. | 699 // settings. Dismiss previous alert if it has not been dismissed yet. |
| 702 - (void)displayImageErrorAlertWithSettingsOnMainQueue; | 700 - (void)displayImageErrorAlertWithSettingsOnMainQueue; |
| 703 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss | 701 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss |
| 704 // previous alert if it has not been dismissed yet. | 702 // previous alert if it has not been dismissed yet. |
| 705 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL; | 703 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL; |
| 706 // Called when Chrome has been denied access to the photos or videos and the | 704 // Called when Chrome has been denied access to the photos or videos and the |
| 707 // user cannot change it. | 705 // user cannot change it. |
| (...skipping 2330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3038 search_args, templateUrlService->search_terms_data(), &post_content)); | 3036 search_args, templateUrlService->search_terms_data(), &post_content)); |
| 3039 [self addSelectedTabWithURL:result | 3037 [self addSelectedTabWithURL:result |
| 3040 postData:&post_content | 3038 postData:&post_content |
| 3041 transition:ui::PAGE_TRANSITION_TYPED]; | 3039 transition:ui::PAGE_TRANSITION_TYPED]; |
| 3042 } | 3040 } |
| 3043 | 3041 |
| 3044 - (void)saveImageAtURL:(const GURL&)url | 3042 - (void)saveImageAtURL:(const GURL&)url |
| 3045 referrer:(const web::Referrer&)referrer { | 3043 referrer:(const web::Referrer&)referrer { |
| 3046 DCHECK(url.is_valid()); | 3044 DCHECK(url.is_valid()); |
| 3047 | 3045 |
| 3048 NSString* fileName = [NSString | |
| 3049 stringWithFormat:@"%@.png", | |
| 3050 [[NSProcessInfo processInfo] globallyUniqueString]]; | |
| 3051 | |
| 3052 if (url.SchemeIs(base::StringPiece("data"))) { | |
| 3053 if (!base::StartsWith(url.GetContent(), "image", | |
| 3054 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 3055 // Not an image, return. | |
| 3056 return; | |
| 3057 } | |
| 3058 | |
| 3059 // Find the type of the image. It should be positionned like here: | |
| 3060 // "image/png;base...", between the '/' and the ';'. | |
| 3061 size_t semiColonPosition = url.GetContent().find(std::string(";")); | |
| 3062 size_t slashPosition = url.GetContent().find(std::string("/")); | |
| 3063 size_t substrLength = semiColonPosition - slashPosition - 1; | |
| 3064 if (substrLength > 1) { | |
| 3065 // Make sure there is a type. | |
| 3066 fileName = [NSString | |
| 3067 stringWithFormat:@"image.%@", | |
| 3068 base::SysUTF8ToNSString(url.GetContent().substr( | |
| 3069 slashPosition + 1, substrLength))]; | |
| 3070 } | |
| 3071 } else { | |
| 3072 fileName = base::SysUTF8ToNSString(url.ExtractFileName()); | |
| 3073 } | |
| 3074 | |
| 3075 web::ImageFetchedCallback callback = | 3046 web::ImageFetchedCallback callback = |
| 3076 ^(const GURL& original_url, int response_code, NSData* data) { | 3047 ^(const GURL& original_url, int response_code, NSData* data) { |
| 3077 DCHECK(data); | 3048 DCHECK(data); |
| 3078 | 3049 |
| 3079 [self managePermissionAndSaveImage:data fileName:fileName]; | 3050 [self managePermissionAndSaveImage:data]; |
| 3080 }; | 3051 }; |
| 3081 _imageFetcher->StartDownload( | 3052 _imageFetcher->StartDownload( |
| 3082 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), | 3053 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), |
| 3083 web::PolicyForNavigation(url, referrer)); | 3054 web::PolicyForNavigation(url, referrer)); |
| 3084 } | 3055 } |
| 3085 | 3056 |
| 3086 - (void)managePermissionAndSaveImage:(NSData*)data | 3057 - (void)managePermissionAndSaveImage:(NSData*)data { |
| 3087 fileName:(NSString*)fileName { | |
| 3088 switch ([PHPhotoLibrary authorizationStatus]) { | 3058 switch ([PHPhotoLibrary authorizationStatus]) { |
| 3089 // User was never asked for permission to access photos. | 3059 // User was never asked for permission to access photos. |
| 3090 case PHAuthorizationStatusNotDetermined: | 3060 case PHAuthorizationStatusNotDetermined: |
| 3091 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { | 3061 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { |
| 3092 // Call -saveImage again to check if chrome needs to display an error or | 3062 // Call -saveImage again to check if chrome needs to display an error or |
| 3093 // saves the image. | 3063 // saves the image. |
| 3094 if (status != PHAuthorizationStatusNotDetermined) | 3064 if (status != PHAuthorizationStatusNotDetermined) |
| 3095 [self managePermissionAndSaveImage:data fileName:fileName]; | 3065 [self managePermissionAndSaveImage:data]; |
| 3096 }]; | 3066 }]; |
| 3097 break; | 3067 break; |
| 3098 | 3068 |
| 3099 // The application doesn't have permission to access photo and the user | 3069 // The application doesn't have permission to access photo and the user |
| 3100 // cannot grant it. | 3070 // cannot grant it. |
| 3101 case PHAuthorizationStatusRestricted: | 3071 case PHAuthorizationStatusRestricted: |
| 3102 [self displayPrivacyErrorAlertOnMainQueue: | 3072 [self displayPrivacyErrorAlertOnMainQueue: |
| 3103 l10n_util::GetNSString( | 3073 l10n_util::GetNSString( |
| 3104 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)]; | 3074 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)]; |
| 3105 break; | 3075 break; |
| 3106 | 3076 |
| 3107 // The application doesn't have permission to access photo and the user | 3077 // The application doesn't have permission to access photo and the user |
| 3108 // can grant it. | 3078 // can grant it. |
| 3109 case PHAuthorizationStatusDenied: | 3079 case PHAuthorizationStatusDenied: |
| 3110 [self displayImageErrorAlertWithSettingsOnMainQueue]; | 3080 [self displayImageErrorAlertWithSettingsOnMainQueue]; |
| 3111 break; | 3081 break; |
| 3112 | 3082 |
| 3113 // The application has permission to access the photos. | 3083 // The application has permission to access the photos. |
| 3114 default: { | 3084 default: { |
| 3115 web::WebThread::PostTask(web::WebThread::FILE, FROM_HERE, | 3085 web::WebThread::PostTask(web::WebThread::FILE, FROM_HERE, |
| 3116 base::BindBlock(^{ | 3086 base::BindBlock(^{ |
| 3117 [self saveImage:data fileName:fileName]; | 3087 [self saveImage:data]; |
| 3118 })); | 3088 })); |
| 3119 break; | 3089 break; |
| 3120 } | 3090 } |
| 3121 } | 3091 } |
| 3122 } | 3092 } |
| 3123 | 3093 |
| 3124 - (void)saveImage:(NSData*)data fileName:(NSString*)fileName { | 3094 - (void)saveImage:(NSData*)data { |
| 3095 NSString* fileName = [NSString | |
|
marq (ping after 24h)
2016/12/21 08:47:00
nit: If you're just concatenating strings you can
gambard
2016/12/21 13:02:11
Done.
| |
| 3096 stringWithFormat:@"%@%@", | |
| 3097 [[NSProcessInfo processInfo] globallyUniqueString], | |
| 3098 @".png"]; | |
| 3125 NSURL* fileURL = | 3099 NSURL* fileURL = |
| 3126 [NSURL fileURLWithPath:[NSTemporaryDirectory() | 3100 [NSURL fileURLWithPath:[NSTemporaryDirectory() |
| 3127 stringByAppendingPathComponent:fileName]]; | 3101 stringByAppendingPathComponent:fileName]]; |
| 3128 NSError* error = nil; | 3102 NSError* error = nil; |
| 3129 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error]; | 3103 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error]; |
| 3130 | 3104 |
| 3131 // Error while writing the image to disk. | 3105 // Error while writing the image to disk. |
| 3132 if (error) { | 3106 if (error) { |
| 3133 NSString* errorMessage = [NSString | 3107 NSString* errorMessage = [NSString |
| 3134 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription], | 3108 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription], |
| (...skipping 1979 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5114 | 5088 |
| 5115 - (UIView*)voiceSearchButton { | 5089 - (UIView*)voiceSearchButton { |
| 5116 return _voiceSearchButton; | 5090 return _voiceSearchButton; |
| 5117 } | 5091 } |
| 5118 | 5092 |
| 5119 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner { | 5093 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner { |
| 5120 return [self currentLogoAnimationControllerOwner]; | 5094 return [self currentLogoAnimationControllerOwner]; |
| 5121 } | 5095 } |
| 5122 | 5096 |
| 5123 @end | 5097 @end |
| OLD | NEW |