fileicon_source.cc revision bb1529ce867d8845a77ec7cdf3e3003ef1771a40
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chrome/browser/ui/webui/fileicon_source.h" 6 7#include "base/basictypes.h" 8#include "base/bind.h" 9#include "base/callback.h" 10#include "base/files/file_path.h" 11#include "base/memory/ref_counted_memory.h" 12#include "base/message_loop/message_loop.h" 13#include "base/strings/string_split.h" 14#include "base/strings/utf_string_conversions.h" 15#include "chrome/browser/browser_process.h" 16#include "grit/generated_resources.h" 17#include "net/base/escape.h" 18#include "third_party/skia/include/core/SkBitmap.h" 19#include "ui/gfx/codec/png_codec.h" 20#include "ui/gfx/image/image.h" 21#include "ui/gfx/image/image_skia.h" 22#include "ui/webui/web_ui_util.h" 23#include "url/gurl.h" 24 25namespace { 26 27typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap; 28 29// The path used in internal URLs to file icon data. 30const char kFileIconPath[] = "fileicon"; 31 32// URL parameter specifying icon size. 33const char kIconSize[] = "iconsize"; 34 35// URL parameter specifying scale factor. 36const char kScaleFactor[] = "scale"; 37 38// Assuming the url is of the form '/path?query', convert the path portion into 39// a FilePath and return the resulting |file_path| and |query|. The path 40// portion may have been encoded using encodeURIComponent(). 41void GetFilePathAndQuery(const std::string& url, 42 base::FilePath* file_path, 43 std::string* query) { 44 // We receive the url with chrome://fileicon/ stripped but GURL expects it. 45 const GURL gurl("chrome://fileicon/" + url); 46 std::string path = net::UnescapeURLComponent( 47 gurl.path().substr(1), (net::UnescapeRule::URL_SPECIAL_CHARS | 48 net::UnescapeRule::SPACES)); 49 50 *file_path = base::FilePath::FromUTF8Unsafe(path); 51 *file_path = file_path->NormalizePathSeparators(); 52 query->assign(gurl.query()); 53} 54 55IconLoader::IconSize SizeStringToIconSize(const std::string& size_string) { 56 if (size_string == "small") return IconLoader::SMALL; 57 if (size_string == "large") return IconLoader::LARGE; 58 // We default to NORMAL if we don't recognize the size_string. Including 59 // size_string=="normal". 60 return IconLoader::NORMAL; 61} 62 63// Simple parser for data on the query. 64void ParseQueryParams(const std::string& query, 65 ui::ScaleFactor* scale_factor, 66 IconLoader::IconSize* icon_size) { 67 typedef std::pair<std::string, std::string> KVPair; 68 std::vector<KVPair> parameters; 69 if (icon_size) 70 *icon_size = IconLoader::NORMAL; 71 if (scale_factor) 72 *scale_factor = ui::SCALE_FACTOR_100P; 73 base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters); 74 for (std::vector<KVPair>::const_iterator iter = parameters.begin(); 75 iter != parameters.end(); ++iter) { 76 if (icon_size && iter->first == kIconSize) 77 *icon_size = SizeStringToIconSize(iter->second); 78 else if (scale_factor && iter->first == kScaleFactor) 79 webui::ParseScaleFactor(iter->second, scale_factor); 80 } 81} 82 83} // namespace 84 85FileIconSource::IconRequestDetails::IconRequestDetails() { 86} 87 88FileIconSource::IconRequestDetails::~IconRequestDetails() { 89} 90 91FileIconSource::FileIconSource() {} 92 93FileIconSource::~FileIconSource() {} 94 95void FileIconSource::FetchFileIcon( 96 const base::FilePath& path, 97 ui::ScaleFactor scale_factor, 98 IconLoader::IconSize icon_size, 99 const content::URLDataSource::GotDataCallback& callback) { 100 IconManager* im = g_browser_process->icon_manager(); 101 gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size); 102 103 if (icon) { 104 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); 105 gfx::PNGCodec::EncodeBGRASkBitmap( 106 icon->ToImageSkia()->GetRepresentation(scale_factor).sk_bitmap(), 107 false, &icon_data->data()); 108 109 callback.Run(icon_data.get()); 110 } else { 111 // Attach the ChromeURLDataManager request ID to the history request. 112 IconRequestDetails details; 113 details.callback = callback; 114 details.scale_factor = scale_factor; 115 116 // Icon was not in cache, go fetch it slowly. 117 im->LoadIcon(path, 118 icon_size, 119 base::Bind(&FileIconSource::OnFileIconDataAvailable, 120 base::Unretained(this), details), 121 &cancelable_task_tracker_); 122 } 123} 124 125std::string FileIconSource::GetSource() const { 126 return kFileIconPath; 127} 128 129void FileIconSource::StartDataRequest( 130 const std::string& url_path, 131 int render_process_id, 132 int render_view_id, 133 const content::URLDataSource::GotDataCallback& callback) { 134 std::string query; 135 base::FilePath file_path; 136 ui::ScaleFactor scale_factor; 137 IconLoader::IconSize icon_size; 138 GetFilePathAndQuery(url_path, &file_path, &query); 139 ParseQueryParams(query, &scale_factor, &icon_size); 140 FetchFileIcon(file_path, scale_factor, icon_size, callback); 141} 142 143std::string FileIconSource::GetMimeType(const std::string&) const { 144 // Rely on image decoder inferring the correct type. 145 return std::string(); 146} 147 148void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details, 149 gfx::Image* icon) { 150 if (icon) { 151 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); 152 gfx::PNGCodec::EncodeBGRASkBitmap( 153 icon->ToImageSkia()->GetRepresentation(details.scale_factor) 154 .sk_bitmap(), 155 false, 156 &icon_data->data()); 157 158 details.callback.Run(icon_data.get()); 159 } else { 160 // TODO(glen): send a dummy icon. 161 details.callback.Run(NULL); 162 } 163} 164