This source file includes following definitions.
- GetFilePathAndQuery
- SizeStringToIconSize
- ParseQueryParams
- FetchFileIcon
- GetSource
- StartDataRequest
- GetMimeType
- OnFileIconDataAvailable
#include "chrome/browser/ui/webui/fileicon_source.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted_memory.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "grit/generated_resources.h"
#include "net/base/escape.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"
namespace {
typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap;
const char kFileIconPath[] = "fileicon";
const char kIconSize[] = "iconsize";
const char kScaleFactor[] = "scale";
void GetFilePathAndQuery(const std::string& url,
base::FilePath* file_path,
std::string* query) {
const GURL gurl("chrome://fileicon/" + url);
std::string path = net::UnescapeURLComponent(
gurl.path().substr(1), (net::UnescapeRule::URL_SPECIAL_CHARS |
net::UnescapeRule::SPACES));
*file_path = base::FilePath::FromUTF8Unsafe(path);
*file_path = file_path->NormalizePathSeparators();
query->assign(gurl.query());
}
IconLoader::IconSize SizeStringToIconSize(const std::string& size_string) {
if (size_string == "small") return IconLoader::SMALL;
if (size_string == "large") return IconLoader::LARGE;
return IconLoader::NORMAL;
}
void ParseQueryParams(const std::string& query,
ui::ScaleFactor* scale_factor,
IconLoader::IconSize* icon_size) {
typedef std::pair<std::string, std::string> KVPair;
std::vector<KVPair> parameters;
if (icon_size)
*icon_size = IconLoader::NORMAL;
if (scale_factor)
*scale_factor = ui::SCALE_FACTOR_100P;
base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters);
for (std::vector<KVPair>::const_iterator iter = parameters.begin();
iter != parameters.end(); ++iter) {
if (icon_size && iter->first == kIconSize)
*icon_size = SizeStringToIconSize(iter->second);
else if (scale_factor && iter->first == kScaleFactor)
webui::ParseScaleFactor(iter->second, scale_factor);
}
}
}
FileIconSource::IconRequestDetails::IconRequestDetails() {
}
FileIconSource::IconRequestDetails::~IconRequestDetails() {
}
FileIconSource::FileIconSource() {}
FileIconSource::~FileIconSource() {}
void FileIconSource::FetchFileIcon(
const base::FilePath& path,
ui::ScaleFactor scale_factor,
IconLoader::IconSize icon_size,
const content::URLDataSource::GotDataCallback& callback) {
IconManager* im = g_browser_process->icon_manager();
gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size);
if (icon) {
scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
gfx::PNGCodec::EncodeBGRASkBitmap(
icon->ToImageSkia()->GetRepresentation(
ui::GetImageScale(scale_factor)).sk_bitmap(),
false, &icon_data->data());
callback.Run(icon_data.get());
} else {
IconRequestDetails details;
details.callback = callback;
details.scale_factor = scale_factor;
im->LoadIcon(path,
icon_size,
base::Bind(&FileIconSource::OnFileIconDataAvailable,
base::Unretained(this), details),
&cancelable_task_tracker_);
}
}
std::string FileIconSource::GetSource() const {
return kFileIconPath;
}
void FileIconSource::StartDataRequest(
const std::string& url_path,
int render_process_id,
int render_frame_id,
const content::URLDataSource::GotDataCallback& callback) {
std::string query;
base::FilePath file_path;
ui::ScaleFactor scale_factor;
IconLoader::IconSize icon_size;
GetFilePathAndQuery(url_path, &file_path, &query);
ParseQueryParams(query, &scale_factor, &icon_size);
FetchFileIcon(file_path, scale_factor, icon_size, callback);
}
std::string FileIconSource::GetMimeType(const std::string&) const {
return std::string();
}
void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details,
gfx::Image* icon) {
if (icon) {
scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
gfx::PNGCodec::EncodeBGRASkBitmap(
icon->ToImageSkia()->GetRepresentation(
ui::GetImageScale(details.scale_factor)).sk_bitmap(),
false,
&icon_data->data());
details.callback.Run(icon_data.get());
} else {
details.callback.Run(NULL);
}
}