| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/utility/chrome_content_utility_client.h" | 5 #include "chrome/utility/chrome_content_utility_client.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 55 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 56 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); | 56 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); |
| 57 if (!lang.empty()) | 57 if (!lang.empty()) |
| 58 extension_l10n_util::SetProcessLocale(lang); | 58 extension_l10n_util::SetProcessLocale(lang); |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool ChromeContentUtilityClient::OnMessageReceived( | 61 bool ChromeContentUtilityClient::OnMessageReceived( |
| 62 const IPC::Message& message) { | 62 const IPC::Message& message) { |
| 63 bool handled = true; | 63 bool handled = true; |
| 64 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) | 64 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) |
| 65 IPC_MESSAGE_HANDLER(UtilityMsg_UnpackExtension, OnUnpackExtension) | 65 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension, OnUnpackExtension) |
| 66 IPC_MESSAGE_HANDLER(UtilityMsg_UnpackWebResource, OnUnpackWebResource) | 66 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackWebResource, |
| 67 IPC_MESSAGE_HANDLER(UtilityMsg_ParseUpdateManifest, OnParseUpdateManifest) | 67 OnUnpackWebResource) |
| 68 IPC_MESSAGE_HANDLER(UtilityMsg_DecodeImage, OnDecodeImage) | 68 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest, |
| 69 IPC_MESSAGE_HANDLER(UtilityMsg_DecodeImageBase64, OnDecodeImageBase64) | 69 OnParseUpdateManifest) |
| 70 IPC_MESSAGE_HANDLER(UtilityMsg_RenderPDFPagesToMetafile, | 70 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage) |
| 71 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64) |
| 72 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafile, |
| 71 OnRenderPDFPagesToMetafile) | 73 OnRenderPDFPagesToMetafile) |
| 72 IPC_MESSAGE_HANDLER(UtilityMsg_ParseJSON, OnParseJSON) | 74 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) |
| 73 IPC_MESSAGE_HANDLER(UtilityMsg_GetPrinterCapsAndDefaults, | 75 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, |
| 74 OnGetPrinterCapsAndDefaults) | 76 OnGetPrinterCapsAndDefaults) |
| 75 IPC_MESSAGE_UNHANDLED(handled = false) | 77 IPC_MESSAGE_UNHANDLED(handled = false) |
| 76 IPC_END_MESSAGE_MAP() | 78 IPC_END_MESSAGE_MAP() |
| 77 return handled; | 79 return handled; |
| 78 } | 80 } |
| 79 | 81 |
| 80 bool ChromeContentUtilityClient::Send(IPC::Message* message) { | 82 bool ChromeContentUtilityClient::Send(IPC::Message* message) { |
| 81 return UtilityThread::current()->Send(message); | 83 return UtilityThread::current()->Send(message); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void ChromeContentUtilityClient::OnUnpackExtension( | 86 void ChromeContentUtilityClient::OnUnpackExtension( |
| 85 const FilePath& extension_path) { | 87 const FilePath& extension_path) { |
| 86 ExtensionUnpacker unpacker(extension_path); | 88 ExtensionUnpacker unpacker(extension_path); |
| 87 if (unpacker.Run() && unpacker.DumpImagesToFile() && | 89 if (unpacker.Run() && unpacker.DumpImagesToFile() && |
| 88 unpacker.DumpMessageCatalogsToFile()) { | 90 unpacker.DumpMessageCatalogsToFile()) { |
| 89 Send(new UtilityHostMsg_UnpackExtension_Succeeded( | 91 Send(new ChromeUtilityHostMsg_UnpackExtension_Succeeded( |
| 90 *unpacker.parsed_manifest())); | 92 *unpacker.parsed_manifest())); |
| 91 } else { | 93 } else { |
| 92 Send(new UtilityHostMsg_UnpackExtension_Failed(unpacker.error_message())); | 94 Send(new ChromeUtilityHostMsg_UnpackExtension_Failed( |
| 95 unpacker.error_message())); |
| 93 } | 96 } |
| 94 | 97 |
| 95 UtilityThread::current()->ReleaseProcessIfNeeded(); | 98 UtilityThread::current()->ReleaseProcessIfNeeded(); |
| 96 } | 99 } |
| 97 | 100 |
| 98 void ChromeContentUtilityClient::OnUnpackWebResource( | 101 void ChromeContentUtilityClient::OnUnpackWebResource( |
| 99 const std::string& resource_data) { | 102 const std::string& resource_data) { |
| 100 // Parse json data. | 103 // Parse json data. |
| 101 // TODO(mrc): Add the possibility of a template that controls parsing, and | 104 // TODO(mrc): Add the possibility of a template that controls parsing, and |
| 102 // the ability to download and verify images. | 105 // the ability to download and verify images. |
| 103 WebResourceUnpacker unpacker(resource_data); | 106 WebResourceUnpacker unpacker(resource_data); |
| 104 if (unpacker.Run()) { | 107 if (unpacker.Run()) { |
| 105 Send(new UtilityHostMsg_UnpackWebResource_Succeeded( | 108 Send(new ChromeUtilityHostMsg_UnpackWebResource_Succeeded( |
| 106 *unpacker.parsed_json())); | 109 *unpacker.parsed_json())); |
| 107 } else { | 110 } else { |
| 108 Send(new UtilityHostMsg_UnpackWebResource_Failed( | 111 Send(new ChromeUtilityHostMsg_UnpackWebResource_Failed( |
| 109 unpacker.error_message())); | 112 unpacker.error_message())); |
| 110 } | 113 } |
| 111 | 114 |
| 112 UtilityThread::current()->ReleaseProcessIfNeeded(); | 115 UtilityThread::current()->ReleaseProcessIfNeeded(); |
| 113 } | 116 } |
| 114 | 117 |
| 115 void ChromeContentUtilityClient::OnParseUpdateManifest(const std::string& xml) { | 118 void ChromeContentUtilityClient::OnParseUpdateManifest(const std::string& xml) { |
| 116 UpdateManifest manifest; | 119 UpdateManifest manifest; |
| 117 if (!manifest.Parse(xml)) { | 120 if (!manifest.Parse(xml)) { |
| 118 Send(new UtilityHostMsg_ParseUpdateManifest_Failed(manifest.errors())); | 121 Send(new ChromeUtilityHostMsg_ParseUpdateManifest_Failed( |
| 122 manifest.errors())); |
| 119 } else { | 123 } else { |
| 120 Send(new UtilityHostMsg_ParseUpdateManifest_Succeeded(manifest.results())); | 124 Send(new ChromeUtilityHostMsg_ParseUpdateManifest_Succeeded( |
| 125 manifest.results())); |
| 121 } | 126 } |
| 122 UtilityThread::current()->ReleaseProcessIfNeeded(); | 127 UtilityThread::current()->ReleaseProcessIfNeeded(); |
| 123 } | 128 } |
| 124 | 129 |
| 125 void ChromeContentUtilityClient::OnDecodeImage( | 130 void ChromeContentUtilityClient::OnDecodeImage( |
| 126 const std::vector<unsigned char>& encoded_data) { | 131 const std::vector<unsigned char>& encoded_data) { |
| 127 webkit_glue::ImageDecoder decoder; | 132 webkit_glue::ImageDecoder decoder; |
| 128 const SkBitmap& decoded_image = decoder.Decode(&encoded_data[0], | 133 const SkBitmap& decoded_image = decoder.Decode(&encoded_data[0], |
| 129 encoded_data.size()); | 134 encoded_data.size()); |
| 130 if (decoded_image.empty()) { | 135 if (decoded_image.empty()) { |
| 131 Send(new UtilityHostMsg_DecodeImage_Failed()); | 136 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); |
| 132 } else { | 137 } else { |
| 133 Send(new UtilityHostMsg_DecodeImage_Succeeded(decoded_image)); | 138 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(decoded_image)); |
| 134 } | 139 } |
| 135 UtilityThread::current()->ReleaseProcessIfNeeded(); | 140 UtilityThread::current()->ReleaseProcessIfNeeded(); |
| 136 } | 141 } |
| 137 | 142 |
| 138 void ChromeContentUtilityClient::OnDecodeImageBase64( | 143 void ChromeContentUtilityClient::OnDecodeImageBase64( |
| 139 const std::string& encoded_string) { | 144 const std::string& encoded_string) { |
| 140 std::string decoded_string; | 145 std::string decoded_string; |
| 141 | 146 |
| 142 if (!base::Base64Decode(encoded_string, &decoded_string)) { | 147 if (!base::Base64Decode(encoded_string, &decoded_string)) { |
| 143 Send(new UtilityHostMsg_DecodeImage_Failed()); | 148 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); |
| 144 return; | 149 return; |
| 145 } | 150 } |
| 146 | 151 |
| 147 std::vector<unsigned char> decoded_vector(decoded_string.size()); | 152 std::vector<unsigned char> decoded_vector(decoded_string.size()); |
| 148 for (size_t i = 0; i < decoded_string.size(); ++i) { | 153 for (size_t i = 0; i < decoded_string.size(); ++i) { |
| 149 decoded_vector[i] = static_cast<unsigned char>(decoded_string[i]); | 154 decoded_vector[i] = static_cast<unsigned char>(decoded_string[i]); |
| 150 } | 155 } |
| 151 | 156 |
| 152 OnDecodeImage(decoded_vector); | 157 OnDecodeImage(decoded_vector); |
| 153 } | 158 } |
| 154 | 159 |
| 155 void ChromeContentUtilityClient::OnRenderPDFPagesToMetafile( | 160 void ChromeContentUtilityClient::OnRenderPDFPagesToMetafile( |
| 156 base::PlatformFile pdf_file, | 161 base::PlatformFile pdf_file, |
| 157 const FilePath& metafile_path, | 162 const FilePath& metafile_path, |
| 158 const gfx::Rect& render_area, | 163 const gfx::Rect& render_area, |
| 159 int render_dpi, | 164 int render_dpi, |
| 160 const std::vector<printing::PageRange>& page_ranges) { | 165 const std::vector<printing::PageRange>& page_ranges) { |
| 161 bool succeeded = false; | 166 bool succeeded = false; |
| 162 #if defined(OS_WIN) | 167 #if defined(OS_WIN) |
| 163 int highest_rendered_page_number = 0; | 168 int highest_rendered_page_number = 0; |
| 164 succeeded = RenderPDFToWinMetafile(pdf_file, | 169 succeeded = RenderPDFToWinMetafile(pdf_file, |
| 165 metafile_path, | 170 metafile_path, |
| 166 render_area, | 171 render_area, |
| 167 render_dpi, | 172 render_dpi, |
| 168 page_ranges, | 173 page_ranges, |
| 169 &highest_rendered_page_number); | 174 &highest_rendered_page_number); |
| 170 if (succeeded) { | 175 if (succeeded) { |
| 171 Send(new UtilityHostMsg_RenderPDFPagesToMetafile_Succeeded( | 176 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded( |
| 172 highest_rendered_page_number)); | 177 highest_rendered_page_number)); |
| 173 } | 178 } |
| 174 #endif // defined(OS_WIN) | 179 #endif // defined(OS_WIN) |
| 175 if (!succeeded) { | 180 if (!succeeded) { |
| 176 Send(new UtilityHostMsg_RenderPDFPagesToMetafile_Failed()); | 181 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); |
| 177 } | 182 } |
| 178 UtilityThread::current()->ReleaseProcessIfNeeded(); | 183 UtilityThread::current()->ReleaseProcessIfNeeded(); |
| 179 } | 184 } |
| 180 | 185 |
| 181 #if defined(OS_WIN) | 186 #if defined(OS_WIN) |
| 182 // Exported by pdf.dll | 187 // Exported by pdf.dll |
| 183 typedef bool (*RenderPDFPageToDCProc)( | 188 typedef bool (*RenderPDFPageToDCProc)( |
| 184 const unsigned char* pdf_buffer, int buffer_size, int page_number, HDC dc, | 189 const unsigned char* pdf_buffer, int buffer_size, int page_number, HDC dc, |
| 185 int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y, | 190 int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y, |
| 186 int bounds_width, int bounds_height, bool fit_to_bounds, | 191 int bounds_width, int bounds_height, bool fit_to_bounds, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 211 DWORD WINAPI UtilityProcess_GetFontDataPatch( | 216 DWORD WINAPI UtilityProcess_GetFontDataPatch( |
| 212 HDC hdc, DWORD table, DWORD offset, LPVOID buffer, DWORD length) { | 217 HDC hdc, DWORD table, DWORD offset, LPVOID buffer, DWORD length) { |
| 213 int rv = GetFontData(hdc, table, offset, buffer, length); | 218 int rv = GetFontData(hdc, table, offset, buffer, length); |
| 214 if (rv == GDI_ERROR && hdc) { | 219 if (rv == GDI_ERROR && hdc) { |
| 215 HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT)); | 220 HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT)); |
| 216 | 221 |
| 217 LOGFONT logfont; | 222 LOGFONT logfont; |
| 218 if (GetObject(font, sizeof(LOGFONT), &logfont)) { | 223 if (GetObject(font, sizeof(LOGFONT), &logfont)) { |
| 219 std::vector<char> font_data; | 224 std::vector<char> font_data; |
| 220 if (UtilityThread::current()->Send( | 225 if (UtilityThread::current()->Send( |
| 221 new UtilityHostMsg_PreCacheFont(logfont))) | 226 new ChromeUtilityHostMsg_PreCacheFont(logfont))) |
| 222 rv = GetFontData(hdc, table, offset, buffer, length); | 227 rv = GetFontData(hdc, table, offset, buffer, length); |
| 223 } | 228 } |
| 224 } | 229 } |
| 225 return rv; | 230 return rv; |
| 226 } | 231 } |
| 227 | 232 |
| 228 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( | 233 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( |
| 229 base::PlatformFile pdf_file, | 234 base::PlatformFile pdf_file, |
| 230 const FilePath& metafile_path, | 235 const FilePath& metafile_path, |
| 231 const gfx::Rect& render_area, | 236 const gfx::Rect& render_area, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 323 |
| 319 | 324 |
| 320 void ChromeContentUtilityClient::OnParseJSON(const std::string& json) { | 325 void ChromeContentUtilityClient::OnParseJSON(const std::string& json) { |
| 321 int error_code; | 326 int error_code; |
| 322 std::string error; | 327 std::string error; |
| 323 Value* value = | 328 Value* value = |
| 324 base::JSONReader::ReadAndReturnError(json, false, &error_code, &error); | 329 base::JSONReader::ReadAndReturnError(json, false, &error_code, &error); |
| 325 if (value) { | 330 if (value) { |
| 326 ListValue wrapper; | 331 ListValue wrapper; |
| 327 wrapper.Append(value); | 332 wrapper.Append(value); |
| 328 Send(new UtilityHostMsg_ParseJSON_Succeeded(wrapper)); | 333 Send(new ChromeUtilityHostMsg_ParseJSON_Succeeded(wrapper)); |
| 329 } else { | 334 } else { |
| 330 Send(new UtilityHostMsg_ParseJSON_Failed(error)); | 335 Send(new ChromeUtilityHostMsg_ParseJSON_Failed(error)); |
| 331 } | 336 } |
| 332 UtilityThread::current()->ReleaseProcessIfNeeded(); | 337 UtilityThread::current()->ReleaseProcessIfNeeded(); |
| 333 } | 338 } |
| 334 | 339 |
| 335 void ChromeContentUtilityClient::OnGetPrinterCapsAndDefaults( | 340 void ChromeContentUtilityClient::OnGetPrinterCapsAndDefaults( |
| 336 const std::string& printer_name) { | 341 const std::string& printer_name) { |
| 337 scoped_refptr<printing::PrintBackend> print_backend = | 342 scoped_refptr<printing::PrintBackend> print_backend = |
| 338 printing::PrintBackend::CreateInstance(NULL); | 343 printing::PrintBackend::CreateInstance(NULL); |
| 339 printing::PrinterCapsAndDefaults printer_info; | 344 printing::PrinterCapsAndDefaults printer_info; |
| 340 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { | 345 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { |
| 341 Send(new UtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded(printer_name, | 346 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded( |
| 342 printer_info)); | 347 printer_name, printer_info)); |
| 343 } else { | 348 } else { |
| 344 Send(new UtilityHostMsg_GetPrinterCapsAndDefaults_Failed(printer_name)); | 349 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed( |
| 350 printer_name)); |
| 345 } | 351 } |
| 346 UtilityThread::current()->ReleaseProcessIfNeeded(); | 352 UtilityThread::current()->ReleaseProcessIfNeeded(); |
| 347 } | 353 } |
| 348 | 354 |
| 349 } // namespace chrome | 355 } // namespace chrome |
| OLD | NEW |