blob: a9e963a94dab7f31d6d1d8e44fe82f499bd52c98 [file] [log] [blame]
[email protected]25cc7502012-01-31 19:33:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]12e14fc2009-02-19 22:25:222// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]fd42ac30f2011-02-27 19:33:365#include "chrome/browser/ui/webui/fileicon_source.h"
[email protected]12e14fc2009-02-19 22:25:226
[email protected]c1896982012-12-05 20:26:177#include "base/bind.h"
[email protected]2041cf342010-02-19 03:15:598#include "base/callback.h"
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/ref_counted_memory.h"
[email protected]1988e1c2013-02-28 20:27:4211#include "base/strings/string_split.h"
[email protected]774cc3c2013-06-07 20:26:4512#include "base/strings/utf_string_conversions.h"
[email protected]12e14fc2009-02-19 22:25:2213#include "chrome/browser/browser_process.h"
Dan Beamb0dba212019-04-20 03:25:1914#include "chrome/common/webui_url_constants.h"
[email protected]43437692009-03-04 04:07:3415#include "net/base/escape.h"
Dan Beamb0dba212019-04-20 03:25:1916#include "net/base/url_util.h"
[email protected]66171ab2011-03-03 15:50:0717#include "third_party/skia/include/core/SkBitmap.h"
[email protected]cd67ed52013-10-15 01:22:1318#include "ui/base/webui/web_ui_util.h"
[email protected]08397d52011-02-05 01:53:3819#include "ui/gfx/codec/png_codec.h"
[email protected]f08e0512011-06-13 18:10:4420#include "ui/gfx/image/image.h"
[email protected]daf19c52012-07-13 19:01:3221#include "ui/gfx/image/image_skia.h"
[email protected]a6483d22013-07-03 22:11:0022#include "url/gurl.h"
[email protected]12e14fc2009-02-19 22:25:2223
[email protected]25cc7502012-01-31 19:33:5524namespace {
25
26typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap;
27
[email protected]12e14fc2009-02-19 22:25:2228// The path used in internal URLs to file icon data.
[email protected]25cc7502012-01-31 19:33:5529const char kFileIconPath[] = "fileicon";
30
31// URL parameter specifying icon size.
Daniel Bratell61e70992018-06-12 10:32:3732const char kIconSizeParameter[] = "iconsize";
[email protected]25cc7502012-01-31 19:33:5533
Dan Beamb0dba212019-04-20 03:25:1934// URL parameter specifying the file path for which to get an icon.
35const char kPathParameter[] = "path";
36
[email protected]daf19c52012-07-13 19:01:3237// URL parameter specifying scale factor.
Daniel Bratell61e70992018-06-12 10:32:3738const char kScaleFactorParameter[] = "scale";
[email protected]daf19c52012-07-13 19:01:3239
[email protected]25cc7502012-01-31 19:33:5540IconLoader::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 Beamb0dba212019-04-20 03:25:1948void ParseQueryParams(const std::string& path,
49 base::FilePath* file_path,
[email protected]33cff1d2014-05-21 16:05:4850 float* scale_factor,
[email protected]daf19c52012-07-13 19:01:3251 IconLoader::IconSize* icon_size) {
Dan Beamb0dba212019-04-20 03:25:1952 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]25cc7502012-01-31 19:33:5563 }
[email protected]25cc7502012-01-31 19:33:5564}
65
66} // namespace
[email protected]12e14fc2009-02-19 22:25:2267
[email protected]33cff1d2014-05-21 16:05:4868FileIconSource::IconRequestDetails::IconRequestDetails() : scale_factor(1.0f) {
[email protected]90d9f1e2013-01-16 02:46:4169}
70
vmpstrb8aacbe2016-02-26 02:00:4871FileIconSource::IconRequestDetails::IconRequestDetails(
72 const IconRequestDetails& other) = default;
73
[email protected]90d9f1e2013-01-16 02:46:4174FileIconSource::IconRequestDetails::~IconRequestDetails() {
75}
76
[email protected]92253622013-01-14 20:40:5077FileIconSource::FileIconSource() {}
[email protected]12e14fc2009-02-19 22:25:2278
[email protected]c1896982012-12-05 20:26:1779FileIconSource::~FileIconSource() {}
[email protected]12e14fc2009-02-19 22:25:2280
[email protected]90d9f1e2013-01-16 02:46:4181void FileIconSource::FetchFileIcon(
[email protected]650b2d52013-02-10 03:41:4582 const base::FilePath& path,
[email protected]33cff1d2014-05-21 16:05:4883 float scale_factor,
[email protected]90d9f1e2013-01-16 02:46:4184 IconLoader::IconSize icon_size,
85 const content::URLDataSource::GotDataCallback& callback) {
[email protected]66171ab2011-03-03 15:50:0786 IconManager* im = g_browser_process->icon_manager();
[email protected]bc0147b2013-04-03 20:50:5987 gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size);
[email protected]12e14fc2009-02-19 22:25:2288
89 if (icon) {
[email protected]ebbccb952012-04-20 09:51:3190 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
[email protected]daf19c52012-07-13 19:01:3291 gfx::PNGCodec::EncodeBGRASkBitmap(
Malay Keshave8c63be2018-10-02 00:12:2492 icon->ToImageSkia()->GetRepresentation(scale_factor).GetBitmap(), false,
[email protected]33cff1d2014-05-21 16:05:4893 &icon_data->data());
[email protected]12e14fc2009-02-19 22:25:2294
[email protected]39790b7f2013-06-03 00:10:5995 callback.Run(icon_data.get());
[email protected]12e14fc2009-02-19 22:25:2296 } else {
[email protected]12e14fc2009-02-19 22:25:2297 // Attach the ChromeURLDataManager request ID to the history request.
[email protected]daf19c52012-07-13 19:01:3298 IconRequestDetails details;
[email protected]90d9f1e2013-01-16 02:46:4199 details.callback = callback;
[email protected]daf19c52012-07-13 19:01:32100 details.scale_factor = scale_factor;
[email protected]c1896982012-12-05 20:26:17101
102 // Icon was not in cache, go fetch it slowly.
103 im->LoadIcon(path,
104 icon_size,
105 base::Bind(&FileIconSource::OnFileIconDataAvailable,
106 base::Unretained(this), details),
107 &cancelable_task_tracker_);
[email protected]12e14fc2009-02-19 22:25:22108 }
109}
110
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:04111std::string FileIconSource::GetSource() {
[email protected]92253622013-01-14 20:40:50112 return kFileIconPath;
113}
114
[email protected]90d9f1e2013-01-16 02:46:41115void FileIconSource::StartDataRequest(
Dan Beamb0dba212019-04-20 03:25:19116 const std::string& path,
John Abd-El-Malek92bf3602019-07-31 02:25:48117 const content::WebContents::Getter& wc_getter,
[email protected]90d9f1e2013-01-16 02:46:41118 const content::URLDataSource::GotDataCallback& callback) {
[email protected]650b2d52013-02-10 03:41:45119 base::FilePath file_path;
Dan Beamb0dba212019-04-20 03:25:19120 IconLoader::IconSize icon_size = IconLoader::NORMAL;
[email protected]33cff1d2014-05-21 16:05:48121 float scale_factor = 1.0f;
Dan Beamb0dba212019-04-20 03:25:19122 ParseQueryParams(path, &file_path, &scale_factor, &icon_size);
[email protected]90d9f1e2013-01-16 02:46:41123 FetchFileIcon(file_path, scale_factor, icon_size, callback);
[email protected]25cc7502012-01-31 19:33:55124}
125
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:04126std::string FileIconSource::GetMimeType(const std::string&) {
[email protected]e4be2dd2010-12-14 00:44:39127 // Rely on image decoder inferring the correct type.
128 return std::string();
129}
130
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:04131bool FileIconSource::AllowCaching() {
treibc782ea52017-01-25 13:24:40132 return false;
133}
134
[email protected]c1896982012-12-05 20:26:17135void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details,
Dana Friedfa14d5f2019-04-21 20:49:36136 gfx::Image icon) {
137 if (!icon.IsEmpty()) {
[email protected]ebbccb952012-04-20 09:51:31138 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
[email protected]daf19c52012-07-13 19:01:32139 gfx::PNGCodec::EncodeBGRASkBitmap(
Dana Friedfa14d5f2019-04-21 20:49:36140 icon.ToImageSkia()->GetRepresentation(details.scale_factor).GetBitmap(),
Malay Keshave8c63be2018-10-02 00:12:24141 false, &icon_data->data());
[email protected]12e14fc2009-02-19 22:25:22142
[email protected]39790b7f2013-06-03 00:10:59143 details.callback.Run(icon_data.get());
[email protected]12e14fc2009-02-19 22:25:22144 } else {
145 // TODO(glen): send a dummy icon.
[email protected]90d9f1e2013-01-16 02:46:41146 details.callback.Run(NULL);
[email protected]12e14fc2009-02-19 22:25:22147 }
[email protected]f0a51fb52009-03-05 12:46:38148}