Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / safe_media_metadata_parser.cc
blob5489c7c05398845a749230a8372dcb664c956425
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;
15 namespace metadata {
17 namespace {
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 int64 /* blob_total_size */) {
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
27 if (!utility_process_host.get())
28 return;
29 utility_process_host->Send(new ChromeUtilityMsg_RequestBlobBytes_Finished(
30 request_id, *data));
33 } // namespace
35 SafeMediaMetadataParser::SafeMediaMetadataParser(Profile* profile,
36 const std::string& blob_uuid,
37 int64 blob_size,
38 const std::string& mime_type)
39 : profile_(profile),
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(
51 BrowserThread::IO,
52 FROM_HERE,
53 base::Bind(&SafeMediaMetadataParser::StartWorkOnIOThread, this,
54 callback));
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());
66 callback_ = callback;
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)
83 return;
85 BrowserThread::PostTask(
86 BrowserThread::UI,
87 FROM_HERE,
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(
99 profile_,
100 blob_uuid_,
101 base::Bind(&OnBlobReaderDone, utility_process_host_, request_id));
102 reader->SetByteRange(byte_start, length);
103 reader->Start();
106 void SafeMediaMetadataParser::OnProcessCrashed(int exit_code) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
108 DCHECK(!callback_.is_null());
110 BrowserThread::PostTask(
111 BrowserThread::UI,
112 FROM_HERE,
113 base::Bind(callback_, false, base::Owned(new base::DictionaryValue)));
114 parser_state_ = FINISHED_PARSING_STATE;
117 bool SafeMediaMetadataParser::OnMessageReceived(const IPC::Message& message) {
118 bool handled = true;
119 IPC_BEGIN_MESSAGE_MAP(SafeMediaMetadataParser, message)
120 IPC_MESSAGE_HANDLER(
121 ChromeUtilityHostMsg_ParseMediaMetadata_Finished,
122 OnParseMediaMetadataFinished)
123 IPC_MESSAGE_HANDLER(
124 ChromeUtilityHostMsg_RequestBlobBytes,
125 OnUtilityProcessRequestBlobBytes)
126 IPC_MESSAGE_UNHANDLED(handled = false)
127 IPC_END_MESSAGE_MAP()
128 return handled;
131 } // namespace metadata