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