Port PluginObject fix downstream. See http://trac.webkit.org/changeset/61415/ for...
[chromium-blink-merge.git] / base / mime_util_xdg.cc
blob3f6227e0189f0d4f45dd56a4cd03e18fa6672956
1 // Copyright (c) 2009 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.
5 #include "base/mime_util.h"
7 #include <gtk/gtk.h>
8 #include <sys/time.h>
9 #include <time.h>
11 #include <cstdlib>
12 #include <list>
13 #include <map>
14 #include <vector>
16 #include "base/file_util.h"
17 #include "base/logging.h"
18 #include "base/message_loop.h"
19 #include "base/scoped_ptr.h"
20 #include "base/singleton.h"
21 #include "base/string_util.h"
22 #include "base/third_party/xdg_mime/xdgmime.h"
24 namespace {
26 class IconTheme;
28 class MimeUtilConstants {
29 public:
31 // In seconds, specified by icon theme specs.
32 const int kUpdateInterval;
34 // Store icon directories and their mtimes.
35 std::map<FilePath, int>* icon_dirs_;
37 // Store icon formats.
38 std::vector<std::string> icon_formats_;
40 // Store loaded icon_theme.
41 std::map<std::string, IconTheme*>* icon_themes_;
43 static const size_t kDefaultThemeNum = 4;
45 // The default theme.
46 IconTheme* default_themes_[kDefaultThemeNum];
48 time_t last_check_time_;
50 // This is set by DetectGtkTheme(). We cache it so that we can access the
51 // theme name from threads that aren't allowed to call
52 // gtk_settings_get_default().
53 std::string gtk_theme_name_;
55 private:
56 MimeUtilConstants()
57 : kUpdateInterval(5),
58 icon_dirs_(NULL),
59 icon_themes_(NULL),
60 last_check_time_(0) {
61 icon_formats_.push_back(".png");
62 icon_formats_.push_back(".svg");
63 icon_formats_.push_back(".xpm");
65 for (size_t i = 0; i < kDefaultThemeNum; ++i)
66 default_themes_[i] = NULL;
68 ~MimeUtilConstants();
70 friend struct DefaultSingletonTraits<MimeUtilConstants>;
72 DISALLOW_COPY_AND_ASSIGN(MimeUtilConstants);
75 // IconTheme represents an icon theme as defined by the xdg icon theme spec.
76 // Example themes on GNOME include 'Human' and 'Mist'.
77 // Example themes on KDE include 'crystalsvg' and 'kdeclassic'.
78 class IconTheme {
79 public:
80 // A theme consists of multiple sub-directories, like '32x32' and 'scalable'.
81 class SubDirInfo {
82 public:
83 // See spec for details.
84 enum Type {
85 Fixed,
86 Scalable,
87 Threshold
89 SubDirInfo()
90 : size(0),
91 type(Threshold),
92 max_size(0),
93 min_size(0),
94 threshold(2) {
96 size_t size; // Nominal size of the icons in this directory.
97 Type type; // Type of the icon size.
98 size_t max_size; // Maximum size that the icons can be scaled to.
99 size_t min_size; // Minimum size that the icons can be scaled to.
100 size_t threshold; // Maximum difference from desired size. 2 by default.
103 explicit IconTheme(const std::string& name);
105 ~IconTheme() {
106 delete[] info_array_;
109 // Returns the path to an icon with the name |icon_name| and a size of |size|
110 // pixels. If the icon does not exist, but |inherits| is true, then look for
111 // the icon in the parent theme.
112 FilePath GetIconPath(const std::string& icon_name, int size, bool inherits);
114 // Load a theme with the name |theme_name| into memory. Returns null if theme
115 // is invalid.
116 static IconTheme* LoadTheme(const std::string& theme_name);
118 private:
119 // Returns the path to an icon with the name |icon_name| in |subdir|.
120 FilePath GetIconPathUnderSubdir(const std::string& icon_name,
121 const std::string& subdir);
123 // Whether the theme loaded properly.
124 bool IsValid() {
125 return index_theme_loaded_;
128 // Read and parse |file| which is usually named 'index.theme' per theme spec.
129 bool LoadIndexTheme(const FilePath& file);
131 // Checks to see if the icons in |info| matches |size| (in pixels). Returns
132 // 0 if they match, or the size difference in pixels.
133 size_t MatchesSize(SubDirInfo* info, size_t size);
135 // Yet another function to read a line.
136 std::string ReadLine(FILE* fp);
138 // Set directories to search for icons to the comma-separated list |dirs|.
139 bool SetDirectories(const std::string& dirs);
141 bool index_theme_loaded_; // True if an instance is properly loaded.
142 // store the scattered directories of this theme.
143 std::list<FilePath> dirs_;
145 // store the subdirs of this theme and array index of |info_array_|.
146 std::map<std::string, int> subdirs_;
147 SubDirInfo* info_array_; // List of sub-directories.
148 std::string inherits_; // Name of the theme this one inherits from.
151 IconTheme::IconTheme(const std::string& name)
152 : index_theme_loaded_(false),
153 info_array_(NULL) {
154 // Iterate on all icon directories to find directories of the specified
155 // theme and load the first encountered index.theme.
156 std::map<FilePath, int>::iterator iter;
157 FilePath theme_path;
158 std::map<FilePath, int>* icon_dirs =
159 Singleton<MimeUtilConstants>::get()->icon_dirs_;
160 for (iter = icon_dirs->begin(); iter != icon_dirs->end(); ++iter) {
161 theme_path = iter->first.Append(name);
162 if (!file_util::DirectoryExists(theme_path))
163 continue;
164 FilePath theme_index = theme_path.Append("index.theme");
165 if (!index_theme_loaded_ && file_util::PathExists(theme_index)) {
166 if (!LoadIndexTheme(theme_index))
167 return;
168 index_theme_loaded_ = true;
170 dirs_.push_back(theme_path);
174 FilePath IconTheme::GetIconPath(const std::string& icon_name, int size,
175 bool inherits) {
176 std::map<std::string, int>::iterator subdir_iter;
177 FilePath icon_path;
179 for (subdir_iter = subdirs_.begin();
180 subdir_iter != subdirs_.end();
181 ++subdir_iter) {
182 SubDirInfo* info = &info_array_[subdir_iter->second];
183 if (MatchesSize(info, size) == 0) {
184 icon_path = GetIconPathUnderSubdir(icon_name, subdir_iter->first);
185 if (!icon_path.empty())
186 return icon_path;
189 // Now looking for the mostly matched.
190 int min_delta_seen = 9999;
192 for (subdir_iter = subdirs_.begin();
193 subdir_iter != subdirs_.end();
194 ++subdir_iter) {
195 SubDirInfo* info = &info_array_[subdir_iter->second];
196 int delta = abs(MatchesSize(info, size));
197 if (delta < min_delta_seen) {
198 FilePath path = GetIconPathUnderSubdir(icon_name, subdir_iter->first);
199 if (!path.empty()) {
200 min_delta_seen = delta;
201 icon_path = path;
206 if (!icon_path.empty() || !inherits || inherits_ == "")
207 return icon_path;
209 IconTheme* theme = LoadTheme(inherits_);
210 // Inheriting from itself means the theme is buggy but we shouldn't crash.
211 if (theme && theme != this)
212 return theme->GetIconPath(icon_name, size, inherits);
213 else
214 return FilePath();
217 IconTheme* IconTheme::LoadTheme(const std::string& theme_name) {
218 scoped_ptr<IconTheme> theme;
219 std::map<std::string, IconTheme*>* icon_themes =
220 Singleton<MimeUtilConstants>::get()->icon_themes_;
221 if (icon_themes->find(theme_name) != icon_themes->end()) {
222 theme.reset((*icon_themes)[theme_name]);
223 } else {
224 theme.reset(new IconTheme(theme_name));
225 if (!theme->IsValid())
226 theme.reset();
227 (*icon_themes)[theme_name] = theme.get();
229 return theme.release();
232 FilePath IconTheme::GetIconPathUnderSubdir(const std::string& icon_name,
233 const std::string& subdir) {
234 FilePath icon_path;
235 std::list<FilePath>::iterator dir_iter;
236 std::vector<std::string>* icon_formats =
237 &Singleton<MimeUtilConstants>::get()->icon_formats_;
238 for (dir_iter = dirs_.begin(); dir_iter != dirs_.end(); ++dir_iter) {
239 for (size_t i = 0; i < icon_formats->size(); ++i) {
240 icon_path = dir_iter->Append(subdir);
241 icon_path = icon_path.Append(icon_name + (*icon_formats)[i]);
242 if (file_util::PathExists(icon_path))
243 return icon_path;
246 return FilePath();
249 bool IconTheme::LoadIndexTheme(const FilePath& file) {
250 FILE* fp = file_util::OpenFile(file, "r");
251 SubDirInfo* current_info = NULL;
252 if (!fp)
253 return false;
255 // Read entries.
256 while (!feof(fp) && !ferror(fp)) {
257 std::string buf = ReadLine(fp);
258 if (buf == "")
259 break;
261 std::string entry;
262 TrimWhitespaceASCII(buf, TRIM_ALL, &entry);
263 if (entry.length() == 0 || entry[0] == '#') {
264 // Blank line or Comment.
265 continue;
266 } else if (entry[0] == '[' && info_array_) {
267 current_info = NULL;
268 std::string subdir = entry.substr(1, entry.length() - 2);
269 if (subdirs_.find(subdir) != subdirs_.end())
270 current_info = &info_array_[subdirs_[subdir]];
273 std::string key, value;
274 std::vector<std::string> r;
275 SplitStringDontTrim(entry, '=', &r);
276 if (r.size() < 2)
277 continue;
279 TrimWhitespaceASCII(r[0], TRIM_ALL, &key);
280 for (size_t i = 1; i < r.size(); i++)
281 value.append(r[i]);
282 TrimWhitespaceASCII(value, TRIM_ALL, &value);
284 if (current_info) {
285 if (key == "Size") {
286 current_info->size = atoi(value.c_str());
287 } else if (key == "Type") {
288 if (value == "Fixed")
289 current_info->type = SubDirInfo::Fixed;
290 else if (value == "Scalable")
291 current_info->type = SubDirInfo::Scalable;
292 else if (value == "Threshold")
293 current_info->type = SubDirInfo::Threshold;
294 } else if (key == "MaxSize") {
295 current_info->max_size = atoi(value.c_str());
296 } else if (key == "MinSize") {
297 current_info->min_size = atoi(value.c_str());
298 } else if (key == "Threshold") {
299 current_info->threshold = atoi(value.c_str());
301 } else {
302 if (key.compare("Directories") == 0 && !info_array_) {
303 if (!SetDirectories(value)) break;
304 } else if (key.compare("Inherits") == 0) {
305 if (value != "hicolor")
306 inherits_ = value;
311 file_util::CloseFile(fp);
312 return info_array_ != NULL;
315 size_t IconTheme::MatchesSize(SubDirInfo* info, size_t size) {
316 if (info->type == SubDirInfo::Fixed) {
317 return size - info->size;
318 } else if (info->type == SubDirInfo::Scalable) {
319 if (size >= info->min_size && size <= info->max_size) {
320 return 0;
321 } else {
322 return abs(size - info->min_size) < abs(size - info->max_size) ?
323 (size - info->min_size) : (size - info->max_size);
325 } else {
326 if (size >= info->size - info->threshold &&
327 size <= info->size + info->threshold) {
328 return 0;
329 } else {
330 return abs(size - info->size - info->threshold) <
331 abs(size - info->size + info->threshold)
332 ? size - info->size - info->threshold
333 : size - info->size + info->threshold;
338 std::string IconTheme::ReadLine(FILE* fp) {
339 if (!fp)
340 return "";
342 std::string result = "";
343 const size_t kBufferSize = 100;
344 char buffer[kBufferSize];
345 while ((fgets(buffer, kBufferSize - 1, fp)) != NULL) {
346 result += buffer;
347 size_t len = result.length();
348 if (len == 0)
349 break;
350 char end = result[len - 1];
351 if (end == '\n' || end == '\0')
352 break;
355 return result;
358 bool IconTheme::SetDirectories(const std::string& dirs) {
359 int num = 0;
360 std::string::size_type pos = 0, epos;
361 std::string dir;
362 while ((epos = dirs.find(',', pos)) != std::string::npos) {
363 TrimWhitespaceASCII(dirs.substr(pos, epos - pos), TRIM_ALL, &dir);
364 if (dir.length() == 0) {
365 LOG(WARNING) << "Invalid index.theme: blank subdir";
366 return false;
368 subdirs_[dir] = num++;
369 pos = epos + 1;
371 TrimWhitespaceASCII(dirs.substr(pos), TRIM_ALL, &dir);
372 if (dir.length() == 0) {
373 LOG(WARNING) << "Invalid index.theme: blank subdir";
374 return false;
376 subdirs_[dir] = num++;
377 info_array_ = new SubDirInfo[num];
378 return true;
381 // Make sure |dir| exists and add it to the list of icon directories.
382 void TryAddIconDir(const FilePath& dir) {
383 if (!file_util::DirectoryExists(dir))
384 return;
385 (*Singleton<MimeUtilConstants>::get()->icon_dirs_)[dir] = 0;
388 // For a xdg directory |dir|, add the appropriate icon sub-directories.
389 void AddXDGDataDir(const FilePath& dir) {
390 if (!file_util::DirectoryExists(dir))
391 return;
392 TryAddIconDir(dir.Append("icons"));
393 TryAddIconDir(dir.Append("pixmaps"));
396 // Add all the xdg icon directories.
397 void InitIconDir() {
398 Singleton<MimeUtilConstants>::get()->icon_dirs_->clear();
399 const char* home = getenv("HOME");
400 if (home) {
401 FilePath legacy_data_dir(home);
402 legacy_data_dir = legacy_data_dir.AppendASCII(".icons");
403 if (file_util::DirectoryExists(legacy_data_dir))
404 TryAddIconDir(legacy_data_dir);
406 const char* env = getenv("XDG_DATA_HOME");
407 if (env) {
408 AddXDGDataDir(FilePath(env));
409 } else if (home) {
410 FilePath local_data_dir(home);
411 local_data_dir = local_data_dir.AppendASCII(".local");
412 local_data_dir = local_data_dir.AppendASCII("share");
413 AddXDGDataDir(local_data_dir);
416 env = getenv("XDG_DATA_DIRS");
417 if (!env) {
418 AddXDGDataDir(FilePath("/usr/local/share"));
419 AddXDGDataDir(FilePath("/usr/share"));
420 } else {
421 std::string xdg_data_dirs = env;
422 std::string::size_type pos = 0, epos;
423 while ((epos = xdg_data_dirs.find(':', pos)) != std::string::npos) {
424 AddXDGDataDir(FilePath(xdg_data_dirs.substr(pos, epos - pos)));
425 pos = epos + 1;
427 AddXDGDataDir(FilePath(xdg_data_dirs.substr(pos)));
431 // Per xdg theme spec, we should check the icon directories every so often for
432 // newly added icons. This isn't quite right.
433 void EnsureUpdated() {
434 struct timeval t;
435 gettimeofday(&t, NULL);
436 time_t now = t.tv_sec;
437 MimeUtilConstants* constants = Singleton<MimeUtilConstants>::get();
439 if (constants->last_check_time_ == 0) {
440 constants->icon_dirs_ = new std::map<FilePath, int>;
441 constants->icon_themes_ = new std::map<std::string, IconTheme*>;
442 InitIconDir();
443 constants->last_check_time_ = now;
444 } else {
445 // TODO(thestig): something changed. start over. Upstream fix to Google
446 // Gadgets for Linux.
447 if (now > constants->last_check_time_ + constants->kUpdateInterval) {
452 // Find a fallback icon if we cannot find it in the default theme.
453 FilePath LookupFallbackIcon(const std::string& icon_name) {
454 FilePath icon;
455 MimeUtilConstants* constants = Singleton<MimeUtilConstants>::get();
456 std::map<FilePath, int>::iterator iter;
457 std::map<FilePath, int>* icon_dirs = constants->icon_dirs_;
458 std::vector<std::string>* icon_formats = &constants->icon_formats_;
459 for (iter = icon_dirs->begin(); iter != icon_dirs->end(); ++iter) {
460 for (size_t i = 0; i < icon_formats->size(); ++i) {
461 icon = iter->first.Append(icon_name + (*icon_formats)[i]);
462 if (file_util::PathExists(icon))
463 return icon;
466 return FilePath();
469 // Initialize the list of default themes.
470 void InitDefaultThemes() {
471 IconTheme** default_themes =
472 Singleton<MimeUtilConstants>::get()->default_themes_;
474 char* env = getenv("KDE_FULL_SESSION");
475 if (env) {
476 // KDE
477 std::string kde_default_theme;
478 std::string kde_fallback_theme;
480 // TODO(thestig): Figure out how to get the current icon theme on KDE.
481 // Setting stored in ~/.kde/share/config/kdeglobals under Icons -> Theme.
482 default_themes[0] = NULL;
484 // Try some reasonable defaults for KDE.
485 env = getenv("KDE_SESSION_VERSION");
486 if (!env || env[0] != '4') {
487 // KDE 3
488 kde_default_theme = "default.kde";
489 kde_fallback_theme = "crystalsvg";
490 } else {
491 // KDE 4
492 kde_default_theme = "default.kde4";
493 kde_fallback_theme = "oxygen";
495 default_themes[1] = IconTheme::LoadTheme(kde_default_theme);
496 default_themes[2] = IconTheme::LoadTheme(kde_fallback_theme);
497 } else {
498 // Assume it's Gnome and use GTK to figure out the theme.
499 default_themes[1] = IconTheme::LoadTheme(
500 Singleton<MimeUtilConstants>::get()->gtk_theme_name_);
501 default_themes[2] = IconTheme::LoadTheme("gnome");
503 // hicolor needs to be last per icon theme spec.
504 default_themes[3] = IconTheme::LoadTheme("hicolor");
506 for (size_t i = 0; i < MimeUtilConstants::kDefaultThemeNum; i++) {
507 if (default_themes[i] == NULL)
508 continue;
509 // NULL out duplicate pointers.
510 for (size_t j = i + 1; j < MimeUtilConstants::kDefaultThemeNum; j++) {
511 if (default_themes[j] == default_themes[i])
512 default_themes[j] = NULL;
517 // Try to find an icon with the name |icon_name| that's |size| pixels.
518 FilePath LookupIconInDefaultTheme(const std::string& icon_name, int size) {
519 EnsureUpdated();
520 MimeUtilConstants* constants = Singleton<MimeUtilConstants>::get();
521 std::map<std::string, IconTheme*>* icon_themes = constants->icon_themes_;
522 if (icon_themes->size() == 0)
523 InitDefaultThemes();
525 FilePath icon_path;
526 IconTheme** default_themes = constants->default_themes_;
527 for (size_t i = 0; i < MimeUtilConstants::kDefaultThemeNum; i++) {
528 if (default_themes[i]) {
529 icon_path = default_themes[i]->GetIconPath(icon_name, size, true);
530 if (!icon_path.empty())
531 return icon_path;
534 return LookupFallbackIcon(icon_name);
537 MimeUtilConstants::~MimeUtilConstants() {
538 delete icon_dirs_;
539 delete icon_themes_;
540 for (size_t i = 0; i < kDefaultThemeNum; i++)
541 delete default_themes_[i];
544 } // namespace
546 namespace mime_util {
548 std::string GetFileMimeType(const FilePath& filepath) {
549 return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str());
552 std::string GetDataMimeType(const std::string& data) {
553 return xdg_mime_get_mime_type_for_data(data.data(), data.length(), NULL);
556 void DetectGtkTheme() {
557 // If the theme name is already loaded, do nothing. Chrome doesn't respond
558 // to changes in the system theme, so we never need to set this more than
559 // once.
560 if (!Singleton<MimeUtilConstants>::get()->gtk_theme_name_.empty())
561 return;
563 // We should only be called on the UI thread.
564 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
566 gchar* gtk_theme_name;
567 g_object_get(gtk_settings_get_default(),
568 "gtk-icon-theme-name",
569 &gtk_theme_name, NULL);
570 Singleton<MimeUtilConstants>::get()->gtk_theme_name_.assign(gtk_theme_name);
571 g_free(gtk_theme_name);
574 FilePath GetMimeIcon(const std::string& mime_type, size_t size) {
575 std::vector<std::string> icon_names;
576 std::string icon_name;
577 FilePath icon_file;
579 const char* icon = xdg_mime_get_icon(mime_type.c_str());
580 icon_name = std::string(icon ? icon : "");
581 if (icon_name.length())
582 icon_names.push_back(icon_name);
584 // For text/plain, try text-plain.
585 icon_name = mime_type;
586 for (size_t i = icon_name.find('/', 0); i != std::string::npos;
587 i = icon_name.find('/', i + 1)) {
588 icon_name[i] = '-';
590 icon_names.push_back(icon_name);
591 // Also try gnome-mime-text-plain.
592 icon_names.push_back("gnome-mime-" + icon_name);
594 // Try "deb" for "application/x-deb" in KDE 3.
595 icon_name = mime_type.substr(mime_type.find("/x-") + 3);
596 icon_names.push_back(icon_name);
598 // Try generic name like text-x-generic.
599 icon_name = mime_type.substr(0, mime_type.find('/')) + "-x-generic";
600 icon_names.push_back(icon_name);
602 // Last resort
603 icon_names.push_back("unknown");
605 for (size_t i = 0; i < icon_names.size(); i++) {
606 if (icon_names[i][0] == '/') {
607 icon_file = FilePath(icon_names[i]);
608 if (file_util::PathExists(icon_file))
609 return icon_file;
610 } else {
611 icon_file = LookupIconInDefaultTheme(icon_names[i], size);
612 if (!icon_file.empty())
613 return icon_file;
616 return FilePath();
619 } // namespace mime_util