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 SafeMediaMetadataParser::SafeMediaMetadataParser(Profile* profile, |
| 18 const std::string& blob_uuid, |
| 19 int64 blob_size, |
| 20 const std::string& mime_type) |
| 21 : profile_(profile), |
| 22 blob_uuid_(blob_uuid), |
| 23 blob_size_(blob_size), |
| 24 mime_type_(mime_type), |
| 25 parser_state_(INITIAL_STATE), |
| 26 weak_factory_(this) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 28 } |
| 29 |
| 30 void SafeMediaMetadataParser::Start(const DoneCallback& callback) { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 DCHECK(!callback.is_null()); |
| 33 |
| 34 callback_ = callback; |
| 35 |
| 36 BrowserThread::PostTask( |
| 37 BrowserThread::IO, |
| 38 FROM_HERE, |
| 39 base::Bind(&SafeMediaMetadataParser::StartWorkOnIOThread, this)); |
| 40 } |
| 41 |
| 42 SafeMediaMetadataParser::~SafeMediaMetadataParser() { |
| 43 } |
| 44 |
| 45 void SafeMediaMetadataParser::StartWorkOnIOThread() { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 47 DCHECK_EQ(INITIAL_STATE, parser_state_); |
| 48 |
| 49 utility_process_host_ = content::UtilityProcessHost::Create( |
| 50 this, base::MessageLoopProxy::current())->AsWeakPtr(); |
| 51 |
| 52 utility_process_host_->Send( |
| 53 new ChromeUtilityMsg_ParseMediaMetadata(mime_type_, blob_size_)); |
| 54 |
| 55 parser_state_ = STARTED_PARSING_STATE; |
| 56 } |
| 57 |
| 58 void SafeMediaMetadataParser::OnParseMediaMetadataFinished( |
| 59 bool parse_success, |
| 60 const base::DictionaryValue& metadata_dictionary) { |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 62 DCHECK(!callback_.is_null()); |
| 63 |
| 64 if (parser_state_ != STARTED_PARSING_STATE) |
| 65 return; |
| 66 |
| 67 BrowserThread::PostTask( |
| 68 BrowserThread::UI, |
| 69 FROM_HERE, |
| 70 base::Bind(callback_, parse_success, base::Passed( |
| 71 make_scoped_ptr(metadata_dictionary.DeepCopy())))); |
| 72 parser_state_ = FINISHED_PARSING_STATE; |
| 73 } |
| 74 |
| 75 void SafeMediaMetadataParser::OnUtilityProcessRequestBlobBytes( |
| 76 int64 request_id, |
| 77 int64 byte_start, |
| 78 int64 length) { |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 80 |
| 81 // BlobReader is self-deleting. |
| 82 BlobReader* reader = new BlobReader( |
| 83 profile_, |
| 84 blob_uuid_, |
| 85 base::Bind(&SafeMediaMetadataParser::OnBlobReaderDone, |
| 86 weak_factory_.GetWeakPtr(), |
| 87 request_id)); |
| 88 reader->SetByteRange(byte_start, length); |
| 89 reader->Start(); |
| 90 } |
| 91 |
| 92 void SafeMediaMetadataParser::OnBlobReaderDone( |
| 93 int64 request_id, |
| 94 scoped_ptr<std::string> data) { |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 96 |
| 97 utility_process_host_->Send(new ChromeUtilityMsg_RequestBlobBytes_Finished( |
| 98 request_id, *data)); |
| 99 } |
| 100 |
| 101 void SafeMediaMetadataParser::OnProcessCrashed(int exit_code) { |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 103 DCHECK(!callback_.is_null()); |
| 104 |
| 105 BrowserThread::PostTask( |
| 106 BrowserThread::UI, |
| 107 FROM_HERE, |
| 108 base::Bind(callback_, false, |
| 109 base::Passed(scoped_ptr<base::DictionaryValue>()))); |
| 110 parser_state_ = FINISHED_PARSING_STATE; |
| 111 } |
| 112 |
| 113 bool SafeMediaMetadataParser::OnMessageReceived( |
| 114 const IPC::Message& message) { |
| 115 bool handled = true; |
| 116 IPC_BEGIN_MESSAGE_MAP(SafeMediaMetadataParser, message) |
| 117 IPC_MESSAGE_HANDLER( |
| 118 ChromeUtilityHostMsg_ParseMediaMetadata_Finished, |
| 119 OnParseMediaMetadataFinished) |
| 120 IPC_MESSAGE_HANDLER( |
| 121 ChromeUtilityHostMsg_RequestBlobBytes, |
| 122 OnUtilityProcessRequestBlobBytes) |
| 123 IPC_MESSAGE_UNHANDLED(handled = false) |
| 124 IPC_END_MESSAGE_MAP() |
| 125 return handled; |
| 126 } |
| 127 |
| 128 } // namespace metadata |
OLD | NEW |