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#ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_MEDIA_METADATA_PARSER_H_ 6#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_MEDIA_METADATA_PARSER_H_ 7 8#include <string> 9#include <vector> 10 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" 19 20namespace IPC { 21class Message; 22} 23 24class Profile; 25 26namespace metadata { 27 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. 32class SafeMediaMetadataParser : public content::UtilityProcessHostClient { 33 public: 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)> 39 DoneCallback; 40 41 SafeMediaMetadataParser(Profile* profile, const std::string& blob_uuid, 42 int64 blob_size, const std::string& mime_type, 43 bool get_attached_images); 44 45 // Should be called on the UI thread. |callback| also runs on the UI thread. 46 void Start(const DoneCallback& callback); 47 48 private: 49 enum ParserState { 50 INITIAL_STATE, 51 STARTED_PARSING_STATE, 52 FINISHED_PARSING_STATE, 53 }; 54 55 // Private because content::UtilityProcessHostClient is ref-counted. 56 virtual ~SafeMediaMetadataParser(); 57 58 // Launches the utility process. Must run on the IO thread. 59 void StartWorkOnIOThread(const DoneCallback& callback); 60 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); 66 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, 70 int64 length); 71 void StartBlobReaderOnUIThread(int64 request_id, int64 byte_start, 72 int64 length); 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); 77 78 // UtilityProcessHostClient implementation. 79 // Runs on the IO thread. 80 virtual void OnProcessCrashed(int exit_code) OVERRIDE; 81 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 82 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_; 89 90 DoneCallback callback_; 91 92 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; 93 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_; 97 98 DISALLOW_COPY_AND_ASSIGN(SafeMediaMetadataParser); 99}; 100 101} // namespace metadata 102 103#endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_SAFE_MEDIA_METADATA_PARSER_H_ 104