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

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

Issue 2686023003: Use mime type for image extension (Closed)
Patch Set: 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 | « no previous file | ios/web/public/image_fetcher/image_data_fetcher.h » ('j') | 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 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 #include "ios/web/public/url_scheme_util.h" 176 #include "ios/web/public/url_scheme_util.h"
176 #include "ios/web/public/web_client.h" 177 #include "ios/web/public/web_client.h"
177 #import "ios/web/public/web_state/context_menu_params.h" 178 #import "ios/web/public/web_state/context_menu_params.h"
178 #import "ios/web/public/web_state/crw_web_view_proxy.h" 179 #import "ios/web/public/web_state/crw_web_view_proxy.h"
179 #import "ios/web/public/web_state/ui/crw_native_content_provider.h" 180 #import "ios/web/public/web_state/ui/crw_native_content_provider.h"
180 #include "ios/web/public/web_state/web_state.h" 181 #include "ios/web/public/web_state/web_state.h"
181 #import "ios/web/public/web_state/web_state_delegate_bridge.h" 182 #import "ios/web/public/web_state/web_state_delegate_bridge.h"
182 #include "ios/web/public/web_thread.h" 183 #include "ios/web/public/web_thread.h"
183 #import "ios/web/web_state/ui/crw_web_controller.h" 184 #import "ios/web/web_state/ui/crw_web_controller.h"
184 #import "net/base/mac/url_conversions.h" 185 #import "net/base/mac/url_conversions.h"
186 #include "net/base/mime_util.h"
185 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 187 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
186 #include "net/ssl/ssl_info.h" 188 #include "net/ssl/ssl_info.h"
187 #include "net/url_request/url_request_context_getter.h" 189 #include "net/url_request/url_request_context_getter.h"
188 #include "third_party/google_toolbox_for_mac/src/iPhone/GTMUIImage+Resize.h" 190 #include "third_party/google_toolbox_for_mac/src/iPhone/GTMUIImage+Resize.h"
189 #include "ui/base/l10n/l10n_util.h" 191 #include "ui/base/l10n/l10n_util.h"
190 #include "ui/base/l10n/l10n_util_mac.h" 192 #include "ui/base/l10n/l10n_util_mac.h"
191 #include "ui/base/page_transition_types.h" 193 #include "ui/base/page_transition_types.h"
192 #include "url/gurl.h" 194 #include "url/gurl.h"
193 195
194 using base::UserMetricsAction; 196 using base::UserMetricsAction;
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 // Dismisses previous alert if it has not been dismissed yet. 683 // Dismisses previous alert if it has not been dismissed yet.
682 - (void)showErrorAlertWithStringTitle:(NSString*)title 684 - (void)showErrorAlertWithStringTitle:(NSString*)title
683 message:(NSString*)message; 685 message:(NSString*)message;
684 // Shows a self-dismissing snackbar displaying |message|. 686 // Shows a self-dismissing snackbar displaying |message|.
685 - (void)showSnackbar:(NSString*)message; 687 - (void)showSnackbar:(NSString*)message;
686 // Induces an intentional crash in the browser process. 688 // Induces an intentional crash in the browser process.
687 - (void)induceBrowserCrash; 689 - (void)induceBrowserCrash;
688 // Returns Tab that corresponds to the given |webState|. 690 // Returns Tab that corresponds to the given |webState|.
689 - (Tab*)tabForWebState:(web::WebState*)webState; 691 - (Tab*)tabForWebState:(web::WebState*)webState;
690 // Saves the image or display error message, based on privacy settings. 692 // Saves the image or display error message, based on privacy settings.
691 - (void)managePermissionAndSaveImage:(NSData*)data; 693 - (void)managePermissionAndSaveImage:(NSData*)data
694 withFileExtension:(NSString*)fileExtension;
692 // Saves the image. In order to keep the metadata of the image, the image is 695 // Saves the image. In order to keep the metadata of the image, the image is
693 // saved as a temporary file on disk then saved in photos. 696 // saved as a temporary file on disk then saved in photos.
694 // This should be called on FILE thread. 697 // This should be called on FILE thread.
695 - (void)saveImage:(NSData*)data; 698 - (void)saveImage:(NSData*)data withFileExtension:(NSString*)fileExtension;
696 // Called when Chrome has been denied access to the photos or videos and the 699 // Called when Chrome has been denied access to the photos or videos and the
697 // user can change it. 700 // user can change it.
698 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's 701 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's
699 // settings. Dismiss previous alert if it has not been dismissed yet. 702 // settings. Dismiss previous alert if it has not been dismissed yet.
700 - (void)displayImageErrorAlertWithSettingsOnMainQueue; 703 - (void)displayImageErrorAlertWithSettingsOnMainQueue;
701 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss 704 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss
702 // previous alert if it has not been dismissed yet. 705 // previous alert if it has not been dismissed yet.
703 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL; 706 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL;
704 // Called when Chrome has been denied access to the photos or videos and the 707 // Called when Chrome has been denied access to the photos or videos and the
705 // user cannot change it. 708 // user cannot change it.
(...skipping 2387 matching lines...) Expand 10 before | Expand all | Expand 10 after
3093 search_args, templateUrlService->search_terms_data(), &post_content)); 3096 search_args, templateUrlService->search_terms_data(), &post_content));
3094 [self addSelectedTabWithURL:result 3097 [self addSelectedTabWithURL:result
3095 postData:&post_content 3098 postData:&post_content
3096 transition:ui::PAGE_TRANSITION_TYPED]; 3099 transition:ui::PAGE_TRANSITION_TYPED];
3097 } 3100 }
3098 3101
3099 - (void)saveImageAtURL:(const GURL&)url 3102 - (void)saveImageAtURL:(const GURL&)url
3100 referrer:(const web::Referrer&)referrer { 3103 referrer:(const web::Referrer&)referrer {
3101 DCHECK(url.is_valid()); 3104 DCHECK(url.is_valid());
3102 3105
3103 web::ImageFetchedCallback callback = 3106 web::ImageFetchedCallbackWithMime callback = ^(
3104 ^(const GURL& original_url, int response_code, NSData* data) { 3107 const GURL& original_url, int response_code, const std::string& mime_type,
3105 DCHECK(data); 3108 NSData* data) {
3109 DCHECK(data);
3106 3110
3107 [self managePermissionAndSaveImage:data]; 3111 base::FilePath::StringType extension;
3108 }; 3112
3109 _imageFetcher->StartDownload( 3113 bool extensionSuccess =
3114 net::GetPreferredExtensionForMimeType(mime_type, &extension);
3115 if (!extensionSuccess || extension.length() == 0) {
3116 extension = "png";
3117 }
3118 NSString* fileExtension =
3119 [@"." stringByAppendingString:base::SysUTF8ToNSString(extension)];
3120 [self managePermissionAndSaveImage:data withFileExtension:fileExtension];
3121 };
3122 _imageFetcher->StartDownloadWithMime(
3110 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), 3123 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer),
3111 web::PolicyForNavigation(url, referrer)); 3124 web::PolicyForNavigation(url, referrer));
3112 } 3125 }
3113 3126
3114 - (void)managePermissionAndSaveImage:(NSData*)data { 3127 - (void)managePermissionAndSaveImage:(NSData*)data
3128 withFileExtension:(NSString*)fileExtension {
3115 switch ([PHPhotoLibrary authorizationStatus]) { 3129 switch ([PHPhotoLibrary authorizationStatus]) {
3116 // User was never asked for permission to access photos. 3130 // User was never asked for permission to access photos.
3117 case PHAuthorizationStatusNotDetermined: 3131 case PHAuthorizationStatusNotDetermined:
3118 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { 3132 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
3119 // Call -saveImage again to check if chrome needs to display an error or 3133 // Call -saveImage again to check if chrome needs to display an error or
3120 // saves the image. 3134 // saves the image.
3121 if (status != PHAuthorizationStatusNotDetermined) 3135 if (status != PHAuthorizationStatusNotDetermined)
3122 [self managePermissionAndSaveImage:data]; 3136 [self managePermissionAndSaveImage:data
3137 withFileExtension:fileExtension];
3123 }]; 3138 }];
3124 break; 3139 break;
3125 3140
3126 // The application doesn't have permission to access photo and the user 3141 // The application doesn't have permission to access photo and the user
3127 // cannot grant it. 3142 // cannot grant it.
3128 case PHAuthorizationStatusRestricted: 3143 case PHAuthorizationStatusRestricted:
3129 [self displayPrivacyErrorAlertOnMainQueue: 3144 [self displayPrivacyErrorAlertOnMainQueue:
3130 l10n_util::GetNSString( 3145 l10n_util::GetNSString(
3131 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)]; 3146 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)];
3132 break; 3147 break;
3133 3148
3134 // The application doesn't have permission to access photo and the user 3149 // The application doesn't have permission to access photo and the user
3135 // can grant it. 3150 // can grant it.
3136 case PHAuthorizationStatusDenied: 3151 case PHAuthorizationStatusDenied:
3137 [self displayImageErrorAlertWithSettingsOnMainQueue]; 3152 [self displayImageErrorAlertWithSettingsOnMainQueue];
3138 break; 3153 break;
3139 3154
3140 // The application has permission to access the photos. 3155 // The application has permission to access the photos.
3141 default: { 3156 default: {
3142 web::WebThread::PostTask(web::WebThread::FILE, FROM_HERE, 3157 web::WebThread::PostTask(
3143 base::BindBlock(^{ 3158 web::WebThread::FILE, FROM_HERE, base::BindBlock(^{
3144 [self saveImage:data]; 3159 [self saveImage:data withFileExtension:fileExtension];
3145 })); 3160 }));
3146 break; 3161 break;
3147 } 3162 }
3148 } 3163 }
3149 } 3164 }
3150 3165
3151 - (void)saveImage:(NSData*)data { 3166 - (void)saveImage:(NSData*)data withFileExtension:(NSString*)fileExtension {
3152 NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString] 3167 NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString]
3153 stringByAppendingString:@".png"]; 3168 stringByAppendingString:fileExtension];
3154 NSURL* fileURL = 3169 NSURL* fileURL =
3155 [NSURL fileURLWithPath:[NSTemporaryDirectory() 3170 [NSURL fileURLWithPath:[NSTemporaryDirectory()
3156 stringByAppendingPathComponent:fileName]]; 3171 stringByAppendingPathComponent:fileName]];
3157 NSError* error = nil; 3172 NSError* error = nil;
3158 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error]; 3173 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error];
3159 3174
3160 // Error while writing the image to disk. 3175 // Error while writing the image to disk.
3161 if (error) { 3176 if (error) {
3162 NSString* errorMessage = [NSString 3177 NSString* errorMessage = [NSString
3163 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription], 3178 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription],
(...skipping 1949 matching lines...) Expand 10 before | Expand all | Expand 10 after
5113 5128
5114 - (UIView*)voiceSearchButton { 5129 - (UIView*)voiceSearchButton {
5115 return _voiceSearchButton; 5130 return _voiceSearchButton;
5116 } 5131 }
5117 5132
5118 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner { 5133 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner {
5119 return [self currentLogoAnimationControllerOwner]; 5134 return [self currentLogoAnimationControllerOwner];
5120 } 5135 }
5121 5136
5122 @end 5137 @end
OLDNEW
« no previous file with comments | « no previous file | ios/web/public/image_fetcher/image_data_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698