Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/utility/utility_handler.h" | 5 #include "extensions/utility/utility_handler.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/i18n/rtl.h" | |
| 11 #include "content/public/utility/utility_thread.h" | 9 #include "content/public/utility/utility_thread.h" |
| 12 #include "extensions/common/constants.h" | 10 #include "extensions/common/constants.h" |
| 13 #include "extensions/common/extension.h" | |
| 14 #include "extensions/common/extension_l10n_util.h" | 11 #include "extensions/common/extension_l10n_util.h" |
| 12 #include "extensions/common/extension_unpacker.mojom.h" | |
| 15 #include "extensions/common/extension_utility_messages.h" | 13 #include "extensions/common/extension_utility_messages.h" |
| 16 #include "extensions/common/extensions_client.h" | 14 #include "extensions/common/extensions_client.h" |
| 17 #include "extensions/common/manifest.h" | 15 #include "extensions/common/manifest.h" |
| 18 #include "extensions/common/update_manifest.h" | 16 #include "extensions/common/update_manifest.h" |
| 19 #include "extensions/strings/grit/extensions_strings.h" | 17 #include "extensions/strings/grit/extensions_strings.h" |
| 20 #include "extensions/utility/unpacker.h" | 18 #include "extensions/utility/unpacker.h" |
| 21 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
| 22 #include "ipc/ipc_message_macros.h" | 20 #include "ipc/ipc_message_macros.h" |
| 21 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 22 #include "services/service_manager/public/cpp/interface_registry.h" | |
| 23 #include "third_party/zlib/google/zip.h" | 23 #include "third_party/zlib/google/zip.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/ui_base_switches.h" | 25 #include "ui/base/ui_base_switches.h" |
| 26 | 26 |
| 27 namespace extensions { | 27 namespace extensions { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 bool Send(IPC::Message* message) { | 31 class ExtensionUnpackerImpl : public extensions::mojom::ExtensionUnpacker { |
| 32 return content::UtilityThread::Get()->Send(message); | 32 public: |
| 33 } | 33 ExtensionUnpackerImpl() = default; |
| 34 ~ExtensionUnpackerImpl() override = default; | |
| 34 | 35 |
| 35 void ReleaseProcessIfNeeded() { | 36 static void Create(extensions::mojom::ExtensionUnpackerRequest request) { |
| 36 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 37 mojo::MakeStrongBinding(base::MakeUnique<ExtensionUnpackerImpl>(), |
| 37 } | 38 std::move(request)); |
| 39 } | |
| 38 | 40 |
| 39 const char kExtensionHandlerUnzipError[] = | 41 private: |
| 40 "Could not unzip extension for install."; | 42 // extensions::mojom::ExtensionUnpacker: |
| 43 void Unzip(const base::FilePath& file, | |
| 44 const base::FilePath& path, | |
| 45 const UnzipCallback& callback) override { | |
| 46 std::unique_ptr<base::DictionaryValue> manifest; | |
| 47 if (UnzipFileManifestIntoPath(file, path, &manifest)) { | |
| 48 callback.Run(UnzipFileIntoPath(file, path, std::move(manifest))); | |
| 49 } else { | |
| 50 callback.Run(false); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 void Unpack(const base::FilePath& path, | |
| 55 const std::string& extension_id, | |
| 56 Manifest::Location location, | |
| 57 int32_t creation_flags, | |
| 58 const UnpackCallback& callback) override { | |
| 59 CHECK_NE(location, Manifest::INVALID_LOCATION); | |
|
dcheng
2017/03/07 09:45:14
I would suggest DCHECK_NE here, as we should be ab
Noel Gordon
2017/03/08 13:44:26
See https://codereview.chromium.org/2711513003
Tu
Devlin
2017/03/08 16:22:24
As Noel points out, this has been a problem lately
Noel Gordon
2017/03/08 17:56:09
My vote is leave the CHECK.
dcheng
2017/03/09 01:03:32
OK, I'm OK with that as long as we figure out what
| |
| 60 DCHECK(ExtensionsClient::Get()); | |
| 61 | |
| 62 content::UtilityThread::Get()->EnsureBlinkInitialized(); | |
| 63 | |
| 64 Unpacker unpacker(path.DirName(), path, extension_id, location, | |
| 65 creation_flags); | |
| 66 if (unpacker.Run()) { | |
| 67 callback.Run(base::string16(), unpacker.TakeParsedManifest()); | |
| 68 } else { | |
| 69 callback.Run(unpacker.error_message(), nullptr); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 static bool UnzipFileManifestIntoPath( | |
| 74 const base::FilePath& file, | |
| 75 const base::FilePath& path, | |
| 76 std::unique_ptr<base::DictionaryValue>* manifest) { | |
| 77 if (zip::UnzipWithFilterCallback( | |
| 78 file, path, base::Bind(&Unpacker::IsManifestFile), false)) { | |
| 79 std::string error; | |
| 80 *manifest = Unpacker::ReadManifest(path, &error); | |
| 81 return error.empty() && manifest->get(); | |
| 82 } | |
| 83 | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 static bool UnzipFileIntoPath( | |
| 88 const base::FilePath& file, | |
| 89 const base::FilePath& path, | |
| 90 std::unique_ptr<base::DictionaryValue> manifest) { | |
| 91 Manifest internal(Manifest::INTERNAL, std::move(manifest)); | |
| 92 // TODO(crbug.com/645263): This silently ignores blocked file types. | |
| 93 // Add install warnings. | |
| 94 return zip::UnzipWithFilterCallback( | |
| 95 file, path, | |
| 96 base::Bind(&Unpacker::ShouldExtractFile, internal.is_theme()), | |
| 97 true /* log_skipped_files */); | |
| 98 } | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(ExtensionUnpackerImpl); | |
| 101 }; | |
| 41 | 102 |
| 42 } // namespace | 103 } // namespace |
| 43 | 104 |
| 44 UtilityHandler::UtilityHandler() { | 105 UtilityHandler::UtilityHandler() = default; |
| 45 } | |
| 46 | 106 |
| 47 UtilityHandler::~UtilityHandler() { | 107 UtilityHandler::~UtilityHandler() = default; |
| 48 } | |
| 49 | 108 |
| 50 // static | 109 // static |
| 51 void UtilityHandler::UtilityThreadStarted() { | 110 void UtilityHandler::UtilityThreadStarted() { |
| 52 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 111 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 53 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); | 112 std::string lang = command_line->GetSwitchValueASCII(switches::kLang); |
| 54 if (!lang.empty()) | 113 if (!lang.empty()) |
| 55 extension_l10n_util::SetProcessLocale(lang); | 114 extension_l10n_util::SetProcessLocale(lang); |
| 56 } | 115 } |
| 57 | 116 |
| 117 // static | |
| 118 void UtilityHandler::ExposeInterfacesToBrowser( | |
| 119 service_manager::InterfaceRegistry* registry, | |
| 120 bool running_elevated) { | |
| 121 // If our process runs with elevated privileges, only add elevated Mojo | |
| 122 // services to the interface registry. | |
| 123 if (running_elevated) | |
| 124 return; | |
| 125 | |
| 126 registry->AddInterface(base::Bind(&ExtensionUnpackerImpl::Create)); | |
| 127 } | |
| 128 | |
| 58 bool UtilityHandler::OnMessageReceived(const IPC::Message& message) { | 129 bool UtilityHandler::OnMessageReceived(const IPC::Message& message) { |
| 59 bool handled = true; | 130 bool handled = true; |
| 60 IPC_BEGIN_MESSAGE_MAP(UtilityHandler, message) | 131 IPC_BEGIN_MESSAGE_MAP(UtilityHandler, message) |
| 61 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_ParseUpdateManifest, | 132 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_ParseUpdateManifest, |
| 62 OnParseUpdateManifest) | 133 OnParseUpdateManifest) |
| 63 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_UnzipToDir, OnUnzipToDir) | |
| 64 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_UnpackExtension, OnUnpackExtension) | |
| 65 IPC_MESSAGE_UNHANDLED(handled = false) | 134 IPC_MESSAGE_UNHANDLED(handled = false) |
| 66 IPC_END_MESSAGE_MAP() | 135 IPC_END_MESSAGE_MAP() |
| 67 return handled; | 136 return handled; |
| 68 } | 137 } |
| 69 | 138 |
| 70 void UtilityHandler::OnParseUpdateManifest(const std::string& xml) { | 139 void UtilityHandler::OnParseUpdateManifest(const std::string& xml) { |
| 71 UpdateManifest manifest; | 140 UpdateManifest manifest; |
| 72 if (!manifest.Parse(xml)) { | 141 if (!manifest.Parse(xml)) { |
| 73 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Failed( | 142 content::UtilityThread::Get()->Send( |
| 74 manifest.errors())); | 143 new ExtensionUtilityHostMsg_ParseUpdateManifest_Failed( |
| 144 manifest.errors())); | |
| 75 } else { | 145 } else { |
| 76 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Succeeded( | 146 content::UtilityThread::Get()->Send( |
| 77 manifest.results())); | 147 new ExtensionUtilityHostMsg_ParseUpdateManifest_Succeeded( |
| 148 manifest.results())); | |
| 78 } | 149 } |
| 79 ReleaseProcessIfNeeded(); | 150 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
| 80 } | 151 } |
| 81 | 152 |
| 82 void UtilityHandler::OnUnzipToDir(const base::FilePath& zip_path, | |
| 83 const base::FilePath& dir) { | |
| 84 // First extract only the manifest to determine the extension type. | |
| 85 if (!zip::UnzipWithFilterCallback(zip_path, dir, | |
| 86 base::Bind(&Unpacker::IsManifestFile), | |
| 87 false /* log_skipped_files */)) { | |
| 88 Send(new ExtensionUtilityHostMsg_UnzipToDir_Failed( | |
| 89 std::string(kExtensionHandlerUnzipError))); | |
| 90 ReleaseProcessIfNeeded(); | |
| 91 return; | |
| 92 } | |
| 93 | |
| 94 // Load the manifest. | |
| 95 std::string error; | |
| 96 std::unique_ptr<base::DictionaryValue> dict = | |
| 97 Unpacker::ReadManifest(dir, &error); | |
| 98 if (!dict.get()) { | |
| 99 Send(new ExtensionUtilityHostMsg_UnzipToDir_Failed( | |
| 100 std::string(kExtensionHandlerUnzipError))); | |
| 101 ReleaseProcessIfNeeded(); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 Manifest manifest(Manifest::INTERNAL, std::move(dict)); | |
| 106 base::Callback<bool(const base::FilePath&)> filetype_filter_cb = | |
| 107 base::Bind(&Unpacker::ShouldExtractFile, manifest.is_theme()); | |
| 108 | |
| 109 // TODO(crbug.com/645263): This silently ignores blocked file types. | |
| 110 // Add install warnings. | |
| 111 if (!zip::UnzipWithFilterCallback(zip_path, dir, filetype_filter_cb, | |
| 112 true /* log_skipped_files */)) { | |
| 113 Send(new ExtensionUtilityHostMsg_UnzipToDir_Failed( | |
| 114 std::string(kExtensionHandlerUnzipError))); | |
| 115 } else { | |
| 116 Send(new ExtensionUtilityHostMsg_UnzipToDir_Succeeded(dir)); | |
| 117 } | |
| 118 ReleaseProcessIfNeeded(); | |
| 119 } | |
| 120 | |
| 121 void UtilityHandler::OnUnpackExtension(const base::FilePath& directory_path, | |
| 122 const std::string& extension_id, | |
| 123 int location, | |
| 124 int creation_flags) { | |
| 125 CHECK_GT(location, Manifest::INVALID_LOCATION); | |
| 126 CHECK_LT(location, Manifest::NUM_LOCATIONS); | |
| 127 DCHECK(ExtensionsClient::Get()); | |
| 128 content::UtilityThread::Get()->EnsureBlinkInitialized(); | |
| 129 Unpacker unpacker(directory_path.DirName(), directory_path, extension_id, | |
| 130 static_cast<Manifest::Location>(location), creation_flags); | |
| 131 if (unpacker.Run()) { | |
| 132 Send(new ExtensionUtilityHostMsg_UnpackExtension_Succeeded( | |
| 133 *unpacker.parsed_manifest())); | |
| 134 } else { | |
| 135 Send(new ExtensionUtilityHostMsg_UnpackExtension_Failed( | |
| 136 unpacker.error_message())); | |
| 137 } | |
| 138 ReleaseProcessIfNeeded(); | |
| 139 } | |
| 140 | |
| 141 | |
| 142 } // namespace extensions | 153 } // namespace extensions |
| OLD | NEW |