| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/mime_util.h" | 5 #include "base/mime_util.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 #include <sys/time.h> | 8 #include <sys/time.h> |
| 9 #include <time.h> | 9 #include <time.h> |
| 10 | 10 |
| 11 #include <cstdlib> | 11 #include <cstdlib> |
| 12 #include <list> | 12 #include <list> |
| 13 #include <map> | 13 #include <map> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
| 19 #include "base/scoped_ptr.h" | 19 #include "base/scoped_ptr.h" |
| 20 #include "base/singleton.h" | 20 #include "base/singleton.h" |
| 21 #include "base/string_split.h" | 21 #include "base/string_split.h" |
| 22 #include "base/string_util.h" | 22 #include "base/string_util.h" |
| 23 #include "base/synchronization/lock.h" |
| 23 #include "base/third_party/xdg_mime/xdgmime.h" | 24 #include "base/third_party/xdg_mime/xdgmime.h" |
| 24 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 29 // None of the XDG stuff is thread-safe, so serialize all accesss under |
| 30 // this lock. |
| 31 base::Lock g_mime_util_xdg_lock; |
| 32 |
| 28 class IconTheme; | 33 class IconTheme; |
| 29 | 34 |
| 30 class MimeUtilConstants { | 35 class MimeUtilConstants { |
| 31 public: | 36 public: |
| 32 static MimeUtilConstants* GetInstance() { | 37 static MimeUtilConstants* GetInstance() { |
| 33 return Singleton<MimeUtilConstants>::get(); | 38 return Singleton<MimeUtilConstants>::get(); |
| 34 } | 39 } |
| 35 | 40 |
| 36 // In seconds, specified by icon theme specs. | 41 // In seconds, specified by icon theme specs. |
| 37 const int kUpdateInterval; | 42 const int kUpdateInterval; |
| (...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 for (size_t i = 0; i < kDefaultThemeNum; i++) | 551 for (size_t i = 0; i < kDefaultThemeNum; i++) |
| 547 delete default_themes_[i]; | 552 delete default_themes_[i]; |
| 548 } | 553 } |
| 549 | 554 |
| 550 } // namespace | 555 } // namespace |
| 551 | 556 |
| 552 namespace mime_util { | 557 namespace mime_util { |
| 553 | 558 |
| 554 std::string GetFileMimeType(const FilePath& filepath) { | 559 std::string GetFileMimeType(const FilePath& filepath) { |
| 555 base::ThreadRestrictions::AssertIOAllowed(); | 560 base::ThreadRestrictions::AssertIOAllowed(); |
| 561 base::AutoLock scoped_lock(g_mime_util_xdg_lock); |
| 556 return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str()); | 562 return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str()); |
| 557 } | 563 } |
| 558 | 564 |
| 559 std::string GetDataMimeType(const std::string& data) { | 565 std::string GetDataMimeType(const std::string& data) { |
| 560 base::ThreadRestrictions::AssertIOAllowed(); | 566 base::ThreadRestrictions::AssertIOAllowed(); |
| 567 base::AutoLock scoped_lock(g_mime_util_xdg_lock); |
| 561 return xdg_mime_get_mime_type_for_data(data.data(), data.length(), NULL); | 568 return xdg_mime_get_mime_type_for_data(data.data(), data.length(), NULL); |
| 562 } | 569 } |
| 563 | 570 |
| 564 void DetectGtkTheme() { | 571 void DetectGtkTheme() { |
| 565 // If the theme name is already loaded, do nothing. Chrome doesn't respond | 572 // If the theme name is already loaded, do nothing. Chrome doesn't respond |
| 566 // to changes in the system theme, so we never need to set this more than | 573 // to changes in the system theme, so we never need to set this more than |
| 567 // once. | 574 // once. |
| 568 if (!MimeUtilConstants::GetInstance()->gtk_theme_name_.empty()) | 575 if (!MimeUtilConstants::GetInstance()->gtk_theme_name_.empty()) |
| 569 return; | 576 return; |
| 570 | 577 |
| 571 // We should only be called on the UI thread. | 578 // We should only be called on the UI thread. |
| 572 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()); | 579 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()); |
| 573 | 580 |
| 574 gchar* gtk_theme_name; | 581 gchar* gtk_theme_name; |
| 575 g_object_get(gtk_settings_get_default(), | 582 g_object_get(gtk_settings_get_default(), |
| 576 "gtk-icon-theme-name", | 583 "gtk-icon-theme-name", |
| 577 >k_theme_name, NULL); | 584 >k_theme_name, NULL); |
| 578 MimeUtilConstants::GetInstance()->gtk_theme_name_.assign(gtk_theme_name); | 585 MimeUtilConstants::GetInstance()->gtk_theme_name_.assign(gtk_theme_name); |
| 579 g_free(gtk_theme_name); | 586 g_free(gtk_theme_name); |
| 580 } | 587 } |
| 581 | 588 |
| 582 FilePath GetMimeIcon(const std::string& mime_type, size_t size) { | 589 FilePath GetMimeIcon(const std::string& mime_type, size_t size) { |
| 583 base::ThreadRestrictions::AssertIOAllowed(); | 590 base::ThreadRestrictions::AssertIOAllowed(); |
| 584 std::vector<std::string> icon_names; | 591 std::vector<std::string> icon_names; |
| 585 std::string icon_name; | 592 std::string icon_name; |
| 586 FilePath icon_file; | 593 FilePath icon_file; |
| 587 | 594 |
| 588 const char* icon = xdg_mime_get_icon(mime_type.c_str()); | 595 { |
| 589 icon_name = std::string(icon ? icon : ""); | 596 base::AutoLock scoped_lock(g_mime_util_xdg_lock); |
| 597 const char *icon = xdg_mime_get_icon(mime_type.c_str()); |
| 598 icon_name = std::string(icon ? icon : ""); |
| 599 } |
| 600 |
| 590 if (icon_name.length()) | 601 if (icon_name.length()) |
| 591 icon_names.push_back(icon_name); | 602 icon_names.push_back(icon_name); |
| 592 | 603 |
| 593 // For text/plain, try text-plain. | 604 // For text/plain, try text-plain. |
| 594 icon_name = mime_type; | 605 icon_name = mime_type; |
| 595 for (size_t i = icon_name.find('/', 0); i != std::string::npos; | 606 for (size_t i = icon_name.find('/', 0); i != std::string::npos; |
| 596 i = icon_name.find('/', i + 1)) { | 607 i = icon_name.find('/', i + 1)) { |
| 597 icon_name[i] = '-'; | 608 icon_name[i] = '-'; |
| 598 } | 609 } |
| 599 icon_names.push_back(icon_name); | 610 icon_names.push_back(icon_name); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 619 } else { | 630 } else { |
| 620 icon_file = LookupIconInDefaultTheme(icon_names[i], size); | 631 icon_file = LookupIconInDefaultTheme(icon_names[i], size); |
| 621 if (!icon_file.empty()) | 632 if (!icon_file.empty()) |
| 622 return icon_file; | 633 return icon_file; |
| 623 } | 634 } |
| 624 } | 635 } |
| 625 return FilePath(); | 636 return FilePath(); |
| 626 } | 637 } |
| 627 | 638 |
| 628 } // namespace mime_util | 639 } // namespace mime_util |
| OLD | NEW |