Commit 163e3957 authored by Ewann's avatar Ewann Committed by Commit Bot

[iOS] Adds OpenIn.. Toolbar to files that support preview

This CL is developed under a flag.
It displays the OpenIn.. toolbar to all files that support preview.

Bug: 1117398
Change-Id: I32ad05e7cf0de89f3407366fa6767a29af3b80f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2276269
Commit-Queue: Ewann Pellé <ewannpv@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#800540}
parent f4080b20
...@@ -2428,6 +2428,11 @@ ...@@ -2428,6 +2428,11 @@
"owners": [ "dmazzoni", "thestig" ], "owners": [ "dmazzoni", "thestig" ],
"expiry_milestone": 86 "expiry_milestone": 86
}, },
{
"name": "extend-open-in-files-support",
"owners": [ "ewannpv", "gambard" ],
"expiry_milestone": 89
},
{ {
"name": "extension-apis", "name": "extension-apis",
"owners": [ "rdevlin.cronin" ], "owners": [ "rdevlin.cronin" ],
......
...@@ -47,6 +47,7 @@ source_set("flags") { ...@@ -47,6 +47,7 @@ source_set("flags") {
"//ios/chrome/browser/crash_report", "//ios/chrome/browser/crash_report",
"//ios/chrome/browser/crash_report/breadcrumbs:feature_flags", "//ios/chrome/browser/crash_report/breadcrumbs:feature_flags",
"//ios/chrome/browser/drag_and_drop", "//ios/chrome/browser/drag_and_drop",
"//ios/chrome/browser/open_in:features",
"//ios/chrome/browser/passwords:feature_flags", "//ios/chrome/browser/passwords:feature_flags",
"//ios/chrome/browser/policy:feature_flags", "//ios/chrome/browser/policy:feature_flags",
"//ios/chrome/browser/ui:feature_flags", "//ios/chrome/browser/ui:feature_flags",
......
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
#include "ios/chrome/browser/crash_report/features.h" #include "ios/chrome/browser/crash_report/features.h"
#include "ios/chrome/browser/drag_and_drop/drag_and_drop_flag.h" #include "ios/chrome/browser/drag_and_drop/drag_and_drop_flag.h"
#include "ios/chrome/browser/flags/ios_chrome_flag_descriptions.h" #include "ios/chrome/browser/flags/ios_chrome_flag_descriptions.h"
#import "ios/chrome/browser/open_in/features.h"
#include "ios/chrome/browser/passwords/password_manager_features.h" #include "ios/chrome/browser/passwords/password_manager_features.h"
#include "ios/chrome/browser/policy/policy_features.h" #include "ios/chrome/browser/policy/policy_features.h"
#include "ios/chrome/browser/system_flags.h" #include "ios/chrome/browser/system_flags.h"
...@@ -668,6 +669,10 @@ const flags_ui::FeatureEntry kFeatureEntries[] = { ...@@ -668,6 +669,10 @@ const flags_ui::FeatureEntry kFeatureEntries[] = {
flag_descriptions::kEnableAutofillPasswordReauthIOSName, flag_descriptions::kEnableAutofillPasswordReauthIOSName,
flag_descriptions::kEnableAutofillPasswordReauthIOSDescription, flag_descriptions::kEnableAutofillPasswordReauthIOSDescription,
flags_ui::kOsIos, FEATURE_VALUE_TYPE(kEnableAutofillPasswordReauthIOS)}, flags_ui::kOsIos, FEATURE_VALUE_TYPE(kEnableAutofillPasswordReauthIOS)},
{"extend-open-in-files-support",
flag_descriptions::kExtendOpenInFilesSupportName,
flag_descriptions::kExtendOpenInFilesSupportDescription, flags_ui::kOsIos,
FEATURE_VALUE_TYPE(kExtendOpenInFilesSupport)},
}; };
bool SkipConditionalFeatureEntry(const flags_ui::FeatureEntry& entry) { bool SkipConditionalFeatureEntry(const flags_ui::FeatureEntry& entry) {
......
...@@ -264,6 +264,11 @@ const char kExpandedTabStripDescription[] = ...@@ -264,6 +264,11 @@ const char kExpandedTabStripDescription[] =
"Enables the new expanded tabstrip. Activated by swiping down the tabstrip" "Enables the new expanded tabstrip. Activated by swiping down the tabstrip"
" or the toolbar"; " or the toolbar";
const char kExtendOpenInFilesSupportName[] =
"Extend Open in toolbar files support";
const char kExtendOpenInFilesSupportDescription[] =
"When enabled, the Open in toolbar is displayed on more file types";
const char kForceStartupSigninPromoName[] = "Display the startup sign-in promo"; const char kForceStartupSigninPromoName[] = "Display the startup sign-in promo";
const char kForceStartupSigninPromoDescription[] = const char kForceStartupSigninPromoDescription[] =
"When enabled, the startup sign-in promo is always displayed when starting " "When enabled, the startup sign-in promo is always displayed when starting "
......
...@@ -222,6 +222,10 @@ extern const char kEnablePersistentDownloadsDescription[]; ...@@ -222,6 +222,10 @@ extern const char kEnablePersistentDownloadsDescription[];
extern const char kExpandedTabStripName[]; extern const char kExpandedTabStripName[];
extern const char kExpandedTabStripDescription[]; extern const char kExpandedTabStripDescription[];
// Title and description for the flag to extend Open in toolbar files support.
extern const char kExtendOpenInFilesSupportName[];
extern const char kExtendOpenInFilesSupportDescription[];
// Title and description for the flag to trigger the startup sign-in promo. // Title and description for the flag to trigger the startup sign-in promo.
extern const char kForceStartupSigninPromoName[]; extern const char kForceStartupSigninPromoName[];
extern const char kForceStartupSigninPromoDescription[]; extern const char kForceStartupSigninPromoDescription[];
......
...@@ -12,16 +12,27 @@ source_set("open_in") { ...@@ -12,16 +12,27 @@ source_set("open_in") {
"open_in_tab_helper_delegate.h", "open_in_tab_helper_delegate.h",
] ]
deps = [ deps = [
":features",
"//base", "//base",
"//ios/chrome/app/strings:ios_strings_grit", "//ios/chrome/app/strings:ios_strings_grit",
"//ios/chrome/browser", "//ios/chrome/browser",
"//ios/chrome/browser/browser_state", "//ios/chrome/browser/browser_state",
"//ios/chrome/browser/ui:feature_flags",
"//ios/web/public", "//ios/web/public",
"//ui/base:base", "//ui/base:base",
"//url", "//url",
] ]
} }
source_set("features") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"features.h",
"features.mm",
]
deps = [ "//base" ]
}
source_set("unit_tests") { source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ] configs += [ "//build/config/compiler:enable_arc" ]
testonly = true testonly = true
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_OPEN_IN_FEATURES_H_
#define IOS_CHROME_BROWSER_OPEN_IN_FEATURES_H_
#include "base/feature_list.h"
// Feature to extend Open in toolbar files support.
extern const base::Feature kExtendOpenInFilesSupport;
#endif // IOS_CHROME_BROWSER_OPEN_IN_FEATURES_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/open_in/features.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
const base::Feature kExtendOpenInFilesSupport{
"ExtendOpenInFilesSupport", base::FEATURE_DISABLED_BY_DEFAULT};
...@@ -40,6 +40,9 @@ class OpenInTabHelper : public web::WebStateObserver, ...@@ -40,6 +40,9 @@ class OpenInTabHelper : public web::WebStateObserver,
// PDF. // PDF.
void HandleExportableFile(); void HandleExportableFile();
// Tests that files are exportable.
bool isExportableFile() const;
// WebStateObserver implementation. // WebStateObserver implementation.
void PageLoaded( void PageLoaded(
web::WebState* web_state, web::WebState* web_state,
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "ios/chrome/browser/open_in/features.h"
#include "ios/chrome/browser/ui/ui_feature_flags.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#import "ios/web/public/navigation/navigation_context.h" #import "ios/web/public/navigation/navigation_context.h"
#import "ios/web/public/navigation/navigation_item.h" #import "ios/web/public/navigation/navigation_item.h"
...@@ -20,6 +22,49 @@ ...@@ -20,6 +22,49 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace {
// .pptx extension.
const char kMimeTypeMicrosoftPowerPointOpenXML[] =
"application/vnd.openxmlformats-officedocument.presentationml.presentation";
// .docx extension.
const char kMimeTypeMicrosoftWordOpenXML[] =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
// .xlsx extension.
const char kMimeTypeMicrosoftExcelOpenXML[] =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
// .pdf extension.
const char kMimeTypePDF[] = "application/pdf";
// .doc extension.
const char kMimeTypeMicrosoftWord[] = "application/msword";
// .jpeg or .jpg extension.
const char kMimeTypeJPEG[] = "image/jpeg";
// .png extension.
const char kMimeTypePNG[] = "image/png";
// .ppt extension.
const char kMimeTypeMicrosoftPowerPoint[] = "application/vnd.ms-powerpoint";
// .rtf extension.
const char kMimeTypeRTF[] = "application/rtf";
// .svg extension.
const char kMimeTypeSVG[] = "image/svg+xml";
// .xls extension.
const char kMimeTypeMicrosoftExcel[] = "application/vnd.ms-excel";
// .usdz extension.
const char kMimeTypeUSDZ[] = "model/vnd.usdz+zip";
} // namespace
// static // static
void OpenInTabHelper::CreateForWebState(web::WebState* web_state) { void OpenInTabHelper::CreateForWebState(web::WebState* web_state) {
DCHECK(web_state); DCHECK(web_state);
...@@ -42,10 +87,42 @@ OpenInTabHelper::~OpenInTabHelper() { ...@@ -42,10 +87,42 @@ OpenInTabHelper::~OpenInTabHelper() {
} }
} }
bool OpenInTabHelper::isExportableFile() const {
if (web_state_->GetContentsMimeType() == kMimeTypePDF)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeMicrosoftWord)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeMicrosoftWordOpenXML)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeJPEG)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypePNG)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeMicrosoftPowerPoint)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeMicrosoftPowerPointOpenXML)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeRTF)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeSVG)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeMicrosoftExcel)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeMicrosoftExcelOpenXML)
return true;
if (web_state_->GetContentsMimeType() == kMimeTypeUSDZ)
return true;
return false;
}
void OpenInTabHelper::HandleExportableFile() { void OpenInTabHelper::HandleExportableFile() {
// Only "application/pdf" is supported for now. if (base::FeatureList::IsEnabled(kExtendOpenInFilesSupport)) {
if (web_state_->GetContentsMimeType() != "application/pdf") if (!isExportableFile())
return;
} else if (web_state_->GetContentsMimeType() != "application/pdf") {
return; return;
}
// Try to generate a filename by first looking at |content_disposition_|, then // Try to generate a filename by first looking at |content_disposition_|, then
// at the last component of WebState's last committed URL and if both of these // at the last component of WebState's last committed URL and if both of these
...@@ -61,9 +138,9 @@ void OpenInTabHelper::HandleExportableFile() { ...@@ -61,9 +138,9 @@ void OpenInTabHelper::HandleExportableFile() {
const GURL& last_committed_url = item ? item->GetURL() : GURL::EmptyGURL(); const GURL& last_committed_url = item ? item->GetURL() : GURL::EmptyGURL();
base::string16 file_name = base::string16 file_name =
net::GetSuggestedFilename(last_committed_url, content_disposition, net::GetSuggestedFilename(last_committed_url, content_disposition,
"", // referrer-charset "", // referrer-charset
"", // suggested-name "", // suggested-name
"application/pdf", // mime-type web_state_->GetContentsMimeType(), // mime-type
default_file_name); default_file_name);
[delegate_ enableOpenInForWebState:web_state_ [delegate_ enableOpenInForWebState:web_state_
withDocumentURL:last_committed_url withDocumentURL:last_committed_url
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#import "ios/chrome/browser/ui/open_in/open_in_controller.h" #import "ios/chrome/browser/ui/open_in/open_in_controller.h"
#import <QuickLook/QuickLook.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/location.h" #include "base/location.h"
...@@ -71,13 +73,19 @@ void LogOpenInDownloadResult(const OpenInDownloadResult result) { ...@@ -71,13 +73,19 @@ void LogOpenInDownloadResult(const OpenInDownloadResult result) {
UMA_HISTOGRAM_ENUMERATION("IOS.OpenIn.DownloadResult", result); UMA_HISTOGRAM_ENUMERATION("IOS.OpenIn.DownloadResult", result);
} }
// Returns true if the file located at |url| is a valid PDF file. // Returns true if the file located at |url| is file.
bool HasValidPdfAtUrl(NSURL* _Nullable url) { bool HasValidFileAtUrl(NSURL* _Nullable url) {
if (!url) if (!url)
return false; return false;
base::ScopedCFTypeRef<CGPDFDocumentRef> document(
CGPDFDocumentCreateWithURL((__bridge CFURLRef)url)); NSString* extension = [[url path] pathExtension];
return document; if ([extension isEqualToString:@"pdf"]) {
base::ScopedCFTypeRef<CGPDFDocumentRef> document(
CGPDFDocumentCreateWithURL((__bridge CFURLRef)url));
return document;
}
return [QLPreviewController canPreviewItem:url];
} }
} // anonymous namespace } // anonymous namespace
...@@ -608,7 +616,7 @@ class OpenInControllerBridge ...@@ -608,7 +616,7 @@ class OpenInControllerBridge
NSURL* fileURL = nil; NSURL* fileURL = nil;
if (!filePath.empty()) if (!filePath.empty())
fileURL = [NSURL fileURLWithPath:base::SysUTF8ToNSString(filePath.value())]; fileURL = [NSURL fileURLWithPath:base::SysUTF8ToNSString(filePath.value())];
if (!_downloadCanceled && HasValidPdfAtUrl(fileURL)) { if (!_downloadCanceled && HasValidFileAtUrl(fileURL)) {
LogOpenInDownloadResult(OpenInDownloadResult::kSucceeded); LogOpenInDownloadResult(OpenInDownloadResult::kSucceeded);
[self presentOpenInMenuForFileAtURL:fileURL]; [self presentOpenInMenuForFileAtURL:fileURL];
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment