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 @@
"owners": [ "dmazzoni", "thestig" ],
"expiry_milestone": 86
},
{
"name": "extend-open-in-files-support",
"owners": [ "ewannpv", "gambard" ],
"expiry_milestone": 89
},
{
"name": "extension-apis",
"owners": [ "rdevlin.cronin" ],
......
......@@ -47,6 +47,7 @@ source_set("flags") {
"//ios/chrome/browser/crash_report",
"//ios/chrome/browser/crash_report/breadcrumbs:feature_flags",
"//ios/chrome/browser/drag_and_drop",
"//ios/chrome/browser/open_in:features",
"//ios/chrome/browser/passwords:feature_flags",
"//ios/chrome/browser/policy:feature_flags",
"//ios/chrome/browser/ui:feature_flags",
......
......@@ -59,6 +59,7 @@
#include "ios/chrome/browser/crash_report/features.h"
#include "ios/chrome/browser/drag_and_drop/drag_and_drop_flag.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/policy/policy_features.h"
#include "ios/chrome/browser/system_flags.h"
......@@ -668,6 +669,10 @@ const flags_ui::FeatureEntry kFeatureEntries[] = {
flag_descriptions::kEnableAutofillPasswordReauthIOSName,
flag_descriptions::kEnableAutofillPasswordReauthIOSDescription,
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) {
......
......@@ -264,6 +264,11 @@ const char kExpandedTabStripDescription[] =
"Enables the new expanded tabstrip. Activated by swiping down the tabstrip"
" 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 kForceStartupSigninPromoDescription[] =
"When enabled, the startup sign-in promo is always displayed when starting "
......
......@@ -222,6 +222,10 @@ extern const char kEnablePersistentDownloadsDescription[];
extern const char kExpandedTabStripName[];
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.
extern const char kForceStartupSigninPromoName[];
extern const char kForceStartupSigninPromoDescription[];
......
......@@ -12,16 +12,27 @@ source_set("open_in") {
"open_in_tab_helper_delegate.h",
]
deps = [
":features",
"//base",
"//ios/chrome/app/strings:ios_strings_grit",
"//ios/chrome/browser",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/ui:feature_flags",
"//ios/web/public",
"//ui/base:base",
"//url",
]
}
source_set("features") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"features.h",
"features.mm",
]
deps = [ "//base" ]
}
source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
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,
// PDF.
void HandleExportableFile();
// Tests that files are exportable.
bool isExportableFile() const;
// WebStateObserver implementation.
void PageLoaded(
web::WebState* web_state,
......
......@@ -6,6 +6,8 @@
#include "base/memory/ptr_util.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"
#import "ios/web/public/navigation/navigation_context.h"
#import "ios/web/public/navigation/navigation_item.h"
......@@ -20,6 +22,49 @@
#error "This file requires ARC support."
#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
void OpenInTabHelper::CreateForWebState(web::WebState* web_state) {
DCHECK(web_state);
......@@ -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() {
// Only "application/pdf" is supported for now.
if (web_state_->GetContentsMimeType() != "application/pdf")
if (base::FeatureList::IsEnabled(kExtendOpenInFilesSupport)) {
if (!isExportableFile())
return;
} else if (web_state_->GetContentsMimeType() != "application/pdf") {
return;
}
// 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
......@@ -61,9 +138,9 @@ void OpenInTabHelper::HandleExportableFile() {
const GURL& last_committed_url = item ? item->GetURL() : GURL::EmptyGURL();
base::string16 file_name =
net::GetSuggestedFilename(last_committed_url, content_disposition,
"", // referrer-charset
"", // suggested-name
"application/pdf", // mime-type
"", // referrer-charset
"", // suggested-name
web_state_->GetContentsMimeType(), // mime-type
default_file_name);
[delegate_ enableOpenInForWebState:web_state_
withDocumentURL:last_committed_url
......
......@@ -4,6 +4,8 @@
#import "ios/chrome/browser/ui/open_in/open_in_controller.h"
#import <QuickLook/QuickLook.h>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/location.h"
......@@ -71,13 +73,19 @@ void LogOpenInDownloadResult(const OpenInDownloadResult result) {
UMA_HISTOGRAM_ENUMERATION("IOS.OpenIn.DownloadResult", result);
}
// Returns true if the file located at |url| is a valid PDF file.
bool HasValidPdfAtUrl(NSURL* _Nullable url) {
// Returns true if the file located at |url| is file.
bool HasValidFileAtUrl(NSURL* _Nullable url) {
if (!url)
return false;
base::ScopedCFTypeRef<CGPDFDocumentRef> document(
CGPDFDocumentCreateWithURL((__bridge CFURLRef)url));
return document;
NSString* extension = [[url path] pathExtension];
if ([extension isEqualToString:@"pdf"]) {
base::ScopedCFTypeRef<CGPDFDocumentRef> document(
CGPDFDocumentCreateWithURL((__bridge CFURLRef)url));
return document;
}
return [QLPreviewController canPreviewItem:url];
}
} // anonymous namespace
......@@ -608,7 +616,7 @@ class OpenInControllerBridge
NSURL* fileURL = nil;
if (!filePath.empty())
fileURL = [NSURL fileURLWithPath:base::SysUTF8ToNSString(filePath.value())];
if (!_downloadCanceled && HasValidPdfAtUrl(fileURL)) {
if (!_downloadCanceled && HasValidFileAtUrl(fileURL)) {
LogOpenInDownloadResult(OpenInDownloadResult::kSucceeded);
[self presentOpenInMenuForFileAtURL:fileURL];
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