[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [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 | |
[email protected] | fd42ac30f | 2011-02-27 19:33:36 | [diff] [blame] | 5 | #include "chrome/browser/ui/webui/fileicon_source.h" |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 6 | |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 8 | #include "base/callback.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/ref_counted_memory.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 11 | #include "base/strings/string_split.h" |
[email protected] | 774cc3c | 2013-06-07 20:26:45 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 13 | #include "chrome/browser/browser_process.h" |
Dan Beam | b0dba21 | 2019-04-20 03:25:19 | [diff] [blame] | 14 | #include "chrome/common/webui_url_constants.h" |
[email protected] | 4343769 | 2009-03-04 04:07:34 | [diff] [blame] | 15 | #include "net/base/escape.h" |
Dan Beam | b0dba21 | 2019-04-20 03:25:19 | [diff] [blame] | 16 | #include "net/base/url_util.h" |
[email protected] | 66171ab | 2011-03-03 15:50:07 | [diff] [blame] | 17 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | cd67ed5 | 2013-10-15 01:22:13 | [diff] [blame] | 18 | #include "ui/base/webui/web_ui_util.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 19 | #include "ui/gfx/codec/png_codec.h" |
[email protected] | f08e051 | 2011-06-13 18:10:44 | [diff] [blame] | 20 | #include "ui/gfx/image/image.h" |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 21 | #include "ui/gfx/image/image_skia.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 22 | #include "url/gurl.h" |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 23 | |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap; |
| 27 | |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 28 | // The path used in internal URLs to file icon data. |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 29 | const char kFileIconPath[] = "fileicon"; |
| 30 | |
| 31 | // URL parameter specifying icon size. |
Daniel Bratell | 61e7099 | 2018-06-12 10:32:37 | [diff] [blame] | 32 | const char kIconSizeParameter[] = "iconsize"; |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 33 | |
Dan Beam | b0dba21 | 2019-04-20 03:25:19 | [diff] [blame] | 34 | // URL parameter specifying the file path for which to get an icon. |
| 35 | const char kPathParameter[] = "path"; |
| 36 | |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 37 | // URL parameter specifying scale factor. |
Daniel Bratell | 61e7099 | 2018-06-12 10:32:37 | [diff] [blame] | 38 | const char kScaleFactorParameter[] = "scale"; |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 39 | |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 40 | IconLoader::IconSize SizeStringToIconSize(const std::string& size_string) { |
| 41 | if (size_string == "small") return IconLoader::SMALL; |
| 42 | if (size_string == "large") return IconLoader::LARGE; |
| 43 | // We default to NORMAL if we don't recognize the size_string. Including |
| 44 | // size_string=="normal". |
| 45 | return IconLoader::NORMAL; |
| 46 | } |
| 47 | |
Dan Beam | b0dba21 | 2019-04-20 03:25:19 | [diff] [blame] | 48 | void ParseQueryParams(const std::string& path, |
| 49 | base::FilePath* file_path, |
[email protected] | 33cff1d | 2014-05-21 16:05:48 | [diff] [blame] | 50 | float* scale_factor, |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 51 | IconLoader::IconSize* icon_size) { |
Dan Beam | b0dba21 | 2019-04-20 03:25:19 | [diff] [blame] | 52 | GURL request = GURL(chrome::kChromeUIFileiconURL).Resolve(path); |
| 53 | for (net::QueryIterator it(request); !it.IsAtEnd(); it.Advance()) { |
| 54 | std::string key = it.GetKey(); |
| 55 | if (key == kPathParameter) { |
| 56 | *file_path = base::FilePath::FromUTF8Unsafe(it.GetUnescapedValue()) |
| 57 | .NormalizePathSeparators(); |
| 58 | } else if (key == kIconSizeParameter) { |
| 59 | *icon_size = SizeStringToIconSize(it.GetValue()); |
| 60 | } else if (key == kScaleFactorParameter) { |
| 61 | webui::ParseScaleFactor(it.GetValue(), scale_factor); |
| 62 | } |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 63 | } |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 67 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 68 | FileIconSource::IconRequestDetails::IconRequestDetails() = default; |
vmpstr | b8aacbe | 2016-02-26 02:00:48 | [diff] [blame] | 69 | FileIconSource::IconRequestDetails::IconRequestDetails( |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 70 | IconRequestDetails&& other) = default; |
| 71 | FileIconSource::IconRequestDetails& FileIconSource::IconRequestDetails:: |
| 72 | operator=(IconRequestDetails&& other) = default; |
| 73 | FileIconSource::IconRequestDetails::~IconRequestDetails() = default; |
vmpstr | b8aacbe | 2016-02-26 02:00:48 | [diff] [blame] | 74 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 75 | FileIconSource::FileIconSource() = default; |
| 76 | FileIconSource::~FileIconSource() = default; |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 77 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 78 | void FileIconSource::FetchFileIcon( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 79 | const base::FilePath& path, |
[email protected] | 33cff1d | 2014-05-21 16:05:48 | [diff] [blame] | 80 | float scale_factor, |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 81 | IconLoader::IconSize icon_size, |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 82 | content::URLDataSource::GotDataCallback callback) { |
[email protected] | 66171ab | 2011-03-03 15:50:07 | [diff] [blame] | 83 | IconManager* im = g_browser_process->icon_manager(); |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 84 | gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 85 | |
| 86 | if (icon) { |
[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 87 | scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 88 | gfx::PNGCodec::EncodeBGRASkBitmap( |
Malay Keshav | e8c63be | 2018-10-02 00:12:24 | [diff] [blame] | 89 | icon->ToImageSkia()->GetRepresentation(scale_factor).GetBitmap(), false, |
[email protected] | 33cff1d | 2014-05-21 16:05:48 | [diff] [blame] | 90 | &icon_data->data()); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 91 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 92 | std::move(callback).Run(icon_data.get()); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 93 | } else { |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 94 | // Attach the ChromeURLDataManager request ID to the history request. |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 95 | IconRequestDetails details; |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 96 | details.callback = std::move(callback); |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 97 | details.scale_factor = scale_factor; |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 98 | |
| 99 | // Icon was not in cache, go fetch it slowly. |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 100 | im->LoadIcon(path, icon_size, |
| 101 | base::BindOnce(&FileIconSource::OnFileIconDataAvailable, |
| 102 | base::Unretained(this), std::move(details)), |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 103 | &cancelable_task_tracker_); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 107 | std::string FileIconSource::GetSource() { |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 108 | return kFileIconPath; |
| 109 | } |
| 110 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 111 | void FileIconSource::StartDataRequest( |
Wei-Yin Chen (陳威尹) | 39f4ff3 | 2019-10-22 17:59:09 | [diff] [blame] | 112 | const GURL& url, |
John Abd-El-Malek | 92bf360 | 2019-07-31 02:25:48 | [diff] [blame] | 113 | const content::WebContents::Getter& wc_getter, |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 114 | content::URLDataSource::GotDataCallback callback) { |
Wei-Yin Chen (陳威尹) | 39f4ff3 | 2019-10-22 17:59:09 | [diff] [blame] | 115 | const std::string path = content::URLDataSource::URLToRequestPath(url); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 116 | base::FilePath file_path; |
Dan Beam | b0dba21 | 2019-04-20 03:25:19 | [diff] [blame] | 117 | IconLoader::IconSize icon_size = IconLoader::NORMAL; |
[email protected] | 33cff1d | 2014-05-21 16:05:48 | [diff] [blame] | 118 | float scale_factor = 1.0f; |
Wei-Yin Chen (陳威尹) | 39f4ff3 | 2019-10-22 17:59:09 | [diff] [blame] | 119 | // TODO(crbug/1009127): Make ParseQueryParams take GURL. |
Dan Beam | b0dba21 | 2019-04-20 03:25:19 | [diff] [blame] | 120 | ParseQueryParams(path, &file_path, &scale_factor, &icon_size); |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 121 | FetchFileIcon(file_path, scale_factor, icon_size, std::move(callback)); |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 122 | } |
| 123 | |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 124 | std::string FileIconSource::GetMimeType(const std::string&) { |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 125 | // Rely on image decoder inferring the correct type. |
| 126 | return std::string(); |
| 127 | } |
| 128 | |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 129 | bool FileIconSource::AllowCaching() { |
treib | c782ea5 | 2017-01-25 13:24:40 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 133 | void FileIconSource::OnFileIconDataAvailable(IconRequestDetails details, |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 134 | gfx::Image icon) { |
| 135 | if (!icon.IsEmpty()) { |
[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 136 | scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 137 | gfx::PNGCodec::EncodeBGRASkBitmap( |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 138 | icon.ToImageSkia()->GetRepresentation(details.scale_factor).GetBitmap(), |
Malay Keshav | e8c63be | 2018-10-02 00:12:24 | [diff] [blame] | 139 | false, &icon_data->data()); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 140 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 141 | std::move(details.callback).Run(icon_data.get()); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 142 | } else { |
| 143 | // TODO(glen): send a dummy icon. |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 144 | std::move(details.callback).Run(nullptr); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 145 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 146 | } |