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 "chrome/browser/shell_integration_linux.h" | 5 #include "chrome/browser/shell_integration_linux.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <glib.h> | 8 #include <glib.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "base/strings/string_tokenizer.h" | 29 #include "base/strings/string_tokenizer.h" |
30 #include "base/threading/thread.h" | 30 #include "base/threading/thread.h" |
31 #include "base/threading/thread_restrictions.h" | 31 #include "base/threading/thread_restrictions.h" |
32 #include "base/utf_string_conversions.h" | 32 #include "base/utf_string_conversions.h" |
33 #include "build/build_config.h" | 33 #include "build/build_config.h" |
34 #include "chrome/browser/web_applications/web_app.h" | 34 #include "chrome/browser/web_applications/web_app.h" |
35 #include "chrome/common/chrome_constants.h" | 35 #include "chrome/common/chrome_constants.h" |
36 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
37 #include "googleurl/src/gurl.h" | 37 #include "googleurl/src/gurl.h" |
38 #include "ui/gfx/codec/png_codec.h" | 38 #include "ui/gfx/codec/png_codec.h" |
| 39 #include "ui/gfx/image/image_skia.h" |
| 40 #include "ui/gfx/image/image_skia_rep.h" |
39 | 41 |
40 using content::BrowserThread; | 42 using content::BrowserThread; |
41 | 43 |
42 namespace { | 44 namespace { |
43 | 45 |
44 // Helper to launch xdg scripts. We don't want them to ask any questions on the | 46 // Helper to launch xdg scripts. We don't want them to ask any questions on the |
45 // terminal etc. The function returns true if the utility launches and exits | 47 // terminal etc. The function returns true if the utility launches and exits |
46 // cleanly, in which case |exit_code| returns the utility's exit code. | 48 // cleanly, in which case |exit_code| returns the utility's exit code. |
47 bool LaunchXdgUtility(const std::vector<std::string>& argv, int* exit_code) { | 49 bool LaunchXdgUtility(const std::vector<std::string>& argv, int* exit_code) { |
48 // xdg-settings internally runs xdg-mime, which uses mv to move newly-created | 50 // xdg-settings internally runs xdg-mime, which uses mv to move newly-created |
(...skipping 27 matching lines...) Expand all Loading... |
76 if (shortcut_info.favicon.IsEmpty()) | 78 if (shortcut_info.favicon.IsEmpty()) |
77 return std::string(); | 79 return std::string(); |
78 | 80 |
79 // TODO(phajdan.jr): Report errors from this function, possibly as infobars. | 81 // TODO(phajdan.jr): Report errors from this function, possibly as infobars. |
80 base::ScopedTempDir temp_dir; | 82 base::ScopedTempDir temp_dir; |
81 if (!temp_dir.CreateUniqueTempDir()) | 83 if (!temp_dir.CreateUniqueTempDir()) |
82 return std::string(); | 84 return std::string(); |
83 | 85 |
84 base::FilePath temp_file_path = temp_dir.path().Append( | 86 base::FilePath temp_file_path = temp_dir.path().Append( |
85 shortcut_filename.ReplaceExtension("png")); | 87 shortcut_filename.ReplaceExtension("png")); |
| 88 std::string icon_name = temp_file_path.BaseName().RemoveExtension().value(); |
86 | 89 |
87 std::vector<unsigned char> png_data; | 90 std::vector<gfx::ImageSkiaRep> image_reps = |
88 const SkBitmap* bitmap = shortcut_info.favicon.ToSkBitmap(); | 91 shortcut_info.favicon.ToImageSkia()->image_reps(); |
89 gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &png_data); | 92 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin(); |
90 int bytes_written = file_util::WriteFile(temp_file_path, | 93 it != image_reps.end(); ++it) { |
91 reinterpret_cast<char*>(png_data.data()), png_data.size()); | 94 std::vector<unsigned char> png_data; |
| 95 const SkBitmap& bitmap = it->sk_bitmap(); |
| 96 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data)) { |
| 97 // If the bitmap could not be encoded to PNG format, skip it. |
| 98 LOG(WARNING) << "Could not encode icon " << icon_name << ".png at size " |
| 99 << bitmap.width() << "."; |
| 100 continue; |
| 101 } |
| 102 int bytes_written = file_util::WriteFile(temp_file_path, |
| 103 reinterpret_cast<char*>(png_data.data()), png_data.size()); |
92 | 104 |
93 if (bytes_written != static_cast<int>(png_data.size())) | 105 if (bytes_written != static_cast<int>(png_data.size())) |
94 return std::string(); | 106 return std::string(); |
95 | 107 |
96 std::vector<std::string> argv; | 108 std::vector<std::string> argv; |
97 argv.push_back("xdg-icon-resource"); | 109 argv.push_back("xdg-icon-resource"); |
98 argv.push_back("install"); | 110 argv.push_back("install"); |
99 | 111 |
100 // Always install in user mode, even if someone runs the browser as root | 112 // Always install in user mode, even if someone runs the browser as root |
101 // (people do that). | 113 // (people do that). |
102 argv.push_back("--mode"); | 114 argv.push_back("--mode"); |
103 argv.push_back("user"); | 115 argv.push_back("user"); |
104 | 116 |
105 argv.push_back("--size"); | 117 argv.push_back("--size"); |
106 argv.push_back(base::IntToString(bitmap->width())); | 118 argv.push_back(base::IntToString(bitmap.width())); |
107 | 119 |
108 argv.push_back(temp_file_path.value()); | 120 argv.push_back(temp_file_path.value()); |
109 std::string icon_name = temp_file_path.BaseName().RemoveExtension().value(); | 121 argv.push_back(icon_name); |
110 argv.push_back(icon_name); | 122 int exit_code; |
111 int exit_code; | 123 if (!LaunchXdgUtility(argv, &exit_code) || exit_code) { |
112 LaunchXdgUtility(argv, &exit_code); | 124 LOG(WARNING) << "Could not install icon " << icon_name << ".png at size " |
| 125 << bitmap.width() << "."; |
| 126 } |
| 127 } |
113 return icon_name; | 128 return icon_name; |
114 } | 129 } |
115 | 130 |
116 bool CreateShortcutOnDesktop(const base::FilePath& shortcut_filename, | 131 bool CreateShortcutOnDesktop(const base::FilePath& shortcut_filename, |
117 const std::string& contents) { | 132 const std::string& contents) { |
118 // Make sure that we will later call openat in a secure way. | 133 // Make sure that we will later call openat in a secure way. |
119 DCHECK_EQ(shortcut_filename.BaseName().value(), shortcut_filename.value()); | 134 DCHECK_EQ(shortcut_filename.BaseName().value(), shortcut_filename.value()); |
120 | 135 |
121 base::FilePath desktop_path; | 136 base::FilePath desktop_path; |
122 if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop_path)) | 137 if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop_path)) |
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 | 724 |
710 base::FilePath shortcut_filename = GetExtensionShortcutFilename( | 725 base::FilePath shortcut_filename = GetExtensionShortcutFilename( |
711 profile_path, extension_id); | 726 profile_path, extension_id); |
712 DCHECK(!shortcut_filename.empty()); | 727 DCHECK(!shortcut_filename.empty()); |
713 | 728 |
714 DeleteShortcutOnDesktop(shortcut_filename); | 729 DeleteShortcutOnDesktop(shortcut_filename); |
715 DeleteShortcutInApplicationsMenu(shortcut_filename); | 730 DeleteShortcutInApplicationsMenu(shortcut_filename); |
716 } | 731 } |
717 | 732 |
718 } // namespace ShellIntegrationLinux | 733 } // namespace ShellIntegrationLinux |
OLD | NEW |