OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/media_galleries/fileapi/safe_media_metadata_parser.h" |
| 6 |
| 7 #include "chrome/browser/extensions/blob_reader.h" |
| 8 #include "chrome/common/chrome_utility_messages.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/child_process_data.h" |
| 11 #include "content/public/browser/utility_process_host.h" |
| 12 |
| 13 using content::BrowserThread; |
| 14 |
| 15 namespace metadata { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Completes the Blob byte request by forwarding it to the utility process. |
| 20 void OnBlobReaderDone( |
| 21 const base::WeakPtr<content::UtilityProcessHost>& utility_process_host, |
| 22 int64 request_id, |
| 23 scoped_ptr<std::string> data) { |
| 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 25 |
| 26 if (!utility_process_host.get()) |
| 27 return; |
| 28 utility_process_host->Send(new ChromeUtilityMsg_RequestBlobBytes_Finished( |
| 29 request_id, *data)); |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 SafeMediaMetadataParser::SafeMediaMetadataParser(Profile* profile, |
| 35 const std::string& blob_uuid, |
| 36 int64 blob_size, |
| 37 const std::string& mime_type) |
| 38 : profile_(profile), |
| 39 blob_uuid_(blob_uuid), |
| 40 blob_size_(blob_size), |
| 41 mime_type_(mime_type), |
| 42 parser_state_(INITIAL_STATE) { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 44 } |
| 45 |
| 46 void SafeMediaMetadataParser::Start(const DoneCallback& callback) { |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 48 |
| 49 BrowserThread::PostTask( |
| 50 BrowserThread::IO, |
| 51 FROM_HERE, |
| 52 base::Bind(&SafeMediaMetadataParser::StartWorkOnIOThread, this, |
| 53 callback)); |
| 54 } |
| 55 |
| 56 SafeMediaMetadataParser::~SafeMediaMetadataParser() { |
| 57 } |
| 58 |
| 59 void SafeMediaMetadataParser::StartWorkOnIOThread( |
| 60 const DoneCallback& callback) { |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 62 DCHECK_EQ(INITIAL_STATE, parser_state_); |
| 63 DCHECK(!callback.is_null()); |
| 64 |
| 65 callback_ = callback; |
| 66 |
| 67 utility_process_host_ = content::UtilityProcessHost::Create( |
| 68 this, base::MessageLoopProxy::current())->AsWeakPtr(); |
| 69 |
| 70 utility_process_host_->Send( |
| 71 new ChromeUtilityMsg_ParseMediaMetadata(mime_type_, blob_size_)); |
| 72 |
| 73 parser_state_ = STARTED_PARSING_STATE; |
| 74 } |
| 75 |
| 76 void SafeMediaMetadataParser::OnParseMediaMetadataFinished( |
| 77 bool parse_success, const base::DictionaryValue& metadata_dictionary) { |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 79 DCHECK(!callback_.is_null()); |
| 80 |
| 81 if (parser_state_ != STARTED_PARSING_STATE) |
| 82 return; |
| 83 |
| 84 BrowserThread::PostTask( |
| 85 BrowserThread::UI, |
| 86 FROM_HERE, |
| 87 base::Bind(callback_, parse_success, |
| 88 base::Owned(metadata_dictionary.DeepCopy()))); |
| 89 parser_state_ = FINISHED_PARSING_STATE; |
| 90 } |
| 91 |
| 92 void SafeMediaMetadataParser::OnUtilityProcessRequestBlobBytes( |
| 93 int64 request_id, int64 byte_start, int64 length) { |
| 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 95 |
| 96 // BlobReader is self-deleting. |
| 97 BlobReader* reader = new BlobReader( |
| 98 profile_, |
| 99 blob_uuid_, |
| 100 base::Bind(&OnBlobReaderDone, utility_process_host_, request_id)); |
| 101 reader->SetByteRange(byte_start, length); |
| 102 reader->Start(); |
| 103 } |
| 104 |
| 105 void SafeMediaMetadataParser::OnProcessCrashed(int exit_code) { |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 107 DCHECK(!callback_.is_null()); |
| 108 |
| 109 BrowserThread::PostTask( |
| 110 BrowserThread::UI, |
| 111 FROM_HERE, |
| 112 base::Bind(callback_, false, base::Owned(new base::DictionaryValue))); |
| 113 parser_state_ = FINISHED_PARSING_STATE; |
| 114 } |
| 115 |
| 116 bool SafeMediaMetadataParser::OnMessageReceived(const IPC::Message& message) { |
| 117 bool handled = true; |
| 118 IPC_BEGIN_MESSAGE_MAP(SafeMediaMetadataParser, message) |
| 119 IPC_MESSAGE_HANDLER( |
| 120 ChromeUtilityHostMsg_ParseMediaMetadata_Finished, |
| 121 OnParseMediaMetadataFinished) |
| 122 IPC_MESSAGE_HANDLER( |
| 123 ChromeUtilityHostMsg_RequestBlobBytes, |
| 124 OnUtilityProcessRequestBlobBytes) |
| 125 IPC_MESSAGE_UNHANDLED(handled = false) |
| 126 IPC_END_MESSAGE_MAP() |
| 127 return handled; |
| 128 } |
| 129 |
| 130 } // namespace metadata |
OLD | NEW |