Index: ios/chrome/browser/ui/browser_view_controller.mm |
diff --git a/ios/chrome/browser/ui/browser_view_controller.mm b/ios/chrome/browser/ui/browser_view_controller.mm |
index 09f2a6ac706186d7b750a0c322e6e123bd18fcf0..ec26522f090e0fa9691d3ed686ef6b63739a02db 100644 |
--- a/ios/chrome/browser/ui/browser_view_controller.mm |
+++ b/ios/chrome/browser/ui/browser_view_controller.mm |
@@ -16,6 +16,7 @@ |
#include "base/base64.h" |
#include "base/command_line.h" |
+#include "base/files/file_path.h" |
#include "base/format_macros.h" |
#include "base/i18n/rtl.h" |
#include "base/ios/block_types.h" |
@@ -184,6 +185,7 @@ |
#include "ios/web/public/web_thread.h" |
#import "ios/web/web_state/ui/crw_web_controller.h" |
#import "net/base/mac/url_conversions.h" |
+#include "net/base/mime_util.h" |
#include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
#include "net/ssl/ssl_info.h" |
#include "net/url_request/url_request_context_getter.h" |
@@ -678,11 +680,12 @@ NSString* const kNativeControllerTemporaryKey = @"NativeControllerTemporaryKey"; |
// Returns Tab that corresponds to the given |webState|. |
- (Tab*)tabForWebState:(web::WebState*)webState; |
// Saves the image or display error message, based on privacy settings. |
-- (void)managePermissionAndSaveImage:(NSData*)data; |
+- (void)managePermissionAndSaveImage:(NSData*)data |
+ fileExtension:(NSString*)fileExtension; |
// Saves the image. In order to keep the metadata of the image, the image is |
// saved as a temporary file on disk then saved in photos. |
// This should be called on FILE thread. |
-- (void)saveImage:(NSData*)data; |
+- (void)saveImage:(NSData*)data fileExtension:(NSString*)fileExtension; |
// Called when Chrome has been denied access to the photos or videos and the |
// user can change it. |
// Shows a privacy alert on the main queue, allowing the user to go to Chrome's |
@@ -2992,7 +2995,8 @@ class BrowserBookmarkModelBridge : public bookmarks::BookmarkModelObserver { |
DCHECK(url.is_valid()); |
base::WeakNSObject<BrowserViewController> weakSelf(self); |
const GURL image_source_url = url; |
- image_fetcher::IOSImageDataFetcherCallback callback = ^(NSData* data) { |
+ image_fetcher::IOSImageDataFetcherCallback callback = ^( |
+ NSData* data, const image_fetcher::RequestMetadata& metadata) { |
DCHECK(data); |
dispatch_async(dispatch_get_main_queue(), ^{ |
[weakSelf searchByImageData:data atURL:image_source_url]; |
@@ -3048,17 +3052,29 @@ class BrowserBookmarkModelBridge : public bookmarks::BookmarkModelObserver { |
referrer:(const web::Referrer&)referrer { |
DCHECK(url.is_valid()); |
- image_fetcher::IOSImageDataFetcherCallback callback = ^(NSData* data) { |
- DCHECK(data); |
+ image_fetcher::IOSImageDataFetcherCallback callback = |
+ ^(NSData* data, const image_fetcher::RequestMetadata& metadata) { |
+ DCHECK(data); |
- [self managePermissionAndSaveImage:data]; |
- }; |
+ base::FilePath::StringType extension; |
+ |
+ bool extensionSuccess = net::GetPreferredExtensionForMimeType( |
+ metadata.mime_type, &extension); |
+ if (!extensionSuccess || extension.length() == 0) { |
+ extension = "png"; |
+ } |
+ |
+ NSString* fileExtension = |
+ [@"." stringByAppendingString:base::SysUTF8ToNSString(extension)]; |
+ [self managePermissionAndSaveImage:data fileExtension:fileExtension]; |
+ }; |
_imageFetcher->FetchImageDataWebpDecoded( |
url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), |
web::PolicyForNavigation(url, referrer)); |
} |
-- (void)managePermissionAndSaveImage:(NSData*)data { |
+- (void)managePermissionAndSaveImage:(NSData*)data |
+ 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.
|
switch ([PHPhotoLibrary authorizationStatus]) { |
// User was never asked for permission to access photos. |
case PHAuthorizationStatusNotDetermined: |
@@ -3066,7 +3082,7 @@ class BrowserBookmarkModelBridge : public bookmarks::BookmarkModelObserver { |
// Call -saveImage again to check if chrome needs to display an error or |
// saves the image. |
if (status != PHAuthorizationStatusNotDetermined) |
- [self managePermissionAndSaveImage:data]; |
+ [self managePermissionAndSaveImage:data fileExtension:fileExtension]; |
}]; |
break; |
@@ -3086,18 +3102,18 @@ class BrowserBookmarkModelBridge : public bookmarks::BookmarkModelObserver { |
// The application has permission to access the photos. |
default: { |
- web::WebThread::PostTask(web::WebThread::FILE, FROM_HERE, |
- base::BindBlock(^{ |
- [self saveImage:data]; |
- })); |
+ web::WebThread::PostTask( |
+ web::WebThread::FILE, FROM_HERE, base::BindBlock(^{ |
+ [self saveImage:data fileExtension:fileExtension]; |
+ })); |
break; |
} |
} |
} |
-- (void)saveImage:(NSData*)data { |
+- (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.
|
NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString] |
- stringByAppendingString:@".png"]; |
+ stringByAppendingString:fileExtension]; |
NSURL* fileURL = |
[NSURL fileURLWithPath:[NSTemporaryDirectory() |
stringByAppendingPathComponent:fileName]]; |