| 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..8a5f96389cc6438c3a09690a44846f4d504f70ef 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 std::string& mimeType) {
|
| 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 std::string& mimeType) {
|
| + DCHECK(data);
|
|
|
| - [self managePermissionAndSaveImage:data];
|
| - };
|
| + base::FilePath::StringType extension;
|
| +
|
| + bool extensionSuccess =
|
| + net::GetPreferredExtensionForMimeType(mimeType, &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 {
|
| 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,16 +3102,16 @@ 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 {
|
| NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString]
|
| stringByAppendingString:@".png"];
|
| NSURL* fileURL =
|
|
|