Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: ios/chrome/browser/ui/browser_view_controller.mm

Issue 2687873002: Use mime type for image extension (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/chrome/browser/suggestions/image_fetcher_impl.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
11 #import <QuartzCore/QuartzCore.h> 11 #import <QuartzCore/QuartzCore.h>
12 12
13 #include <stdint.h> 13 #include <stdint.h>
14 #include <cmath> 14 #include <cmath>
15 #include <memory> 15 #include <memory>
16 16
17 #include "base/base64.h" 17 #include "base/base64.h"
18 #include "base/command_line.h" 18 #include "base/command_line.h"
19 #include "base/files/file_path.h"
19 #include "base/format_macros.h" 20 #include "base/format_macros.h"
20 #include "base/i18n/rtl.h" 21 #include "base/i18n/rtl.h"
21 #include "base/ios/block_types.h" 22 #include "base/ios/block_types.h"
22 #include "base/ios/ios_util.h" 23 #include "base/ios/ios_util.h"
23 #include "base/ios/weak_nsobject.h" 24 #include "base/ios/weak_nsobject.h"
24 #include "base/logging.h" 25 #include "base/logging.h"
25 #include "base/mac/bind_objc_block.h" 26 #include "base/mac/bind_objc_block.h"
26 #include "base/mac/bundle_locations.h" 27 #include "base/mac/bundle_locations.h"
27 #include "base/mac/foundation_util.h" 28 #include "base/mac/foundation_util.h"
28 #include "base/mac/objc_property_releaser.h" 29 #include "base/mac/objc_property_releaser.h"
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 #include "ios/web/public/url_scheme_util.h" 179 #include "ios/web/public/url_scheme_util.h"
179 #include "ios/web/public/web_client.h" 180 #include "ios/web/public/web_client.h"
180 #import "ios/web/public/web_state/context_menu_params.h" 181 #import "ios/web/public/web_state/context_menu_params.h"
181 #import "ios/web/public/web_state/crw_web_view_proxy.h" 182 #import "ios/web/public/web_state/crw_web_view_proxy.h"
182 #import "ios/web/public/web_state/ui/crw_native_content_provider.h" 183 #import "ios/web/public/web_state/ui/crw_native_content_provider.h"
183 #include "ios/web/public/web_state/web_state.h" 184 #include "ios/web/public/web_state/web_state.h"
184 #import "ios/web/public/web_state/web_state_delegate_bridge.h" 185 #import "ios/web/public/web_state/web_state_delegate_bridge.h"
185 #include "ios/web/public/web_thread.h" 186 #include "ios/web/public/web_thread.h"
186 #import "ios/web/web_state/ui/crw_web_controller.h" 187 #import "ios/web/web_state/ui/crw_web_controller.h"
187 #import "net/base/mac/url_conversions.h" 188 #import "net/base/mac/url_conversions.h"
189 #include "net/base/mime_util.h"
188 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 190 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
189 #include "net/ssl/ssl_info.h" 191 #include "net/ssl/ssl_info.h"
190 #include "net/url_request/url_request_context_getter.h" 192 #include "net/url_request/url_request_context_getter.h"
191 #include "third_party/google_toolbox_for_mac/src/iPhone/GTMUIImage+Resize.h" 193 #include "third_party/google_toolbox_for_mac/src/iPhone/GTMUIImage+Resize.h"
192 #include "ui/base/l10n/l10n_util.h" 194 #include "ui/base/l10n/l10n_util.h"
193 #include "ui/base/l10n/l10n_util_mac.h" 195 #include "ui/base/l10n/l10n_util_mac.h"
194 #include "ui/base/page_transition_types.h" 196 #include "ui/base/page_transition_types.h"
195 #include "url/gurl.h" 197 #include "url/gurl.h"
196 198
197 using base::UserMetricsAction; 199 using base::UserMetricsAction;
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 - (void)showErrorAlert:(int)titleMessageId message:(int)messageId; 672 - (void)showErrorAlert:(int)titleMessageId message:(int)messageId;
671 // Helper method displaying an alert with the given title and message. 673 // Helper method displaying an alert with the given title and message.
672 // Dismisses previous alert if it has not been dismissed yet. 674 // Dismisses previous alert if it has not been dismissed yet.
673 - (void)showErrorAlertWithStringTitle:(NSString*)title 675 - (void)showErrorAlertWithStringTitle:(NSString*)title
674 message:(NSString*)message; 676 message:(NSString*)message;
675 // Shows a self-dismissing snackbar displaying |message|. 677 // Shows a self-dismissing snackbar displaying |message|.
676 - (void)showSnackbar:(NSString*)message; 678 - (void)showSnackbar:(NSString*)message;
677 // Induces an intentional crash in the browser process. 679 // Induces an intentional crash in the browser process.
678 - (void)induceBrowserCrash; 680 - (void)induceBrowserCrash;
679 // Saves the image or display error message, based on privacy settings. 681 // Saves the image or display error message, based on privacy settings.
680 - (void)managePermissionAndSaveImage:(NSData*)data; 682 - (void)managePermissionAndSaveImage:(NSData*)data
683 withFileExtension:(NSString*)fileExtension;
681 // Saves the image. In order to keep the metadata of the image, the image is 684 // Saves the image. In order to keep the metadata of the image, the image is
682 // saved as a temporary file on disk then saved in photos. 685 // saved as a temporary file on disk then saved in photos.
683 // This should be called on FILE thread. 686 // This should be called on FILE thread.
684 - (void)saveImage:(NSData*)data; 687 - (void)saveImage:(NSData*)data withFileExtension:(NSString*)fileExtension;
685 // Called when Chrome has been denied access to the photos or videos and the 688 // Called when Chrome has been denied access to the photos or videos and the
686 // user can change it. 689 // user can change it.
687 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's 690 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's
688 // settings. Dismiss previous alert if it has not been dismissed yet. 691 // settings. Dismiss previous alert if it has not been dismissed yet.
689 - (void)displayImageErrorAlertWithSettingsOnMainQueue; 692 - (void)displayImageErrorAlertWithSettingsOnMainQueue;
690 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss 693 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss
691 // previous alert if it has not been dismissed yet. 694 // previous alert if it has not been dismissed yet.
692 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL; 695 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL;
693 // 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
694 // user cannot change it. 697 // user cannot change it.
(...skipping 2289 matching lines...) Expand 10 before | Expand all | Expand 10 after
2984 } 2987 }
2985 } 2988 }
2986 2989
2987 #pragma mark - Context menu methods 2990 #pragma mark - Context menu methods
2988 2991
2989 - (void)searchByImageAtURL:(const GURL&)url 2992 - (void)searchByImageAtURL:(const GURL&)url
2990 referrer:(const web::Referrer)referrer { 2993 referrer:(const web::Referrer)referrer {
2991 DCHECK(url.is_valid()); 2994 DCHECK(url.is_valid());
2992 base::WeakNSObject<BrowserViewController> weakSelf(self); 2995 base::WeakNSObject<BrowserViewController> weakSelf(self);
2993 const GURL image_source_url = url; 2996 const GURL image_source_url = url;
2994 image_fetcher::IOSImageDataFetcherCallback callback = ^(NSData* data) { 2997 image_fetcher::IOSImageDataFetcherCallback callback = ^(
2998 NSData* data, const image_fetcher::RequestMetadata& metadata) {
2995 DCHECK(data); 2999 DCHECK(data);
2996 dispatch_async(dispatch_get_main_queue(), ^{ 3000 dispatch_async(dispatch_get_main_queue(), ^{
2997 [weakSelf searchByImageData:data atURL:image_source_url]; 3001 [weakSelf searchByImageData:data atURL:image_source_url];
2998 }); 3002 });
2999 }; 3003 };
3000 _imageFetcher->FetchImageDataWebpDecoded( 3004 _imageFetcher->FetchImageDataWebpDecoded(
3001 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), 3005 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer),
3002 web::PolicyForNavigation(url, referrer)); 3006 web::PolicyForNavigation(url, referrer));
3003 } 3007 }
3004 3008
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
3040 search_args, templateUrlService->search_terms_data(), &post_content)); 3044 search_args, templateUrlService->search_terms_data(), &post_content));
3041 [self addSelectedTabWithURL:result 3045 [self addSelectedTabWithURL:result
3042 postData:&post_content 3046 postData:&post_content
3043 transition:ui::PAGE_TRANSITION_TYPED]; 3047 transition:ui::PAGE_TRANSITION_TYPED];
3044 } 3048 }
3045 3049
3046 - (void)saveImageAtURL:(const GURL&)url 3050 - (void)saveImageAtURL:(const GURL&)url
3047 referrer:(const web::Referrer&)referrer { 3051 referrer:(const web::Referrer&)referrer {
3048 DCHECK(url.is_valid()); 3052 DCHECK(url.is_valid());
3049 3053
3050 image_fetcher::IOSImageDataFetcherCallback callback = ^(NSData* data) { 3054 image_fetcher::IOSImageDataFetcherCallback callback = ^(
3055 NSData* data, const image_fetcher::RequestMetadata& metadata) {
3051 DCHECK(data); 3056 DCHECK(data);
3052 3057
3053 [self managePermissionAndSaveImage:data]; 3058 base::FilePath::StringType extension;
3059
3060 bool extensionSuccess =
3061 net::GetPreferredExtensionForMimeType(metadata.mime_type, &extension);
3062 if (!extensionSuccess || extension.length() == 0) {
3063 extension = "png";
3064 }
3065
3066 NSString* fileExtension =
3067 [@"." stringByAppendingString:base::SysUTF8ToNSString(extension)];
3068 [self managePermissionAndSaveImage:data withFileExtension:fileExtension];
3054 }; 3069 };
3055 _imageFetcher->FetchImageDataWebpDecoded( 3070 _imageFetcher->FetchImageDataWebpDecoded(
3056 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), 3071 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer),
3057 web::PolicyForNavigation(url, referrer)); 3072 web::PolicyForNavigation(url, referrer));
3058 } 3073 }
3059 3074
3060 - (void)managePermissionAndSaveImage:(NSData*)data { 3075 - (void)managePermissionAndSaveImage:(NSData*)data
3076 withFileExtension:(NSString*)fileExtension {
3061 switch ([PHPhotoLibrary authorizationStatus]) { 3077 switch ([PHPhotoLibrary authorizationStatus]) {
3062 // User was never asked for permission to access photos. 3078 // User was never asked for permission to access photos.
3063 case PHAuthorizationStatusNotDetermined: 3079 case PHAuthorizationStatusNotDetermined:
3064 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { 3080 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
3065 // Call -saveImage again to check if chrome needs to display an error or 3081 // Call -saveImage again to check if chrome needs to display an error or
3066 // saves the image. 3082 // saves the image.
3067 if (status != PHAuthorizationStatusNotDetermined) 3083 if (status != PHAuthorizationStatusNotDetermined)
3068 [self managePermissionAndSaveImage:data]; 3084 [self managePermissionAndSaveImage:data
3085 withFileExtension:fileExtension];
3069 }]; 3086 }];
3070 break; 3087 break;
3071 3088
3072 // The application doesn't have permission to access photo and the user 3089 // The application doesn't have permission to access photo and the user
3073 // cannot grant it. 3090 // cannot grant it.
3074 case PHAuthorizationStatusRestricted: 3091 case PHAuthorizationStatusRestricted:
3075 [self displayPrivacyErrorAlertOnMainQueue: 3092 [self displayPrivacyErrorAlertOnMainQueue:
3076 l10n_util::GetNSString( 3093 l10n_util::GetNSString(
3077 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)]; 3094 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)];
3078 break; 3095 break;
3079 3096
3080 // The application doesn't have permission to access photo and the user 3097 // The application doesn't have permission to access photo and the user
3081 // can grant it. 3098 // can grant it.
3082 case PHAuthorizationStatusDenied: 3099 case PHAuthorizationStatusDenied:
3083 [self displayImageErrorAlertWithSettingsOnMainQueue]; 3100 [self displayImageErrorAlertWithSettingsOnMainQueue];
3084 break; 3101 break;
3085 3102
3086 // The application has permission to access the photos. 3103 // The application has permission to access the photos.
3087 default: { 3104 default: {
3088 web::WebThread::PostTask(web::WebThread::FILE, FROM_HERE, 3105 web::WebThread::PostTask(
3089 base::BindBlock(^{ 3106 web::WebThread::FILE, FROM_HERE, base::BindBlock(^{
3090 [self saveImage:data]; 3107 [self saveImage:data withFileExtension:fileExtension];
3091 })); 3108 }));
3092 break; 3109 break;
3093 } 3110 }
3094 } 3111 }
3095 } 3112 }
3096 3113
3097 - (void)saveImage:(NSData*)data { 3114 - (void)saveImage:(NSData*)data withFileExtension:(NSString*)fileExtension {
3098 NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString] 3115 NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString]
3099 stringByAppendingString:@".png"]; 3116 stringByAppendingString:fileExtension];
Olivier 2017/02/10 08:54:43 nit, and not important for this CL: Whenever manip
3100 NSURL* fileURL = 3117 NSURL* fileURL =
3101 [NSURL fileURLWithPath:[NSTemporaryDirectory() 3118 [NSURL fileURLWithPath:[NSTemporaryDirectory()
3102 stringByAppendingPathComponent:fileName]]; 3119 stringByAppendingPathComponent:fileName]];
3103 NSError* error = nil; 3120 NSError* error = nil;
3104 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error]; 3121 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error];
3105 3122
3106 // Error while writing the image to disk. 3123 // Error while writing the image to disk.
3107 if (error) { 3124 if (error) {
3108 NSString* errorMessage = [NSString 3125 NSString* errorMessage = [NSString
3109 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription], 3126 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription],
(...skipping 1843 matching lines...) Expand 10 before | Expand all | Expand 10 after
4953 4970
4954 - (UIView*)voiceSearchButton { 4971 - (UIView*)voiceSearchButton {
4955 return _voiceSearchButton; 4972 return _voiceSearchButton;
4956 } 4973 }
4957 4974
4958 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner { 4975 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner {
4959 return [self currentLogoAnimationControllerOwner]; 4976 return [self currentLogoAnimationControllerOwner];
4960 } 4977 }
4961 4978
4962 @end 4979 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/suggestions/image_fetcher_impl.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698