| OLD | NEW |
| 1 // Copyright (c) 2012 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 "base/nix/mime_util_xdg.h" | 5 #include "base/nix/mime_util_xdg.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/environment.h" | 12 #include "base/environment.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/singleton.h" | 17 #include "base/memory/singleton.h" |
| 18 #include "base/nix/xdg_util.h" | 18 #include "base/nix/xdg_util.h" |
| 19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
| 21 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 22 #include "base/third_party/xdg_mime/xdgmime.h" | 22 #include "base/third_party/xdg_mime/xdgmime.h" |
| 23 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
| 24 #include "base/time.h" | 24 #include "base/time.h" |
| 25 | 25 |
| 26 #if defined(TOOLKIT_GTK) | |
| 27 #include <gtk/gtk.h> // NOLINT | |
| 28 | |
| 29 #include "base/message_loop.h" | |
| 30 #endif | |
| 31 | |
| 32 namespace { | 26 namespace { |
| 33 | 27 |
| 34 class IconTheme; | 28 class IconTheme; |
| 35 | 29 |
| 36 // None of the XDG stuff is thread-safe, so serialize all access under | 30 // None of the XDG stuff is thread-safe, so serialize all access under |
| 37 // this lock. | 31 // this lock. |
| 38 base::LazyInstance<base::Lock>::Leaky | 32 base::LazyInstance<base::Lock>::Leaky |
| 39 g_mime_util_xdg_lock = LAZY_INSTANCE_INITIALIZER; | 33 g_mime_util_xdg_lock = LAZY_INSTANCE_INITIALIZER; |
| 40 | 34 |
| 41 class MimeUtilConstants { | 35 class MimeUtilConstants { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 60 IconFormats icon_formats_; | 54 IconFormats icon_formats_; |
| 61 | 55 |
| 62 // Store loaded icon_theme. | 56 // Store loaded icon_theme. |
| 63 IconThemeMap icon_themes_; | 57 IconThemeMap icon_themes_; |
| 64 | 58 |
| 65 // The default theme. | 59 // The default theme. |
| 66 IconTheme* default_themes_[kDefaultThemeNum]; | 60 IconTheme* default_themes_[kDefaultThemeNum]; |
| 67 | 61 |
| 68 base::TimeTicks last_check_time_; | 62 base::TimeTicks last_check_time_; |
| 69 | 63 |
| 70 #if defined(TOOLKIT_GTK) | 64 // The current icon theme, usually set through GTK theme integration. |
| 71 // This is set by DetectGtkTheme(). We cache it so that we can access the | 65 std::string icon_theme_name_; |
| 72 // theme name from threads that aren't allowed to call | |
| 73 // gtk_settings_get_default(). | |
| 74 std::string gtk_theme_name_; | |
| 75 #endif | |
| 76 | 66 |
| 77 private: | 67 private: |
| 78 MimeUtilConstants() { | 68 MimeUtilConstants() { |
| 79 icon_formats_.push_back(".png"); | 69 icon_formats_.push_back(".png"); |
| 80 icon_formats_.push_back(".svg"); | 70 icon_formats_.push_back(".svg"); |
| 81 icon_formats_.push_back(".xpm"); | 71 icon_formats_.push_back(".xpm"); |
| 82 | 72 |
| 83 for (size_t i = 0; i < kDefaultThemeNum; ++i) | 73 for (size_t i = 0; i < kDefaultThemeNum; ++i) |
| 84 default_themes_[i] = NULL; | 74 default_themes_[i] = NULL; |
| 85 } | 75 } |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 kde_default_theme = "default.kde"; | 518 kde_default_theme = "default.kde"; |
| 529 kde_fallback_theme = "crystalsvg"; | 519 kde_fallback_theme = "crystalsvg"; |
| 530 } else { | 520 } else { |
| 531 // KDE 4 | 521 // KDE 4 |
| 532 kde_default_theme = "default.kde4"; | 522 kde_default_theme = "default.kde4"; |
| 533 kde_fallback_theme = "oxygen"; | 523 kde_fallback_theme = "oxygen"; |
| 534 } | 524 } |
| 535 default_themes[1] = IconTheme::LoadTheme(kde_default_theme); | 525 default_themes[1] = IconTheme::LoadTheme(kde_default_theme); |
| 536 default_themes[2] = IconTheme::LoadTheme(kde_fallback_theme); | 526 default_themes[2] = IconTheme::LoadTheme(kde_fallback_theme); |
| 537 } else { | 527 } else { |
| 538 #if defined(TOOLKIT_GTK) | |
| 539 // Assume it's Gnome and use GTK to figure out the theme. | 528 // Assume it's Gnome and use GTK to figure out the theme. |
| 540 default_themes[1] = IconTheme::LoadTheme( | 529 default_themes[1] = IconTheme::LoadTheme( |
| 541 MimeUtilConstants::GetInstance()->gtk_theme_name_); | 530 MimeUtilConstants::GetInstance()->icon_theme_name_); |
| 542 default_themes[2] = IconTheme::LoadTheme("gnome"); | 531 default_themes[2] = IconTheme::LoadTheme("gnome"); |
| 543 #endif | |
| 544 } | 532 } |
| 545 // hicolor needs to be last per icon theme spec. | 533 // hicolor needs to be last per icon theme spec. |
| 546 default_themes[3] = IconTheme::LoadTheme("hicolor"); | 534 default_themes[3] = IconTheme::LoadTheme("hicolor"); |
| 547 | 535 |
| 548 for (size_t i = 0; i < MimeUtilConstants::kDefaultThemeNum; i++) { | 536 for (size_t i = 0; i < MimeUtilConstants::kDefaultThemeNum; i++) { |
| 549 if (default_themes[i] == NULL) | 537 if (default_themes[i] == NULL) |
| 550 continue; | 538 continue; |
| 551 // NULL out duplicate pointers. | 539 // NULL out duplicate pointers. |
| 552 for (size_t j = i + 1; j < MimeUtilConstants::kDefaultThemeNum; j++) { | 540 for (size_t j = i + 1; j < MimeUtilConstants::kDefaultThemeNum; j++) { |
| 553 if (default_themes[j] == default_themes[i]) | 541 if (default_themes[j] == default_themes[i]) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get()); | 581 base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get()); |
| 594 return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str()); | 582 return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str()); |
| 595 } | 583 } |
| 596 | 584 |
| 597 std::string GetDataMimeType(const std::string& data) { | 585 std::string GetDataMimeType(const std::string& data) { |
| 598 base::ThreadRestrictions::AssertIOAllowed(); | 586 base::ThreadRestrictions::AssertIOAllowed(); |
| 599 base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get()); | 587 base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get()); |
| 600 return xdg_mime_get_mime_type_for_data(data.data(), data.length(), NULL); | 588 return xdg_mime_get_mime_type_for_data(data.data(), data.length(), NULL); |
| 601 } | 589 } |
| 602 | 590 |
| 603 #if defined(TOOLKIT_GTK) | 591 void SetIconThemeName(const std::string& name) { |
| 604 void DetectGtkTheme() { | |
| 605 // If the theme name is already loaded, do nothing. Chrome doesn't respond | 592 // If the theme name is already loaded, do nothing. Chrome doesn't respond |
| 606 // to changes in the system theme, so we never need to set this more than | 593 // to changes in the system theme, so we never need to set this more than |
| 607 // once. | 594 // once. |
| 608 if (!MimeUtilConstants::GetInstance()->gtk_theme_name_.empty()) | 595 if (!MimeUtilConstants::GetInstance()->icon_theme_name_.empty()) |
| 609 return; | 596 return; |
| 610 | 597 |
| 611 // We should only be called on the UI thread. | 598 MimeUtilConstants::GetInstance()->icon_theme_name_ = name; |
| 612 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()); | |
| 613 | |
| 614 gchar* gtk_theme_name; | |
| 615 g_object_get(gtk_settings_get_default(), | |
| 616 "gtk-icon-theme-name", | |
| 617 >k_theme_name, NULL); | |
| 618 MimeUtilConstants::GetInstance()->gtk_theme_name_.assign(gtk_theme_name); | |
| 619 g_free(gtk_theme_name); | |
| 620 } | 599 } |
| 621 #endif | |
| 622 | 600 |
| 623 FilePath GetMimeIcon(const std::string& mime_type, size_t size) { | 601 FilePath GetMimeIcon(const std::string& mime_type, size_t size) { |
| 624 base::ThreadRestrictions::AssertIOAllowed(); | 602 base::ThreadRestrictions::AssertIOAllowed(); |
| 625 std::vector<std::string> icon_names; | 603 std::vector<std::string> icon_names; |
| 626 std::string icon_name; | 604 std::string icon_name; |
| 627 FilePath icon_file; | 605 FilePath icon_file; |
| 628 | 606 |
| 629 if (!mime_type.empty()) { | 607 if (!mime_type.empty()) { |
| 630 base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get()); | 608 base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get()); |
| 631 const char *icon = xdg_mime_get_icon(mime_type.c_str()); | 609 const char *icon = xdg_mime_get_icon(mime_type.c_str()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 icon_file = LookupIconInDefaultTheme(icon_names[i], size); | 646 icon_file = LookupIconInDefaultTheme(icon_names[i], size); |
| 669 if (!icon_file.empty()) | 647 if (!icon_file.empty()) |
| 670 return icon_file; | 648 return icon_file; |
| 671 } | 649 } |
| 672 } | 650 } |
| 673 return FilePath(); | 651 return FilePath(); |
| 674 } | 652 } |
| 675 | 653 |
| 676 } // namespace nix | 654 } // namespace nix |
| 677 } // namespace base | 655 } // namespace base |
| OLD | NEW |