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.
5 #include "chrome/browser/media_galleries/fileapi/safe_media_metadata_parser.h"
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"
13 using content::BrowserThread
;
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
,
23 scoped_ptr
<std::string
> data
,
24 int64
/* blob_total_size */) {
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
27 if (!utility_process_host
.get())
29 utility_process_host
->Send(new ChromeUtilityMsg_RequestBlobBytes_Finished(
35 SafeMediaMetadataParser::SafeMediaMetadataParser(Profile
* profile
,
36 const std::string
& blob_uuid
,
38 const std::string
& mime_type
)
40 blob_uuid_(blob_uuid
),
41 blob_size_(blob_size
),
42 mime_type_(mime_type
),
43 parser_state_(INITIAL_STATE
) {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
47 void SafeMediaMetadataParser::Start(const DoneCallback
& callback
) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
50 BrowserThread::PostTask(
53 base::Bind(&SafeMediaMetadataParser::StartWorkOnIOThread
, this,
57 SafeMediaMetadataParser::~SafeMediaMetadataParser() {
60 void SafeMediaMetadataParser::StartWorkOnIOThread(
61 const DoneCallback
& callback
) {
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
63 DCHECK_EQ(INITIAL_STATE
, parser_state_
);
64 DCHECK(!callback
.is_null());
68 utility_process_host_
= content::UtilityProcessHost::Create(
69 this, base::MessageLoopProxy::current())->AsWeakPtr();
71 utility_process_host_
->Send(
72 new ChromeUtilityMsg_ParseMediaMetadata(mime_type_
, blob_size_
));
74 parser_state_
= STARTED_PARSING_STATE
;
77 void SafeMediaMetadataParser::OnParseMediaMetadataFinished(
78 bool parse_success
, const base::DictionaryValue
& metadata_dictionary
) {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
80 DCHECK(!callback_
.is_null());
82 if (parser_state_
!= STARTED_PARSING_STATE
)
85 BrowserThread::PostTask(
88 base::Bind(callback_
, parse_success
,
89 base::Owned(metadata_dictionary
.DeepCopy())));
90 parser_state_
= FINISHED_PARSING_STATE
;
93 void SafeMediaMetadataParser::OnUtilityProcessRequestBlobBytes(
94 int64 request_id
, int64 byte_start
, int64 length
) {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
97 // BlobReader is self-deleting.
98 BlobReader
* reader
= new BlobReader(
101 base::Bind(&OnBlobReaderDone
, utility_process_host_
, request_id
));
102 reader
->SetByteRange(byte_start
, length
);
106 void SafeMediaMetadataParser::OnProcessCrashed(int exit_code
) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
108 DCHECK(!callback_
.is_null());
110 BrowserThread::PostTask(
113 base::Bind(callback_
, false, base::Owned(new base::DictionaryValue
)));
114 parser_state_
= FINISHED_PARSING_STATE
;
117 bool SafeMediaMetadataParser::OnMessageReceived(const IPC::Message
& message
) {
119 IPC_BEGIN_MESSAGE_MAP(SafeMediaMetadataParser
, message
)
121 ChromeUtilityHostMsg_ParseMediaMetadata_Finished
,
122 OnParseMediaMetadataFinished
)
124 ChromeUtilityHostMsg_RequestBlobBytes
,
125 OnUtilityProcessRequestBlobBytes
)
126 IPC_MESSAGE_UNHANDLED(handled
= false)
127 IPC_END_MESSAGE_MAP()
131 } // namespace metadata