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/extensions/chrome_utility_extensions_messages.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/child_process_data.h"
12 #include "content/public/browser/utility_process_host.h"
13 #include "ui/base/l10n/l10n_util.h"
15 using content::BrowserThread
;
19 SafeMediaMetadataParser::SafeMediaMetadataParser(
20 Profile
* profile
, const std::string
& blob_uuid
, int64 blob_size
,
21 const std::string
& mime_type
, bool get_attached_images
)
23 blob_uuid_(blob_uuid
),
24 blob_size_(blob_size
),
25 mime_type_(mime_type
),
26 get_attached_images_(get_attached_images
),
27 parser_state_(INITIAL_STATE
) {
28 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
31 void SafeMediaMetadataParser::Start(const DoneCallback
& callback
) {
32 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
34 BrowserThread::PostTask(
37 base::Bind(&SafeMediaMetadataParser::StartWorkOnIOThread
, this,
41 SafeMediaMetadataParser::~SafeMediaMetadataParser() {
44 void SafeMediaMetadataParser::StartWorkOnIOThread(
45 const DoneCallback
& callback
) {
46 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
47 DCHECK_EQ(INITIAL_STATE
, parser_state_
);
48 DCHECK(!callback
.is_null());
52 utility_process_host_
= content::UtilityProcessHost::Create(
53 this, base::MessageLoopProxy::current())->AsWeakPtr();
54 utility_process_host_
->SetName(l10n_util::GetStringUTF16(
55 IDS_UTILITY_PROCESS_MEDIA_FILE_CHECKER_NAME
));
57 utility_process_host_
->Send(
58 new ChromeUtilityMsg_ParseMediaMetadata(mime_type_
, blob_size_
,
59 get_attached_images_
));
61 parser_state_
= STARTED_PARSING_STATE
;
64 void SafeMediaMetadataParser::OnParseMediaMetadataFinished(
65 bool parse_success
, const base::DictionaryValue
& metadata_dictionary
,
66 const std::vector
<AttachedImage
>& attached_images
) {
67 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
68 DCHECK(!callback_
.is_null());
70 if (parser_state_
!= STARTED_PARSING_STATE
)
73 // We need to make a scoped copy of this vector since it will be destroyed
74 // at the end of the IPC message handler.
75 scoped_ptr
<std::vector
<metadata::AttachedImage
> > attached_images_copy
=
76 make_scoped_ptr(new std::vector
<metadata::AttachedImage
>(
79 BrowserThread::PostTask(
82 base::Bind(callback_
, parse_success
,
83 base::Passed(make_scoped_ptr(metadata_dictionary
.DeepCopy())),
84 base::Passed(&attached_images_copy
)));
85 parser_state_
= FINISHED_PARSING_STATE
;
88 void SafeMediaMetadataParser::OnUtilityProcessRequestBlobBytes(
89 int64 request_id
, int64 byte_start
, int64 length
) {
90 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
91 BrowserThread::PostTask(
94 base::Bind(&SafeMediaMetadataParser::StartBlobReaderOnUIThread
, this,
95 request_id
, byte_start
, length
));
98 void SafeMediaMetadataParser::StartBlobReaderOnUIThread(
99 int64 request_id
, int64 byte_start
, int64 length
) {
100 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
102 // BlobReader is self-deleting.
103 BlobReader
* reader
= new BlobReader(profile_
, blob_uuid_
, base::Bind(
104 &SafeMediaMetadataParser::OnBlobReaderDoneOnUIThread
, this, request_id
));
105 reader
->SetByteRange(byte_start
, length
);
109 void SafeMediaMetadataParser::OnBlobReaderDoneOnUIThread(
110 int64 request_id
, scoped_ptr
<std::string
> data
,
111 int64
/* blob_total_size */) {
112 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
113 BrowserThread::PostTask(
116 base::Bind(&SafeMediaMetadataParser::FinishRequestBlobBytes
, this,
117 request_id
, base::Passed(data
.Pass())));
120 void SafeMediaMetadataParser::FinishRequestBlobBytes(
121 int64 request_id
, scoped_ptr
<std::string
> data
) {
122 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
123 if (!utility_process_host_
.get())
125 utility_process_host_
->Send(new ChromeUtilityMsg_RequestBlobBytes_Finished(
129 void SafeMediaMetadataParser::OnProcessCrashed(int exit_code
) {
130 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
131 DCHECK(!callback_
.is_null());
133 BrowserThread::PostTask(
136 base::Bind(callback_
, false,
137 base::Passed(scoped_ptr
<base::DictionaryValue
>()),
138 base::Passed(scoped_ptr
<std::vector
<AttachedImage
> >())));
139 parser_state_
= FINISHED_PARSING_STATE
;
142 bool SafeMediaMetadataParser::OnMessageReceived(const IPC::Message
& message
) {
144 IPC_BEGIN_MESSAGE_MAP(SafeMediaMetadataParser
, message
)
146 ChromeUtilityHostMsg_ParseMediaMetadata_Finished
,
147 OnParseMediaMetadataFinished
)
149 ChromeUtilityHostMsg_RequestBlobBytes
,
150 OnUtilityProcessRequestBlobBytes
)
151 IPC_MESSAGE_UNHANDLED(handled
= false)
152 IPC_END_MESSAGE_MAP()
156 } // namespace metadata