Chromium Code Reviews| 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/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/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/base64.h" | |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/memory/ref_counted.h" | |
| 11 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
| 12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 13 #include "chrome/browser/importer/external_process_importer_bridge.h" | 14 #include "chrome/browser/importer/external_process_importer_bridge.h" |
| 14 #include "chrome/browser/importer/importer.h" | 15 #include "chrome/browser/importer/importer.h" |
| 15 #include "chrome/browser/importer/profile_import_process_messages.h" | 16 #include "chrome/browser/importer/profile_import_process_messages.h" |
| 16 #include "chrome/common/child_process_logging.h" | 17 #include "chrome/common/child_process_logging.h" |
| 17 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/common/chrome_utility_messages.h" | 19 #include "chrome/common/chrome_utility_messages.h" |
| 19 #include "chrome/common/extensions/extension.h" | 20 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/extension_l10n_util.h" | 21 #include "chrome/common/extensions/extension_l10n_util.h" |
| 21 #include "chrome/common/extensions/extension_unpacker.h" | 22 #include "chrome/common/extensions/extension_unpacker.h" |
| 22 #include "chrome/common/extensions/update_manifest.h" | 23 #include "chrome/common/extensions/update_manifest.h" |
| 23 #include "chrome/common/web_resource/web_resource_unpacker.h" | 24 #include "chrome/common/web_resource/web_resource_unpacker.h" |
| 24 #include "content/public/utility/utility_thread.h" | 25 #include "content/public/utility/utility_thread.h" |
| 25 #include "printing/backend/print_backend.h" | 26 #include "printing/backend/print_backend.h" |
| 26 #include "printing/page_range.h" | 27 #include "printing/page_range.h" |
| 27 #include "third_party/skia/include/core/SkBitmap.h" | 28 #include "third_party/skia/include/core/SkBitmap.h" |
| 28 #include "ui/base/ui_base_switches.h" | 29 #include "ui/base/ui_base_switches.h" |
| 29 #include "ui/gfx/rect.h" | 30 #include "ui/gfx/rect.h" |
| 30 #include "webkit/glue/image_decoder.h" | 31 #include "webkit/glue/image_decoder.h" |
| 31 | 32 |
| 32 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
| 33 #include "base/file_util.h" | 34 #include "base/file_util.h" |
| 34 #include "base/memory/scoped_ptr.h" | |
| 35 #include "base/path_service.h" | 35 #include "base/path_service.h" |
| 36 #include "base/win/iat_patch_function.h" | 36 #include "base/win/iat_patch_function.h" |
| 37 #include "base/win/scoped_handle.h" | 37 #include "base/win/scoped_handle.h" |
| 38 #include "content/public/common/content_switches.h" | 38 #include "content/public/common/content_switches.h" |
| 39 #include "printing/emf_win.h" | 39 #include "printing/emf_win.h" |
| 40 #include "ui/gfx/gdi_util.h" | 40 #include "ui/gfx/gdi_util.h" |
| 41 #endif // defined(OS_WIN) | 41 #endif // defined(OS_WIN) |
| 42 | 42 |
| 43 namespace chrome { | 43 namespace chrome { |
| 44 | 44 |
| 45 ChromeContentUtilityClient::ChromeContentUtilityClient() : items_to_import_(0) { | 45 #if defined(OS_ANDROID) |
| 46 | |
| 47 // Dummy implementation on Android. | |
| 48 class ChromeContentUtilityClient::ImportHandler { | |
| 49 public: | |
| 50 void OnImportStart( | |
| 51 const importer::SourceProfile& source_profile, | |
| 52 uint16 items, | |
| 53 const base::DictionaryValue& localized_strings) { | |
| 54 } | |
| 55 void OnImportCancel() {} | |
| 56 void OnImportItemFinished(uint16 item) {} | |
| 57 }; | |
| 58 | |
| 59 #else | |
| 60 | |
| 61 class ChromeContentUtilityClient::ImportHandler { | |
|
Yaron
2012/07/17 19:13:53
Why not split this out to a separate file and then
Philippe
2012/07/18 09:34:27
Good point. I kept the dummy implementation for An
| |
| 62 public: | |
| 63 ImportHandler() : items_to_import_(0) {} | |
| 64 | |
| 65 void OnImportStart( | |
| 66 const importer::SourceProfile& source_profile, | |
| 67 uint16 items, | |
| 68 const base::DictionaryValue& localized_strings) { | |
| 69 bridge_ = new ExternalProcessImporterBridge( | |
| 70 localized_strings, content::UtilityThread::Get(), | |
| 71 base::MessageLoopProxy::current()); | |
| 72 importer_ = importer::CreateImporterByType(source_profile.importer_type); | |
| 73 if (!importer_) { | |
| 74 Send(new ProfileImportProcessHostMsg_Import_Finished(false, | |
| 75 "Importer could not be created.")); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 items_to_import_ = items; | |
| 80 | |
| 81 // Create worker thread in which importer runs. | |
| 82 import_thread_.reset(new base::Thread("import_thread")); | |
| 83 base::Thread::Options options; | |
| 84 options.message_loop_type = MessageLoop::TYPE_IO; | |
| 85 if (!import_thread_->StartWithOptions(options)) { | |
| 86 NOTREACHED(); | |
| 87 ImporterCleanup(); | |
| 88 } | |
| 89 import_thread_->message_loop()->PostTask( | |
| 90 FROM_HERE, base::Bind(&Importer::StartImport, importer_.get(), | |
| 91 source_profile, items, bridge_)); | |
| 92 } | |
| 93 | |
| 94 void OnImportCancel() { | |
| 95 ImporterCleanup(); | |
| 96 } | |
| 97 | |
| 98 void OnImportItemFinished(uint16 item) { | |
| 99 items_to_import_ ^= item; // Remove finished item from mask. | |
| 100 // If we've finished with all items, notify the browser process. | |
| 101 if (items_to_import_ == 0) { | |
| 102 Send(new ProfileImportProcessHostMsg_Import_Finished(true, "")); | |
| 103 ImporterCleanup(); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 private: | |
| 108 // The following are used with out of process profile import: | |
| 109 void ImporterCleanup() { | |
| 110 importer_->Cancel(); | |
| 111 importer_ = NULL; | |
| 112 bridge_ = NULL; | |
| 113 import_thread_.reset(); | |
| 114 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | |
| 115 } | |
| 116 | |
| 117 static bool Send(IPC::Message* message) { | |
| 118 return content::UtilityThread::Get()->Send(message); | |
| 119 } | |
| 120 | |
| 121 // Thread that importer runs on, while ProfileImportThread handles messages | |
| 122 // from the browser process. | |
| 123 scoped_ptr<base::Thread> import_thread_; | |
| 124 | |
| 125 // Bridge object is passed to importer, so that it can send IPC calls | |
| 126 // directly back to the ProfileImportProcessHost. | |
| 127 scoped_refptr<ExternalProcessImporterBridge> bridge_; | |
| 128 | |
| 129 // A bitmask of importer::ImportItem. | |
| 130 uint16 items_to_import_; | |
| 131 | |
| 132 // Importer of the appropriate type (Firefox, Safari, IE, etc.) | |
| 133 scoped_refptr<Importer> importer_; | |
| 134 }; | |
| 135 | |
| 136 #endif | |
| 137 | |
| 138 ChromeContentUtilityClient::ChromeContentUtilityClient() | |
| 139 : import_handler_(new ImportHandler()) { | |
| 46 } | 140 } |
| 47 | 141 |
| 48 ChromeContentUtilityClient::~ChromeContentUtilityClient() { | 142 ChromeContentUtilityClient::~ChromeContentUtilityClient() { |
| 49 } | 143 } |
| 50 | 144 |
| 51 void ChromeContentUtilityClient::UtilityThreadStarted() { | 145 void ChromeContentUtilityClient::UtilityThreadStarted() { |
| 52 #if defined(OS_WIN) | 146 #if defined(OS_WIN) |
| 53 // Load the pdf plugin before the sandbox is turned on. This is for Windows | 147 // Load the pdf plugin before the sandbox is turned on. This is for Windows |
| 54 // only because we need this DLL only on Windows. | 148 // only because we need this DLL only on Windows. |
| 55 FilePath pdf; | 149 FilePath pdf; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 75 OnUnpackWebResource) | 169 OnUnpackWebResource) |
| 76 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest, | 170 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest, |
| 77 OnParseUpdateManifest) | 171 OnParseUpdateManifest) |
| 78 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage) | 172 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage) |
| 79 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64) | 173 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64) |
| 80 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafile, | 174 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafile, |
| 81 OnRenderPDFPagesToMetafile) | 175 OnRenderPDFPagesToMetafile) |
| 82 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) | 176 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) |
| 83 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, | 177 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, |
| 84 OnGetPrinterCapsAndDefaults) | 178 OnGetPrinterCapsAndDefaults) |
| 85 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, | 179 IPC_MESSAGE_FORWARD( |
|
Yaron
2012/07/17 19:13:53
couldn't you also ask your ImportHandler to regist
Philippe
2012/07/18 09:34:27
Good point.
| |
| 86 OnImportStart) | 180 ProfileImportProcessMsg_StartImport, |
| 87 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, | 181 import_handler_.get(), |
| 88 OnImportCancel) | 182 ChromeContentUtilityClient::ImportHandler::OnImportStart) |
| 89 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished, | 183 IPC_MESSAGE_FORWARD( |
| 90 OnImportItemFinished) | 184 ProfileImportProcessMsg_CancelImport, |
| 185 import_handler_.get(), | |
| 186 ChromeContentUtilityClient::ImportHandler::OnImportCancel) | |
| 187 IPC_MESSAGE_FORWARD( | |
| 188 ProfileImportProcessMsg_ReportImportItemFinished, | |
| 189 import_handler_.get(), | |
| 190 ChromeContentUtilityClient::ImportHandler::OnImportItemFinished) | |
| 91 IPC_MESSAGE_UNHANDLED(handled = false) | 191 IPC_MESSAGE_UNHANDLED(handled = false) |
| 92 IPC_END_MESSAGE_MAP() | 192 IPC_END_MESSAGE_MAP() |
| 93 return handled; | 193 return handled; |
| 94 } | 194 } |
| 95 | 195 |
| 96 bool ChromeContentUtilityClient::Send(IPC::Message* message) { | 196 bool ChromeContentUtilityClient::Send(IPC::Message* message) { |
| 97 return content::UtilityThread::Get()->Send(message); | 197 return content::UtilityThread::Get()->Send(message); |
| 98 } | 198 } |
| 99 | 199 |
| 100 void ChromeContentUtilityClient::OnUnpackExtension( | 200 void ChromeContentUtilityClient::OnUnpackExtension( |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 printer_name, printer_info)); | 484 printer_name, printer_info)); |
| 385 } else | 485 } else |
| 386 #endif | 486 #endif |
| 387 { | 487 { |
| 388 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed( | 488 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed( |
| 389 printer_name)); | 489 printer_name)); |
| 390 } | 490 } |
| 391 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 491 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
| 392 } | 492 } |
| 393 | 493 |
| 394 void ChromeContentUtilityClient::OnImportStart( | |
| 395 const importer::SourceProfile& source_profile, | |
| 396 uint16 items, | |
| 397 const DictionaryValue& localized_strings) { | |
| 398 bridge_ = new ExternalProcessImporterBridge( | |
| 399 localized_strings, content::UtilityThread::Get(), | |
| 400 base::MessageLoopProxy::current()); | |
| 401 importer_ = importer::CreateImporterByType(source_profile.importer_type); | |
| 402 if (!importer_) { | |
| 403 Send(new ProfileImportProcessHostMsg_Import_Finished(false, | |
| 404 "Importer could not be created.")); | |
| 405 return; | |
| 406 } | |
| 407 | |
| 408 items_to_import_ = items; | |
| 409 | |
| 410 // Create worker thread in which importer runs. | |
| 411 import_thread_.reset(new base::Thread("import_thread")); | |
| 412 base::Thread::Options options; | |
| 413 options.message_loop_type = MessageLoop::TYPE_IO; | |
| 414 if (!import_thread_->StartWithOptions(options)) { | |
| 415 NOTREACHED(); | |
| 416 ImporterCleanup(); | |
| 417 } | |
| 418 import_thread_->message_loop()->PostTask( | |
| 419 FROM_HERE, base::Bind(&Importer::StartImport, importer_.get(), | |
| 420 source_profile, items, bridge_)); | |
| 421 } | |
| 422 | |
| 423 void ChromeContentUtilityClient::OnImportCancel() { | |
| 424 ImporterCleanup(); | |
| 425 } | |
| 426 | |
| 427 void ChromeContentUtilityClient::OnImportItemFinished(uint16 item) { | |
| 428 items_to_import_ ^= item; // Remove finished item from mask. | |
| 429 // If we've finished with all items, notify the browser process. | |
| 430 if (items_to_import_ == 0) { | |
| 431 Send(new ProfileImportProcessHostMsg_Import_Finished(true, "")); | |
| 432 ImporterCleanup(); | |
| 433 } | |
| 434 } | |
| 435 | |
| 436 void ChromeContentUtilityClient::ImporterCleanup() { | |
| 437 importer_->Cancel(); | |
| 438 importer_ = NULL; | |
| 439 bridge_ = NULL; | |
| 440 import_thread_.reset(); | |
| 441 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | |
| 442 } | |
| 443 | |
| 444 } // namespace chrome | 494 } // namespace chrome |
| OLD | NEW |