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

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

Issue 2595573002: Remove image name (Closed)
Patch Set: Address nit Created 3 years, 12 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 | 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>
(...skipping 15 matching lines...) Expand all
26 #include "base/mac/bundle_locations.h" 26 #include "base/mac/bundle_locations.h"
27 #include "base/mac/foundation_util.h" 27 #include "base/mac/foundation_util.h"
28 #include "base/mac/objc_property_releaser.h" 28 #include "base/mac/objc_property_releaser.h"
29 #import "base/mac/scoped_block.h" 29 #import "base/mac/scoped_block.h"
30 #import "base/mac/scoped_nsobject.h" 30 #import "base/mac/scoped_nsobject.h"
31 #include "base/macros.h" 31 #include "base/macros.h"
32 #include "base/memory/ptr_util.h" 32 #include "base/memory/ptr_util.h"
33 #include "base/metrics/histogram.h" 33 #include "base/metrics/histogram.h"
34 #include "base/metrics/user_metrics.h" 34 #include "base/metrics/user_metrics.h"
35 #include "base/metrics/user_metrics_action.h" 35 #include "base/metrics/user_metrics_action.h"
36 #include "base/strings/string_piece.h"
37 #include "base/strings/string_util.h"
38 #include "base/strings/sys_string_conversions.h" 36 #include "base/strings/sys_string_conversions.h"
39 #include "components/bookmarks/browser/base_bookmark_model_observer.h" 37 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
40 #include "components/bookmarks/browser/bookmark_model.h" 38 #include "components/bookmarks/browser/bookmark_model.h"
41 #include "components/infobars/core/infobar_manager.h" 39 #include "components/infobars/core/infobar_manager.h"
42 #include "components/prefs/pref_service.h" 40 #include "components/prefs/pref_service.h"
43 #include "components/reading_list/core/reading_list_switches.h" 41 #include "components/reading_list/core/reading_list_switches.h"
44 #include "components/reading_list/ios/reading_list_model.h" 42 #include "components/reading_list/ios/reading_list_model.h"
45 #include "components/search_engines/search_engines_pref_names.h" 43 #include "components/search_engines/search_engines_pref_names.h"
46 #include "components/search_engines/template_url_service.h" 44 #include "components/search_engines/template_url_service.h"
47 #include "components/sessions/core/tab_restore_service_helper.h" 45 #include "components/sessions/core/tab_restore_service_helper.h"
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 - (void)showErrorAlert:(int)titleMessageId message:(int)messageId; 681 - (void)showErrorAlert:(int)titleMessageId message:(int)messageId;
684 // Helper method displaying an alert with the given title and message. 682 // Helper method displaying an alert with the given title and message.
685 // Dismisses previous alert if it has not been dismissed yet. 683 // Dismisses previous alert if it has not been dismissed yet.
686 - (void)showErrorAlertWithStringTitle:(NSString*)title 684 - (void)showErrorAlertWithStringTitle:(NSString*)title
687 message:(NSString*)message; 685 message:(NSString*)message;
688 // Shows a self-dismissing snackbar displaying |message|. 686 // Shows a self-dismissing snackbar displaying |message|.
689 - (void)showSnackbar:(NSString*)message; 687 - (void)showSnackbar:(NSString*)message;
690 // Induces an intentional crash in the browser process. 688 // Induces an intentional crash in the browser process.
691 - (void)induceBrowserCrash; 689 - (void)induceBrowserCrash;
692 // Saves the image or display error message, based on privacy settings. 690 // Saves the image or display error message, based on privacy settings.
693 - (void)managePermissionAndSaveImage:(NSData*)data fileName:(NSString*)fileName; 691 - (void)managePermissionAndSaveImage:(NSData*)data;
694 // Saves the image. In order to keep the metadata of the image, the image is 692 // Saves the image. In order to keep the metadata of the image, the image is
695 // saved as a temporary file on disk then saved in photos. 693 // saved as a temporary file on disk then saved in photos.
696 // This should be called on FILE thread. 694 // This should be called on FILE thread.
697 - (void)saveImage:(NSData*)data fileName:(NSString*)fileName; 695 - (void)saveImage:(NSData*)data;
698 // 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
699 // user can change it. 697 // user can change it.
700 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's 698 // Shows a privacy alert on the main queue, allowing the user to go to Chrome's
701 // settings. Dismiss previous alert if it has not been dismissed yet. 699 // settings. Dismiss previous alert if it has not been dismissed yet.
702 - (void)displayImageErrorAlertWithSettingsOnMainQueue; 700 - (void)displayImageErrorAlertWithSettingsOnMainQueue;
703 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss 701 // Shows a privacy alert allowing the user to go to Chrome's settings. Dismiss
704 // previous alert if it has not been dismissed yet. 702 // previous alert if it has not been dismissed yet.
705 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL; 703 - (void)displayImageErrorAlertWithSettings:(NSURL*)settingURL;
706 // Called when Chrome has been denied access to the photos or videos and the 704 // Called when Chrome has been denied access to the photos or videos and the
707 // user cannot change it. 705 // user cannot change it.
(...skipping 2333 matching lines...) Expand 10 before | Expand all | Expand 10 after
3041 search_args, templateUrlService->search_terms_data(), &post_content)); 3039 search_args, templateUrlService->search_terms_data(), &post_content));
3042 [self addSelectedTabWithURL:result 3040 [self addSelectedTabWithURL:result
3043 postData:&post_content 3041 postData:&post_content
3044 transition:ui::PAGE_TRANSITION_TYPED]; 3042 transition:ui::PAGE_TRANSITION_TYPED];
3045 } 3043 }
3046 3044
3047 - (void)saveImageAtURL:(const GURL&)url 3045 - (void)saveImageAtURL:(const GURL&)url
3048 referrer:(const web::Referrer&)referrer { 3046 referrer:(const web::Referrer&)referrer {
3049 DCHECK(url.is_valid()); 3047 DCHECK(url.is_valid());
3050 3048
3051 NSString* fileName = [NSString
3052 stringWithFormat:@"%@.png",
3053 [[NSProcessInfo processInfo] globallyUniqueString]];
3054
3055 if (url.SchemeIs(base::StringPiece("data"))) {
3056 if (!base::StartsWith(url.GetContent(), "image",
3057 base::CompareCase::INSENSITIVE_ASCII)) {
3058 // Not an image, return.
3059 return;
3060 }
3061
3062 // Find the type of the image. It should be positionned like here:
3063 // "image/png;base...", between the '/' and the ';'.
3064 size_t semiColonPosition = url.GetContent().find(std::string(";"));
3065 size_t slashPosition = url.GetContent().find(std::string("/"));
3066 size_t substrLength = semiColonPosition - slashPosition - 1;
3067 if (substrLength > 1) {
3068 // Make sure there is a type.
3069 fileName = [NSString
3070 stringWithFormat:@"image.%@",
3071 base::SysUTF8ToNSString(url.GetContent().substr(
3072 slashPosition + 1, substrLength))];
3073 }
3074 } else {
3075 fileName = base::SysUTF8ToNSString(url.ExtractFileName());
3076 }
3077
3078 web::ImageFetchedCallback callback = 3049 web::ImageFetchedCallback callback =
3079 ^(const GURL& original_url, int response_code, NSData* data) { 3050 ^(const GURL& original_url, int response_code, NSData* data) {
3080 DCHECK(data); 3051 DCHECK(data);
3081 3052
3082 [self managePermissionAndSaveImage:data fileName:fileName]; 3053 [self managePermissionAndSaveImage:data];
3083 }; 3054 };
3084 _imageFetcher->StartDownload( 3055 _imageFetcher->StartDownload(
3085 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), 3056 url, callback, web::ReferrerHeaderValueForNavigation(url, referrer),
3086 web::PolicyForNavigation(url, referrer)); 3057 web::PolicyForNavigation(url, referrer));
3087 } 3058 }
3088 3059
3089 - (void)managePermissionAndSaveImage:(NSData*)data 3060 - (void)managePermissionAndSaveImage:(NSData*)data {
3090 fileName:(NSString*)fileName {
3091 switch ([PHPhotoLibrary authorizationStatus]) { 3061 switch ([PHPhotoLibrary authorizationStatus]) {
3092 // User was never asked for permission to access photos. 3062 // User was never asked for permission to access photos.
3093 case PHAuthorizationStatusNotDetermined: 3063 case PHAuthorizationStatusNotDetermined:
3094 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { 3064 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
3095 // Call -saveImage again to check if chrome needs to display an error or 3065 // Call -saveImage again to check if chrome needs to display an error or
3096 // saves the image. 3066 // saves the image.
3097 if (status != PHAuthorizationStatusNotDetermined) 3067 if (status != PHAuthorizationStatusNotDetermined)
3098 [self managePermissionAndSaveImage:data fileName:fileName]; 3068 [self managePermissionAndSaveImage:data];
3099 }]; 3069 }];
3100 break; 3070 break;
3101 3071
3102 // The application doesn't have permission to access photo and the user 3072 // The application doesn't have permission to access photo and the user
3103 // cannot grant it. 3073 // cannot grant it.
3104 case PHAuthorizationStatusRestricted: 3074 case PHAuthorizationStatusRestricted:
3105 [self displayPrivacyErrorAlertOnMainQueue: 3075 [self displayPrivacyErrorAlertOnMainQueue:
3106 l10n_util::GetNSString( 3076 l10n_util::GetNSString(
3107 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)]; 3077 IDS_IOS_SAVE_IMAGE_RESTRICTED_PRIVACY_ALERT_MESSAGE)];
3108 break; 3078 break;
3109 3079
3110 // The application doesn't have permission to access photo and the user 3080 // The application doesn't have permission to access photo and the user
3111 // can grant it. 3081 // can grant it.
3112 case PHAuthorizationStatusDenied: 3082 case PHAuthorizationStatusDenied:
3113 [self displayImageErrorAlertWithSettingsOnMainQueue]; 3083 [self displayImageErrorAlertWithSettingsOnMainQueue];
3114 break; 3084 break;
3115 3085
3116 // The application has permission to access the photos. 3086 // The application has permission to access the photos.
3117 default: { 3087 default: {
3118 web::WebThread::PostTask(web::WebThread::FILE, FROM_HERE, 3088 web::WebThread::PostTask(web::WebThread::FILE, FROM_HERE,
3119 base::BindBlock(^{ 3089 base::BindBlock(^{
3120 [self saveImage:data fileName:fileName]; 3090 [self saveImage:data];
3121 })); 3091 }));
3122 break; 3092 break;
3123 } 3093 }
3124 } 3094 }
3125 } 3095 }
3126 3096
3127 - (void)saveImage:(NSData*)data fileName:(NSString*)fileName { 3097 - (void)saveImage:(NSData*)data {
3098 NSString* fileName = [[[NSProcessInfo processInfo] globallyUniqueString]
3099 stringByAppendingString:@".png"];
3128 NSURL* fileURL = 3100 NSURL* fileURL =
3129 [NSURL fileURLWithPath:[NSTemporaryDirectory() 3101 [NSURL fileURLWithPath:[NSTemporaryDirectory()
3130 stringByAppendingPathComponent:fileName]]; 3102 stringByAppendingPathComponent:fileName]];
3131 NSError* error = nil; 3103 NSError* error = nil;
3132 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error]; 3104 [data writeToURL:fileURL options:NSDataWritingAtomic error:&error];
3133 3105
3134 // Error while writing the image to disk. 3106 // Error while writing the image to disk.
3135 if (error) { 3107 if (error) {
3136 NSString* errorMessage = [NSString 3108 NSString* errorMessage = [NSString
3137 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription], 3109 stringWithFormat:@"%@ (%@ %" PRIdNS ")", [error localizedDescription],
(...skipping 1979 matching lines...) Expand 10 before | Expand all | Expand 10 after
5117 5089
5118 - (UIView*)voiceSearchButton { 5090 - (UIView*)voiceSearchButton {
5119 return _voiceSearchButton; 5091 return _voiceSearchButton;
5120 } 5092 }
5121 5093
5122 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner { 5094 - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner {
5123 return [self currentLogoAnimationControllerOwner]; 5095 return [self currentLogoAnimationControllerOwner];
5124 } 5096 }
5125 5097
5126 @end 5098 @end
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698