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