| 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 "extensions/browser/updater/safe_manifest_parser.h" | 5 #include "extensions/browser/updater/safe_manifest_parser.h" |
| 6 | 6 |
| 7 #include <memory> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 10 #include "base/optional.h" |
| 9 #include "base/location.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/utility_process_host.h" | 12 #include "content/public/browser/utility_process_mojo_client.h" |
| 13 #include "content/public/common/content_switches.h" | 13 #include "extensions/common/manifest_parser.mojom.h" |
| 14 #include "extensions/common/extension_utility_messages.h" | |
| 15 #include "extensions/strings/grit/extensions_strings.h" | 14 #include "extensions/strings/grit/extensions_strings.h" |
| 16 #include "ipc/ipc_message_macros.h" | |
| 17 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 18 | 16 |
| 19 using content::BrowserThread; | 17 namespace { |
| 18 |
| 19 using UtilityProcess = |
| 20 content::UtilityProcessMojoClient<extensions::mojom::ManifestParser>; |
| 21 |
| 22 using ResultCallback = base::Callback<void(const UpdateManifest::Results*)>; |
| 23 |
| 24 void ParseDone(std::unique_ptr<UtilityProcess> /* utility_process */, |
| 25 const ResultCallback& callback, |
| 26 const base::Optional<UpdateManifest::Results>& results) { |
| 27 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 28 |
| 29 callback.Run(results ? &results.value() : nullptr); |
| 30 } |
| 31 |
| 32 } // namespace |
| 20 | 33 |
| 21 namespace extensions { | 34 namespace extensions { |
| 22 | 35 |
| 23 SafeManifestParser::SafeManifestParser(const std::string& xml, | 36 void ParseUpdateManifest(const std::string& xml, |
| 24 const ResultsCallback& results_callback) | 37 const ResultCallback& callback) { |
| 25 : xml_(xml), results_callback_(results_callback) { | 38 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 26 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 39 DCHECK(callback); |
| 27 } | |
| 28 | 40 |
| 29 void SafeManifestParser::Start() { | 41 auto process = base::MakeUnique<UtilityProcess>( |
| 30 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 42 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_MANIFEST_PARSER_NAME)); |
| 31 if (!BrowserThread::PostTask( | 43 auto* utility_process = process.get(); |
| 32 BrowserThread::IO, | 44 auto done = base::Bind(&ParseDone, base::Passed(&process), callback); |
| 33 FROM_HERE, | 45 utility_process->set_error_callback(base::Bind(done, base::nullopt)); |
| 34 base::Bind(&SafeManifestParser::ParseInSandbox, this))) { | |
| 35 NOTREACHED(); | |
| 36 } | |
| 37 } | |
| 38 | 46 |
| 39 SafeManifestParser::~SafeManifestParser() { | 47 utility_process->Start(); |
| 40 // If we're using UtilityProcessHost, we may not be destroyed on | |
| 41 // the UI or IO thread. | |
| 42 } | |
| 43 | 48 |
| 44 void SafeManifestParser::ParseInSandbox() { | 49 utility_process->service()->Parse(xml, done); |
| 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 46 | |
| 47 content::UtilityProcessHost* host = content::UtilityProcessHost::Create( | |
| 48 this, BrowserThread::GetTaskRunnerForThread(BrowserThread::UI).get()); | |
| 49 host->SetName( | |
| 50 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_MANIFEST_PARSER_NAME)); | |
| 51 host->Send(new ExtensionUtilityMsg_ParseUpdateManifest(xml_)); | |
| 52 } | |
| 53 | |
| 54 bool SafeManifestParser::OnMessageReceived(const IPC::Message& message) { | |
| 55 bool handled = true; | |
| 56 IPC_BEGIN_MESSAGE_MAP(SafeManifestParser, message) | |
| 57 IPC_MESSAGE_HANDLER(ExtensionUtilityHostMsg_ParseUpdateManifest_Succeeded, | |
| 58 OnParseUpdateManifestSucceeded) | |
| 59 IPC_MESSAGE_HANDLER(ExtensionUtilityHostMsg_ParseUpdateManifest_Failed, | |
| 60 OnParseUpdateManifestFailed) | |
| 61 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 62 IPC_END_MESSAGE_MAP() | |
| 63 return handled; | |
| 64 } | |
| 65 | |
| 66 void SafeManifestParser::OnParseUpdateManifestSucceeded( | |
| 67 const UpdateManifest::Results& results) { | |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 69 results_callback_.Run(&results); | |
| 70 } | |
| 71 | |
| 72 void SafeManifestParser::OnParseUpdateManifestFailed( | |
| 73 const std::string& error_message) { | |
| 74 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 75 LOG(WARNING) << "Error parsing update manifest:\n" << error_message; | |
| 76 results_callback_.Run(NULL); | |
| 77 } | 50 } |
| 78 | 51 |
| 79 } // namespace extensions | 52 } // namespace extensions |
| OLD | NEW |