| 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 "chrome/browser/media_galleries/fileapi/safe_media_metadata_parser.h" | 5 #include "chrome/browser/media_galleries/fileapi/safe_media_metadata_parser.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/single_thread_task_runner.h" | |
| 11 #include "base/threading/thread_task_runner_handle.h" | |
| 12 #include "chrome/browser/extensions/blob_reader.h" | 10 #include "chrome/browser/extensions/blob_reader.h" |
| 13 #include "chrome/common/extensions/chrome_utility_extensions_messages.h" | |
| 14 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 15 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/child_process_data.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
| 17 #include "content/public/browser/utility_process_host.h" | |
| 18 #include "services/service_manager/public/cpp/interface_provider.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 20 | 15 |
| 21 using content::BrowserThread; | 16 using content::BrowserThread; |
| 22 | 17 |
| 23 namespace metadata { | 18 namespace metadata { |
| 24 | 19 |
| 20 class SafeMediaMetadataParser::MediaDataSourceImpl |
| 21 : public extensions::mojom::MediaDataSource { |
| 22 public: |
| 23 MediaDataSourceImpl(SafeMediaMetadataParser* owner, |
| 24 extensions::mojom::MediaDataSourcePtr* interface) |
| 25 : binding_(this, interface), safe_media_metadata_parser_(owner) { |
| 26 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 27 } |
| 28 |
| 29 ~MediaDataSourceImpl() override = default; |
| 30 |
| 31 private: |
| 32 void ReadBlob(int64_t request_id, |
| 33 int64_t position, |
| 34 int64_t length, |
| 35 const ReadBlobCallback& callback) override { |
| 36 safe_media_metadata_parser_->ProcessBlobRequest(callback, request_id, |
| 37 position, length); |
| 38 } |
| 39 |
| 40 mojo::Binding<extensions::mojom::MediaDataSource> binding_; |
| 41 // |safe_media_metadata_parser_| owns |this|. |
| 42 SafeMediaMetadataParser* const safe_media_metadata_parser_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(MediaDataSourceImpl); |
| 45 }; |
| 46 |
| 25 SafeMediaMetadataParser::SafeMediaMetadataParser(Profile* profile, | 47 SafeMediaMetadataParser::SafeMediaMetadataParser(Profile* profile, |
| 26 const std::string& blob_uuid, | 48 const std::string& blob_uuid, |
| 27 int64_t blob_size, | 49 int64_t blob_size, |
| 28 const std::string& mime_type, | 50 const std::string& mime_type, |
| 29 bool get_attached_images) | 51 bool get_attached_images) |
| 30 : profile_(profile), | 52 : profile_(profile), |
| 31 blob_uuid_(blob_uuid), | 53 blob_uuid_(blob_uuid), |
| 32 blob_size_(blob_size), | 54 blob_size_(blob_size), |
| 33 mime_type_(mime_type), | 55 mime_type_(mime_type), |
| 34 get_attached_images_(get_attached_images), | 56 get_attached_images_(get_attached_images) { |
| 35 parser_state_(INITIAL_STATE) { | |
| 36 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 37 } | 58 } |
| 38 | 59 |
| 39 void SafeMediaMetadataParser::Start(const DoneCallback& callback) { | 60 void SafeMediaMetadataParser::Start(const DoneCallback& callback) { |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 41 | 62 |
| 42 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
| 43 BrowserThread::IO, | 64 BrowserThread::IO, FROM_HERE, |
| 44 FROM_HERE, | |
| 45 base::Bind(&SafeMediaMetadataParser::StartWorkOnIOThread, this, | 65 base::Bind(&SafeMediaMetadataParser::StartWorkOnIOThread, this, |
| 46 callback)); | 66 callback)); |
| 47 } | 67 } |
| 48 | 68 |
| 49 SafeMediaMetadataParser::~SafeMediaMetadataParser() { | 69 SafeMediaMetadataParser::~SafeMediaMetadataParser() = default; |
| 50 } | |
| 51 | 70 |
| 52 void SafeMediaMetadataParser::StartWorkOnIOThread( | 71 void SafeMediaMetadataParser::StartWorkOnIOThread( |
| 53 const DoneCallback& callback) { | 72 const DoneCallback& callback) { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 73 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 55 DCHECK_EQ(INITIAL_STATE, parser_state_); | |
| 56 DCHECK(!callback.is_null()); | 74 DCHECK(!callback.is_null()); |
| 57 | 75 |
| 58 callback_ = callback; | 76 callback_ = callback; |
| 59 | 77 |
| 60 utility_process_host_ = content::UtilityProcessHost::Create( | 78 const base::string16 utility_process_name = |
| 61 this, base::ThreadTaskRunnerHandle::Get())->AsWeakPtr(); | 79 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_MEDIA_FILE_CHECKER_NAME); |
| 62 utility_process_host_->SetName(l10n_util::GetStringUTF16( | |
| 63 IDS_UTILITY_PROCESS_MEDIA_FILE_CHECKER_NAME)); | |
| 64 utility_process_host_->Start(); | |
| 65 | 80 |
| 66 parser_state_ = STARTED_PARSING_STATE; | 81 utility_process_mojo_client_.reset( |
| 67 | 82 new content::UtilityProcessMojoClient<extensions::mojom::MediaParser>( |
| 68 utility_process_host_->GetRemoteInterfaces()->GetInterface(&interface_); | 83 utility_process_name)); |
| 69 | 84 utility_process_mojo_client_->set_error_callback( |
| 70 interface_.set_connection_error_handler( | |
| 71 base::Bind(&SafeMediaMetadataParser::ParseMediaMetadataFailed, this)); | 85 base::Bind(&SafeMediaMetadataParser::ParseMediaMetadataFailed, this)); |
| 72 | 86 |
| 73 interface_->ParseMediaMetadata( | 87 utility_process_mojo_client_->Start(); // Start the utility process. |
| 74 mime_type_, blob_size_, get_attached_images_, | 88 |
| 89 extensions::mojom::MediaDataSourcePtr source; |
| 90 media_data_source_ = base::MakeUnique<MediaDataSourceImpl>(this, &source); |
| 91 |
| 92 utility_process_mojo_client_->service()->ParseMediaMetadata( |
| 93 mime_type_, blob_size_, get_attached_images_, std::move(source), |
| 75 base::Bind(&SafeMediaMetadataParser::ParseMediaMetadataDone, this)); | 94 base::Bind(&SafeMediaMetadataParser::ParseMediaMetadataDone, this)); |
| 76 } | 95 } |
| 77 | 96 |
| 78 void SafeMediaMetadataParser::ParseMediaMetadataFailed() { | 97 void SafeMediaMetadataParser::ParseMediaMetadataFailed() { |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 98 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 80 DCHECK_EQ(parser_state_, STARTED_PARSING_STATE); | |
| 81 DCHECK(!callback_.is_null()); | |
| 82 | 99 |
| 83 interface_.reset(); | 100 utility_process_mojo_client_.reset(); // Terminate the utility process. |
| 101 media_data_source_.reset(); |
| 84 | 102 |
| 85 BrowserThread::PostTask( | 103 BrowserThread::PostTask( |
| 86 BrowserThread::UI, FROM_HERE, | 104 BrowserThread::UI, FROM_HERE, |
| 87 base::Bind(callback_, false, | 105 base::Bind(callback_, false, |
| 88 base::Passed(std::unique_ptr<base::DictionaryValue>()), | 106 base::Passed(std::unique_ptr<base::DictionaryValue>()), |
| 89 base::Passed(std::unique_ptr<std::vector<AttachedImage>>()))); | 107 base::Passed(std::unique_ptr<std::vector<AttachedImage>>()))); |
| 90 | |
| 91 parser_state_ = FINISHED_PARSING_STATE; | |
| 92 } | 108 } |
| 93 | 109 |
| 94 void SafeMediaMetadataParser::ParseMediaMetadataDone( | 110 void SafeMediaMetadataParser::ParseMediaMetadataDone( |
| 95 bool parse_success, | 111 bool parse_success, |
| 96 std::unique_ptr<base::DictionaryValue> metadata_dictionary, | 112 std::unique_ptr<base::DictionaryValue> metadata_dictionary, |
| 97 const std::vector<AttachedImage>& attached_images) { | 113 const std::vector<AttachedImage>& attached_images) { |
| 98 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 99 DCHECK_EQ(parser_state_, STARTED_PARSING_STATE); | |
| 100 DCHECK(!callback_.is_null()); | |
| 101 | 115 |
| 102 interface_.reset(); | 116 utility_process_mojo_client_.reset(); // Terminate the utility process. |
| 117 media_data_source_.reset(); |
| 103 | 118 |
| 104 // We need to make a scoped copy of this vector since it will be destroyed | 119 // We need to make a scoped copy of this vector since it will be destroyed |
| 105 // at the end of the handler. | 120 // at the end of the handler. |
| 106 std::unique_ptr<std::vector<metadata::AttachedImage>> attached_images_copy = | 121 std::unique_ptr<std::vector<metadata::AttachedImage>> attached_images_copy = |
| 107 base::MakeUnique<std::vector<metadata::AttachedImage>>(attached_images); | 122 base::MakeUnique<std::vector<metadata::AttachedImage>>(attached_images); |
| 108 | 123 |
| 109 BrowserThread::PostTask( | 124 BrowserThread::PostTask( |
| 110 BrowserThread::UI, FROM_HERE, | 125 BrowserThread::UI, FROM_HERE, |
| 111 base::Bind(callback_, parse_success, base::Passed(&metadata_dictionary), | 126 base::Bind(callback_, parse_success, base::Passed(&metadata_dictionary), |
| 112 base::Passed(&attached_images_copy))); | 127 base::Passed(&attached_images_copy))); |
| 113 | |
| 114 parser_state_ = FINISHED_PARSING_STATE; | |
| 115 } | 128 } |
| 116 | 129 |
| 117 void SafeMediaMetadataParser::OnUtilityProcessRequestBlobBytes( | 130 void SafeMediaMetadataParser::ProcessBlobRequest( |
| 131 const extensions::mojom::MediaDataSource::ReadBlobCallback& callback, |
| 118 int64_t request_id, | 132 int64_t request_id, |
| 119 int64_t byte_start, | 133 int64_t position, |
| 120 int64_t length) { | 134 int64_t length) { |
| 121 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 135 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 136 |
| 122 BrowserThread::PostTask( | 137 BrowserThread::PostTask( |
| 123 BrowserThread::UI, | 138 BrowserThread::UI, FROM_HERE, |
| 124 FROM_HERE, | |
| 125 base::Bind(&SafeMediaMetadataParser::StartBlobReaderOnUIThread, this, | 139 base::Bind(&SafeMediaMetadataParser::StartBlobReaderOnUIThread, this, |
| 126 request_id, byte_start, length)); | 140 callback, request_id, position, length)); |
| 127 } | 141 } |
| 128 | 142 |
| 129 void SafeMediaMetadataParser::StartBlobReaderOnUIThread(int64_t request_id, | 143 void SafeMediaMetadataParser::StartBlobReaderOnUIThread( |
| 130 int64_t byte_start, | 144 const extensions::mojom::MediaDataSource::ReadBlobCallback& callback, |
| 131 int64_t length) { | 145 int64_t request_id, |
| 146 int64_t position, |
| 147 int64_t length) { |
| 132 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 148 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 133 | 149 |
| 134 // BlobReader is self-deleting. | 150 BlobReader* reader = new BlobReader( // BlobReader is self-deleting. |
| 135 BlobReader* reader = new BlobReader(profile_, blob_uuid_, base::Bind( | 151 profile_, blob_uuid_, |
| 136 &SafeMediaMetadataParser::OnBlobReaderDoneOnUIThread, this, request_id)); | 152 base::Bind(&SafeMediaMetadataParser::BlobReaderDoneOnUIThread, this, |
| 137 reader->SetByteRange(byte_start, length); | 153 callback, request_id)); |
| 154 reader->SetByteRange(position, length); |
| 138 reader->Start(); | 155 reader->Start(); |
| 139 } | 156 } |
| 140 | 157 |
| 141 void SafeMediaMetadataParser::OnBlobReaderDoneOnUIThread( | 158 void SafeMediaMetadataParser::BlobReaderDoneOnUIThread( |
| 159 const extensions::mojom::MediaDataSource::ReadBlobCallback& callback, |
| 142 int64_t request_id, | 160 int64_t request_id, |
| 143 std::unique_ptr<std::string> data, | 161 std::unique_ptr<std::string> data, |
| 144 int64_t /* blob_total_size */) { | 162 int64_t /* blob_total_size */) { |
| 145 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 163 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 164 |
| 146 BrowserThread::PostTask( | 165 BrowserThread::PostTask( |
| 147 BrowserThread::IO, FROM_HERE, | 166 BrowserThread::IO, FROM_HERE, |
| 148 base::Bind(&SafeMediaMetadataParser::FinishRequestBlobBytes, this, | 167 base::Bind(&SafeMediaMetadataParser::FinishBlobRequest, this, callback, |
| 149 request_id, base::Passed(std::move(data)))); | 168 request_id, base::Passed(std::move(data)))); |
| 150 } | 169 } |
| 151 | 170 |
| 152 void SafeMediaMetadataParser::FinishRequestBlobBytes( | 171 void SafeMediaMetadataParser::FinishBlobRequest( |
| 172 const extensions::mojom::MediaDataSource::ReadBlobCallback& callback, |
| 153 int64_t request_id, | 173 int64_t request_id, |
| 154 std::unique_ptr<std::string> data) { | 174 std::unique_ptr<std::string> data) { |
| 155 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 175 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 156 if (!utility_process_host_.get()) | |
| 157 return; | |
| 158 utility_process_host_->Send(new ChromeUtilityMsg_RequestBlobBytes_Finished( | |
| 159 request_id, *data)); | |
| 160 } | |
| 161 | 176 |
| 162 bool SafeMediaMetadataParser::OnMessageReceived(const IPC::Message& message) { | 177 if (utility_process_mojo_client_) { |
| 163 bool handled = true; | 178 const std::vector<uint8_t> blob(data->begin(), data->end()); |
| 164 IPC_BEGIN_MESSAGE_MAP(SafeMediaMetadataParser, message) | 179 callback.Run(request_id, blob); |
| 165 IPC_MESSAGE_HANDLER( | 180 } |
| 166 ChromeUtilityHostMsg_RequestBlobBytes, | |
| 167 OnUtilityProcessRequestBlobBytes) | |
| 168 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 169 IPC_END_MESSAGE_MAP() | |
| 170 return handled; | |
| 171 } | 181 } |
| 172 | 182 |
| 173 } // namespace metadata | 183 } // namespace metadata |
| OLD | NEW |