OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/fileicon_source.h" | 5 #include "chrome/browser/ui/webui/fileicon_source.h" |
6 | 6 |
| 7 #include "base/basictypes.h" |
7 #include "base/callback.h" | 8 #include "base/callback.h" |
8 #include "base/file_path.h" | 9 #include "base/file_path.h" |
9 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/string_split.h" |
10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
12 #include "chrome/common/time_format.h" | 14 #include "chrome/common/time_format.h" |
| 15 #include "googleurl/src/gurl.h" |
13 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
14 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
16 #include "ui/gfx/codec/png_codec.h" | 19 #include "ui/gfx/codec/png_codec.h" |
17 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
18 | 21 |
| 22 namespace { |
| 23 |
| 24 typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap; |
| 25 |
19 // The path used in internal URLs to file icon data. | 26 // The path used in internal URLs to file icon data. |
20 static const char kFileIconPath[] = "fileicon"; | 27 const char kFileIconPath[] = "fileicon"; |
| 28 |
| 29 // URL parameter specifying icon size. |
| 30 const char kIconSize[] = "iconsize"; |
| 31 |
| 32 // Assuming the url is of the form '/path?query', convert the path portion into |
| 33 // a FilePath and return the resulting |file_path| and |query|. The path |
| 34 // portion may have been encoded using encodeURIComponent(). |
| 35 void GetFilePathAndQuery(const std::string& url, |
| 36 FilePath* file_path, |
| 37 std::string* query) { |
| 38 // We receive the url with chrome://fileicon/ stripped but GURL expects it. |
| 39 const GURL gurl("chrome://fileicon/" + url); |
| 40 std::string path = |
| 41 net::UnescapeURLComponent(gurl.path().substr(1), |
| 42 (net::UnescapeRule::URL_SPECIAL_CHARS | |
| 43 net::UnescapeRule::SPACES)); |
| 44 #if defined(OS_WIN) |
| 45 // The path we receive has the wrong slashes and escaping for what we need; |
| 46 // this only appears to matter for getting icons from .exe files. |
| 47 std::replace(path.begin(), path.end(), '/', '\\'); |
| 48 *file_path = FilePath(UTF8ToWide(path)); |
| 49 #elif defined(OS_POSIX) |
| 50 // The correct encoding on Linux may not actually be UTF8. |
| 51 *file_path = FilePath(path); |
| 52 #endif |
| 53 query->assign(gurl.query()); |
| 54 } |
| 55 |
| 56 IconLoader::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. |
| 65 IconLoader::IconSize QueryToIconSize(const std::string& query) { |
| 66 typedef std::pair<std::string, std::string> KVPair; |
| 67 std::vector<KVPair> parameters; |
| 68 base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters); |
| 69 for (std::vector<KVPair>::const_iterator iter = parameters.begin(); |
| 70 iter != parameters.end(); ++iter) { |
| 71 if (iter->first == kIconSize) |
| 72 return SizeStringToIconSize(iter->second); |
| 73 } |
| 74 return IconLoader::NORMAL; |
| 75 } |
| 76 |
| 77 } // namespace |
21 | 78 |
22 FileIconSource::FileIconSource() | 79 FileIconSource::FileIconSource() |
23 : DataSource(kFileIconPath, MessageLoop::current()) {} | 80 : DataSource(kFileIconPath, MessageLoop::current()) {} |
24 | 81 |
25 FileIconSource::~FileIconSource() { | 82 FileIconSource::~FileIconSource() { |
26 cancelable_consumer_.CancelAllRequests(); | 83 cancelable_consumer_.CancelAllRequests(); |
27 } | 84 } |
28 | 85 |
29 void FileIconSource::StartDataRequest(const std::string& path, | 86 void FileIconSource::FetchFileIcon(const FilePath& path, |
30 bool is_incognito, | 87 IconLoader::IconSize icon_size, |
31 int request_id) { | 88 int request_id) { |
32 std::string escaped_path = net::UnescapeURLComponent(path, | |
33 net::UnescapeRule::SPACES); | |
34 #if defined(OS_WIN) | |
35 // The path we receive has the wrong slashes and escaping for what we need; | |
36 // this only appears to matter for getting icons from .exe files. | |
37 std::replace(escaped_path.begin(), escaped_path.end(), '/', '\\'); | |
38 FilePath escaped_filepath(UTF8ToWide(escaped_path)); | |
39 #elif defined(OS_POSIX) | |
40 // The correct encoding on Linux may not actually be UTF8. | |
41 FilePath escaped_filepath(escaped_path); | |
42 #endif | |
43 | |
44 IconManager* im = g_browser_process->icon_manager(); | 89 IconManager* im = g_browser_process->icon_manager(); |
45 gfx::Image* icon = im->LookupIcon(escaped_filepath, IconLoader::NORMAL); | 90 gfx::Image* icon = im->LookupIcon(path, icon_size); |
46 | 91 |
47 if (icon) { | 92 if (icon) { |
48 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); | 93 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); |
49 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); | 94 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); |
50 | 95 |
51 SendResponse(request_id, icon_data); | 96 SendResponse(request_id, icon_data); |
52 } else { | 97 } else { |
53 // Icon was not in cache, go fetch it slowly. | 98 // Icon was not in cache, go fetch it slowly. |
54 IconManager::Handle h = im->LoadIcon(escaped_filepath, | 99 IconManager::Handle h = im->LoadIcon( |
55 IconLoader::NORMAL, | 100 path, icon_size, &cancelable_consumer_, |
56 &cancelable_consumer_, | |
57 base::Bind(&FileIconSource::OnFileIconDataAvailable, | 101 base::Bind(&FileIconSource::OnFileIconDataAvailable, |
58 base::Unretained(this))); | 102 base::Unretained(this))); |
59 | 103 |
60 // Attach the ChromeURLDataManager request ID to the history request. | 104 // Attach the ChromeURLDataManager request ID to the history request. |
61 cancelable_consumer_.SetClientData(im, h, request_id); | 105 cancelable_consumer_.SetClientData(im, h, request_id); |
62 } | 106 } |
63 } | 107 } |
64 | 108 |
| 109 void FileIconSource::StartDataRequest(const std::string& url_path, |
| 110 bool is_incognito, |
| 111 int request_id) { |
| 112 std::string query; |
| 113 FilePath file_path; |
| 114 GetFilePathAndQuery(url_path, &file_path, &query); |
| 115 FetchFileIcon(file_path, QueryToIconSize(query), request_id); |
| 116 } |
| 117 |
65 std::string FileIconSource::GetMimeType(const std::string&) const { | 118 std::string FileIconSource::GetMimeType(const std::string&) const { |
66 // Rely on image decoder inferring the correct type. | 119 // Rely on image decoder inferring the correct type. |
67 return std::string(); | 120 return std::string(); |
68 } | 121 } |
69 | 122 |
70 void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle, | 123 void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle, |
71 gfx::Image* icon) { | 124 gfx::Image* icon) { |
72 IconManager* im = g_browser_process->icon_manager(); | 125 IconManager* im = g_browser_process->icon_manager(); |
73 int request_id = cancelable_consumer_.GetClientData(im, handle); | 126 int request_id = cancelable_consumer_.GetClientData(im, handle); |
74 | 127 |
75 if (icon) { | 128 if (icon) { |
76 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); | 129 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); |
77 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); | 130 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); |
78 | 131 |
79 SendResponse(request_id, icon_data); | 132 SendResponse(request_id, icon_data); |
80 } else { | 133 } else { |
81 // TODO(glen): send a dummy icon. | 134 // TODO(glen): send a dummy icon. |
82 SendResponse(request_id, NULL); | 135 SendResponse(request_id, NULL); |
83 } | 136 } |
84 } | 137 } |
OLD | NEW |