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

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

Issue 2687873002: Use mime type for image extension (Closed)
Patch Set: Use const ref 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
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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 #include "ios/web/public/url_scheme_util.h" 178 #include "ios/web/public/url_scheme_util.h"
178 #include "ios/web/public/web_client.h" 179 #include "ios/web/public/web_client.h"
179 #import "ios/web/public/web_state/context_menu_params.h" 180 #import "ios/web/public/web_state/context_menu_params.h"
180 #import "ios/web/public/web_state/crw_web_view_proxy.h" 181 #import "ios/web/public/web_state/crw_web_view_proxy.h"
181 #import "ios/web/public/web_state/ui/crw_native_content_provider.h" 182 #import "ios/web/public/web_state/ui/crw_native_content_provider.h"
182 #include "ios/web/public/web_state/web_state.h" 183 #include "ios/web/public/web_state/web_state.h"
183 #import "ios/web/public/web_state/web_state_delegate_bridge.h" 184 #import "ios/web/public/web_state/web_state_delegate_bridge.h"
184 #include "ios/web/public/web_thread.h" 185 #include "ios/web/public/web_thread.h"
185 #import "ios/web/web_state/ui/crw_web_controller.h" 186 #import "ios/web/web_state/ui/crw_web_controller.h"
186 #import "net/base/mac/url_conversions.h" 187 #import "net/base/mac/url_conversions.h"
188 #include "net/base/mime_util.h"
187 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 189 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
188 #include "net/ssl/ssl_info.h" 190 #include "net/ssl/ssl_info.h"
189 #include "net/url_request/url_request_context_getter.h" 191 #include "net/url_request/url_request_context_getter.h"
190 #include "third_party/google_toolbox_for_mac/src/iPhone/GTMUIImage+Resize.h" 192 #include "third_party/google_toolbox_for_mac/src/iPhone/GTMUIImage+Resize.h"
191 #include "ui/base/l10n/l10n_util.h" 193 #include "ui/base/l10n/l10n_util.h"
192 #include "ui/base/l10n/l10n_util_mac.h" 194 #include "ui/base/l10n/l10n_util_mac.h"
193 #include "ui/base/page_transition_types.h" 195 #include "ui/base/page_transition_types.h"
194 #include "url/gurl.h" 196 #include "url/gurl.h"
195 197
196 using base::UserMetricsAction; 198 using base::UserMetricsAction;
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 // Dismisses previous alert if it has not been dismissed yet. 673 // Dismisses previous alert if it has not been dismissed yet.
672 - (void)showErrorAlertWithStringTitle:(NSString*)title 674 - (void)showErrorAlertWithStringTitle:(NSString*)title
673 message:(NSString*)message; 675 message:(NSString*)message;
674 // Shows a self-dismissing snackbar displaying |message|. 676 // Shows a self-dismissing snackbar displaying |message|.
675 - (void)showSnackbar:(NSString*)message; 677 - (void)showSnackbar:(NSString*)message;
676 // Induces an intentional crash in the browser process. 678 // Induces an intentional crash in the browser process.
677 - (void)induceBrowserCrash; 679 - (void)induceBrowserCrash;
678 // Returns Tab that corresponds to the given |webState|. 680 // Returns Tab that corresponds to the given |webState|.
679 - (Tab*)tabForWebState:(web::WebState*)webState; 681 - (Tab*)tabForWebState:(web::WebState*)webState;
680 // Saves the image or display error message, based on privacy settings. 682 // Saves the image or display error message, based on privacy settings.
681 - (void)managePermissionAndSaveImage:(NSData*)data; 683 - (void)managePermissionAndSaveImage:(NSData*)data
684 fileExtension:(NSString*)fileExtension;
682 // Saves the image. In order to keep the metadata of the image, the image is 685 // Saves the image. In order to keep the metadata of the image, the image is
683 // saved as a temporary file on disk then saved in photos. 686 // saved as a temporary file on disk then saved in photos.
684 // This should be called on FILE thread. 687 // This should be called on FILE thread.
685 - (void)saveImage:(NSData*)data; 688 - (void)saveImage:(NSData*)data fileExtension:(NSString*)fileExtension;
686 // Called when Chrome has been denied access to the photos or videos and the 689 // Called when Chrome has been denied access to the photos or videos and the
687 // user can change it. 690 // user can change it.
688 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's 691 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's
689 // settings. Dismiss previous alert if it has not been dismissed yet. 692 // settings. Dismiss previous alert if it has not been dismissed yet.
690 - (void)displayImageErrorAlertWithSettingsOnMainQueue; 693 - (void)displayImageErrorAlertWithSettingsOnMainQueue;
691 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss 694 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss
692 // previous alert if it has not been dismissed yet. 695 // previous alert if it has not been dismissed yet.
693 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL; 696 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL;
694 // Called when Chrome has been denied access to the photos or videos and the 697 // Called when Chrome has been denied access to the photos or videos and the
695 // user cannot change it. 698 // user cannot change it.
(...skipping 2289 matching lines...) Expand 10 before | Expand all | Expand 10 after
2985 } 2988 }
2986 } 2989 }
2987 2990
2988 #pragma mark - Context menu methods 2991 #pragma mark - Context menu methods
2989 2992
2990 - (void)searchByImageAtURL:(const GURL&)url 2993 - (void)searchByImageAtURL:(const GURL&)url
2991 referrer:(const web::Referrer)referrer { 2994 referrer:(const web::Referrer)referrer {
2992 DCHECK(url.is_valid()); 2995 DCHECK(url.is_valid());
2993 base::WeakNSObject<BrowserViewController> weakSelf(self); 2996 base::WeakNSObject<BrowserViewController> weakSelf(self);
2994 const GURL image_source_url = url; 2997 const GURL image_source_url = url;
2995 image_fetcher::IOSImageDataFetcherCallback callback = ^(NSData* data) { 2998 image_fetcher::IOSImageDataFetcherCallback callback = ^(
2999 NSData* data, const image_fetcher::RequestMetadata& metadata) {
2996 DCHECK(data); 3000 DCHECK(data);
2997 dispatch_async(dispatch_get_main_queue(), ^{ 3001 dispatch_async(dispatch_get_main_queue(), ^{
2998 [weakSelf searchByImageData:data atURL:image_source_url]; 3002 [weakSelf searchByImageData:data atURL:image_source_url];
2999 }); 3003 });
3000 }; 3004 };
3001 _imageFetcher->FetchImageDataWebpDecoded( 3005 _imageFetcher->FetchImageDataWebpDecoded(
3002 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), 3006 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer),
3003 web::PolicyForNavigation(url, referrer)); 3007 web::PolicyForNavigation(url, referrer));
3004 } 3008 }
3005 3009
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
3041 search_args, templateUrlService->search_terms_data(), &post_content)); 3045 search_args, templateUrlService->search_terms_data(), &post_content));
3042 [self addSelectedTabWithURL:result 3046 [self addSelectedTabWithURL:result
3043 postData:&post_content 3047 postData:&post_content
3044 transition:ui::PAGE_TRANSITION_TYPED]; 3048 transition:ui::PAGE_TRANSITION_TYPED];
3045 } 3049 }
3046 3050
3047 - (void)saveImageAtURL:(const GURL&)url 3051 - (void)saveImageAtURL:(const GURL&)url
3048 referrer:(const web::Referrer&)referrer { 3052 referrer:(const web::Referrer&)referrer {
3049 DCHECK(url.is_valid()); 3053 DCHECK(url.is_valid());
3050 3054
3051 image_fetcher::IOSImageDataFetcherCallback callback = ^(NSData* data) { 3055 image_fetcher::IOSImageDataFetcherCallback callback =
3052 DCHECK(data); 3056 ^(NSData* data, const image_fetcher::RequestMetadata& metadata) {
3057 DCHECK(data);
3053 3058
3054 [self managePermissionAndSaveImage:data]; 3059 base::FilePath::StringType extension;
3055 }; 3060
3061 bool extensionSuccess = net::GetPreferredExtensionForMimeType(
3062 metadata.mime_type, &extension);
3063 if (!extensionSuccess || extension.length() == 0) {
3064 extension = "png";
3065 }
3066
3067 NSString* fileExtension =
3068 [@"." stringByAppendingString:base::SysUTF8ToNSString(extension)];
3069 [self managePermissionAndSaveImage:data fileExtension:fileExtension];
3070 };
3056 _imageFetcher->FetchImageDataWebpDecoded( 3071 _imageFetcher->FetchImageDataWebpDecoded(
3057 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), 3072 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer),
3058 web::PolicyForNavigation(url, referrer)); 3073 web::PolicyForNavigation(url, referrer));
3059 } 3074 }
3060 3075
3061 - (void)managePermissionAndSaveImage:(NSData*)data { 3076 - (void)managePermissionAndSaveImage:(NSData*)data
3077 fileExtension:(NSString*)fileExtension {
marq (ping after 24h) 2017/02/09 14:31:30 withFileExtension: reads better, I think.
gambard 2017/02/09 15:10:32 Done.
3062 switch ([PHPhotoLibrary authorizationStatus]) { 3078 switch ([PHPhotoLibrary authorizationStatus]) {
3063 // User was never asked for permission to access photos. 3079 // User was never asked for permission to access photos.
3064 case PHAuthorizationStatusNotDetermined: 3080 case PHAuthorizationStatusNotDetermined:
3065 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { 3081 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
3066 // Call -saveImage again to check if chrome needs to display an error or 3082 // Call -saveImage again to check if chrome needs to display an error or
3067 // saves the image. 3083 // saves the image.
3068 if (status != PHAuthorizationStatusNotDetermined) 3084 if (status != PHAuthorizationStatusNotDetermined)
3069 [self managePermissionAndSaveImage:data]; 3085 [self managePermissionAndSaveImage:data fileExtension:fileExtension];
3070 }]; 3086 }];
3071 break; 3087 break;
3072 3088
3073 // 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
3074 // cannot grant it. 3090 // cannot grant it.
3075 case PHAuthorizationStatusRestricted: 3091 case PHAuthorizationStatusRestricted:
3076 [self displayPrivacyErrorAlertOnMainQueue: 3092 [self displayPrivacyErrorAlertOnMainQueue:
3077 l10n_util::GetNSString( 3093 l10n_util::GetNSString(
3078 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)]; 3094 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)];
3079 break; 3095 break;
3080 3096
3081 // 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
3082 // can grant it. 3098 // can grant it.
3083 case PHAuthorizationStatusDenied: 3099 case PHAuthorizationStatusDenied:
3084 [self displayImageErrorAlertWithSettingsOnMainQueue]; 3100 [self displayImageErrorAlertWithSettingsOnMainQueue];
3085 break; 3101 break;
3086 3102
3087 // The application has permission to access the photos. 3103 // The application has permission to access the photos.
3088 default: { 3104 default: {
3089 web::WebThread::PostTask(web::WebThread::FILE, FROM_HERE, 3105 web::WebThread::PostTask(
3090 base::BindBlock(^{ 3106 web::WebThread::FILE, FROM_HERE, base::BindBlock(^{
3091 [self saveImage:data]; 3107 [self saveImage:data fileExtension:fileExtension];
3092 })); 3108 }));
3093 break; 3109 break;
3094 } 3110 }
3095 } 3111 }
3096 } 3112 }
3097 3113
3098 - (void)saveImage:(NSData*)data { 3114 - (void)saveImage:(NSData*)data fileExtension:(NSString*)fileExtension {
marq (ping after 24h) 2017/02/09 14:31:30 withFileExtension:
gambard 2017/02/09 15:10:32 Done.
3099 NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString] 3115 NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString]
3100 stringByAppendingString:@".png"]; 3116 stringByAppendingString:fileExtension];
3101 NSURL* fileURL = 3117 NSURL* fileURL =
3102 [NSURL fileURLWithPath:[NSTemporaryDirectory() 3118 [NSURL fileURLWithPath:[NSTemporaryDirectory()
3103 stringByAppendingPathComponent:fileName]]; 3119 stringByAppendingPathComponent:fileName]];
3104 NSError* error = nil; 3120 NSError* error = nil;
3105 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error]; 3121 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error];
3106 3122
3107 // Error while writing the image to disk. 3123 // Error while writing the image to disk.
3108 if (error) { 3124 if (error) {
3109 NSString* errorMessage = [NSString 3125 NSString* errorMessage = [NSString
3110 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription], 3126 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription],
(...skipping 1851 matching lines...) Expand 10 before | Expand all | Expand 10 after
4962 4978
4963 - (UIView*)voiceSearchButton { 4979 - (UIView*)voiceSearchButton {
4964 return _voiceSearchButton; 4980 return _voiceSearchButton;
4965 } 4981 }
4966 4982
4967 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner { 4983 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner {
4968 return [self currentLogoAnimationControllerOwner]; 4984 return [self currentLogoAnimationControllerOwner];
4969 } 4985 }
4970 4986
4971 @end 4987 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698