Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | // |
| 5 | // This file provides API functions to fetch external thumbnails for filesystem |
| 6 | // providers that support it. |
| 7 | |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 8 | #ifndef CHROME_BROWSER_ASH_EXTENSIONS_FILE_MANAGER_IMAGE_LOADER_PRIVATE_API_H_ |
| 9 | #define CHROME_BROWSER_ASH_EXTENSIONS_FILE_MANAGER_IMAGE_LOADER_PRIVATE_API_H_ |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 10 | |
| 11 | #include <memory> |
| 12 | #include <string> |
| 13 | |
Henrique Ferreiro | d67f047 | 2022-11-05 00:40:09 | [diff] [blame] | 14 | #include "chrome/browser/ash/extensions/file_manager/logged_extension_function.h" |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 15 | |
Henrique Ferreiro | e828b7b0 | 2021-03-23 23:00:59 | [diff] [blame] | 16 | #include "chrome/browser/ash/arc/fileapi/arc_documents_provider_root.h" |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 17 | #include "chrome/common/extensions/api/image_loader_private.h" |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 18 | #include "chrome/services/printing/public/mojom/pdf_thumbnailer.mojom.h" |
| 19 | #include "mojo/public/cpp/bindings/remote.h" |
Alexander Bolodurin | 5b21741 | 2020-10-21 11:55:31 | [diff] [blame] | 20 | #include "storage/browser/file_system/file_system_url.h" |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 21 | |
| 22 | class SkBitmap; |
| 23 | |
| 24 | namespace extensions { |
| 25 | |
| 26 | // Base class for thumbnail functions |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 27 | class ImageLoaderPrivateGetThumbnailFunction : public LoggedExtensionFunction { |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 28 | public: |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 29 | ImageLoaderPrivateGetThumbnailFunction(); |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 30 | |
| 31 | protected: |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 32 | ~ImageLoaderPrivateGetThumbnailFunction() override = default; |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 33 | |
| 34 | // Responds with a base64 encoded PNG thumbnail data. |
| 35 | void SendEncodedThumbnail(std::string thumbnail_data_url); |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 36 | }; |
| 37 | |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 38 | class ImageLoaderPrivateGetDriveThumbnailFunction |
| 39 | : public ImageLoaderPrivateGetThumbnailFunction { |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 40 | public: |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 41 | DECLARE_EXTENSION_FUNCTION("imageLoaderPrivate.getDriveThumbnail", |
| 42 | IMAGELOADERPRIVATE_GETDRIVETHUMBNAIL) |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 43 | |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 44 | ImageLoaderPrivateGetDriveThumbnailFunction(); |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 45 | |
| 46 | protected: |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 47 | ~ImageLoaderPrivateGetDriveThumbnailFunction() override; |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 48 | |
| 49 | // ExtensionFunction overrides. |
| 50 | ResponseAction Run() override; |
| 51 | |
| 52 | private: |
| 53 | // A callback invoked when thumbnail data has been generated. |
Mohamed Mansour | 5cf9214 | 2023-12-08 23:12:12 | [diff] [blame] | 54 | void GotThumbnail(const std::optional<std::vector<uint8_t>>& data); |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 55 | }; |
| 56 | |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 57 | class ImageLoaderPrivateGetPdfThumbnailFunction |
| 58 | : public ImageLoaderPrivateGetThumbnailFunction { |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 59 | public: |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 60 | DECLARE_EXTENSION_FUNCTION("imageLoaderPrivate.getPdfThumbnail", |
| 61 | IMAGELOADERPRIVATE_GETPDFTHUMBNAIL) |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 62 | |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 63 | ImageLoaderPrivateGetPdfThumbnailFunction(); |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 64 | |
| 65 | protected: |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 66 | ~ImageLoaderPrivateGetPdfThumbnailFunction() override; |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 67 | |
| 68 | // ExtensionFunction overrides. |
| 69 | ResponseAction Run() override; |
| 70 | |
| 71 | private: |
| 72 | // For a given |content| starts fetching the first page PDF thumbnail by |
| 73 | // calling PdfThumbnailer from the printing service. The first parameters, |
| 74 | // |size| is supplied by the JavaScript caller. |
| 75 | void FetchThumbnail(const gfx::Size& size, const std::string& content); |
| 76 | |
| 77 | // Callback invoked by the thumbnailing service when a PDF thumbnail has been |
| 78 | // generated. The solitary parameter |bitmap| is supplied by the callback. |
| 79 | // If |bitmap| is null, an error occurred. Otherwise, |bitmap| contains the |
| 80 | // generated thumbnail. |
| 81 | void GotThumbnail(const SkBitmap& bitmap); |
| 82 | |
| 83 | // Handles a mojo channel disconnect event. |
| 84 | void ThumbnailDisconnected(); |
| 85 | |
| 86 | // Holds the channel to Printing PDF thumbnailing service. Bound only |
| 87 | // when needed. |
| 88 | mojo::Remote<printing::mojom::PdfThumbnailer> pdf_thumbnailer_; |
| 89 | |
| 90 | // The dots per inch (dpi) resolution at which the PDF is rendered to a |
| 91 | // thumbnail. The value of 30 is selected so that a US Letter size page does |
| 92 | // not overflow a kSize x kSize thumbnail. |
| 93 | constexpr static int kDpi = 30; |
| 94 | }; |
| 95 | |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 96 | class ImageLoaderPrivateGetArcDocumentsProviderThumbnailFunction |
| 97 | : public ImageLoaderPrivateGetThumbnailFunction { |
Alexander Bolodurin | 5b21741 | 2020-10-21 11:55:31 | [diff] [blame] | 98 | public: |
| 99 | DECLARE_EXTENSION_FUNCTION( |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 100 | "imageLoaderPrivate.getArcDocumentsProviderThumbnail", |
| 101 | IMAGELOADERPRIVATE_GETARCDOCUMENTSPROVIDERTHUMBNAIL) |
Alexander Bolodurin | 5b21741 | 2020-10-21 11:55:31 | [diff] [blame] | 102 | |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 103 | ImageLoaderPrivateGetArcDocumentsProviderThumbnailFunction(); |
Alexander Bolodurin | 5b21741 | 2020-10-21 11:55:31 | [diff] [blame] | 104 | |
| 105 | protected: |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 106 | ~ImageLoaderPrivateGetArcDocumentsProviderThumbnailFunction() override; |
Alexander Bolodurin | 5b21741 | 2020-10-21 11:55:31 | [diff] [blame] | 107 | |
| 108 | // ExtensionFunction overrides. |
| 109 | ResponseAction Run() override; |
| 110 | |
| 111 | private: |
| 112 | // A callback invoked when ExtraFileMetadata is returned from ARC. |
| 113 | void OnGetExtraFileMetadata( |
| 114 | const gfx::Size& size_hint, |
| 115 | const storage::FileSystemURL& file_system_url, |
| 116 | base::File::Error result, |
| 117 | const arc::ArcDocumentsProviderRoot::ExtraFileMetadata& metadata); |
| 118 | |
| 119 | // A callback invoked when a FilesystemURL is resolved to content URLs. |
Cherie Cheung | 24b6c92d | 2021-01-15 18:34:51 | [diff] [blame] | 120 | // |paths_to_share| is always expected to be empty because |
| 121 | // ArcDocumentsProviderThumbnail related functions do not share path |
| 122 | // to ARCVM via Seneschal. |
Alexander Bolodurin | 5b21741 | 2020-10-21 11:55:31 | [diff] [blame] | 123 | void GotContentUrls(const gfx::Size& size_hint, |
Cherie Cheung | 24b6c92d | 2021-01-15 18:34:51 | [diff] [blame] | 124 | const std::vector<GURL>& urls, |
| 125 | const std::vector<base::FilePath>& paths_to_share); |
Alexander Bolodurin | 5b21741 | 2020-10-21 11:55:31 | [diff] [blame] | 126 | |
| 127 | // A callback invoked when ARC thumbnail file has been opened. |
| 128 | void GotArcThumbnailFileHandle(mojo::ScopedHandle handle); |
| 129 | }; |
| 130 | |
Alexander Bolodurin | e76e12a | 2020-10-06 09:05:35 | [diff] [blame] | 131 | } // namespace extensions |
| 132 | |
Joel Hockey | c2aae80 | 2023-08-11 05:06:13 | [diff] [blame] | 133 | #endif // CHROME_BROWSER_ASH_EXTENSIONS_FILE_MANAGER_IMAGE_LOADER_PRIVATE_API_H_ |