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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_MEDIA_METADATA_PARSER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_MEDIA_METADATA_PARSER_H_
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "chrome/common/extensions/api/media_galleries.h"
16 #include "chrome/common/media_galleries/metadata_types.h"
17 #include "content/public/browser/utility_process_host.h"
18 #include "content/public/browser/utility_process_host_client.h"
28 // Parses the media metadata of a Blob safely in a utility process. This class
29 // expects the MIME type of the Blob to be already determined. It spawns a
30 // utility process to do further MIME-type specific metadata extraction.
31 // All public methods and callbacks of this class run on the UI thread.
32 class SafeMediaMetadataParser
: public content::UtilityProcessHostClient
{
34 // |metadata_dictionary| is owned by the callback.
35 typedef base::Callback
<
36 void(bool parse_success
,
37 scoped_ptr
<base::DictionaryValue
> metadata_dictionary
,
38 scoped_ptr
<std::vector
<AttachedImage
> > attached_images
)>
41 SafeMediaMetadataParser(Profile
* profile
, const std::string
& blob_uuid
,
42 int64 blob_size
, const std::string
& mime_type
,
43 bool get_attached_images
);
45 // Should be called on the UI thread. |callback| also runs on the UI thread.
46 void Start(const DoneCallback
& callback
);
51 STARTED_PARSING_STATE
,
52 FINISHED_PARSING_STATE
,
55 // Private because content::UtilityProcessHostClient is ref-counted.
56 ~SafeMediaMetadataParser() override
;
58 // Launches the utility process. Must run on the IO thread.
59 void StartWorkOnIOThread(const DoneCallback
& callback
);
61 // Notification from the utility process when it finishes parsing metadata.
62 // Runs on the IO thread.
63 void OnParseMediaMetadataFinished(
64 bool parse_success
, const base::DictionaryValue
& metadata_dictionary
,
65 const std::vector
<AttachedImage
>& attached_images
);
67 // Sequence of functions that bounces from the IO thread to the UI thread to
68 // read the blob data, then sends the data back to the utility process.
69 void OnUtilityProcessRequestBlobBytes(int64 request_id
, int64 byte_start
,
71 void StartBlobReaderOnUIThread(int64 request_id
, int64 byte_start
,
73 void OnBlobReaderDoneOnUIThread(int64 request_id
,
74 scoped_ptr
<std::string
> data
,
75 int64
/* blob_total_size */);
76 void FinishRequestBlobBytes(int64 request_id
, scoped_ptr
<std::string
> data
);
78 // UtilityProcessHostClient implementation.
79 // Runs on the IO thread.
80 void OnProcessCrashed(int exit_code
) override
;
81 bool OnMessageReceived(const IPC::Message
& message
) override
;
83 // All member variables are only accessed on the IO thread.
84 Profile
* const profile_
;
85 const std::string blob_uuid_
;
86 const int64 blob_size_
;
87 const std::string mime_type_
;
88 bool get_attached_images_
;
90 DoneCallback callback_
;
92 base::WeakPtr
<content::UtilityProcessHost
> utility_process_host_
;
94 // Verifies the messages from the utility process came at the right time.
95 // Initialized on the UI thread, but only accessed on the IO thread.
96 ParserState parser_state_
;
98 DISALLOW_COPY_AND_ASSIGN(SafeMediaMetadataParser
);
101 } // namespace metadata
103 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_MEDIA_METADATA_PARSER_H_