1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_IMAGE_WRITER_UTILITY_CLIENT_H_ 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_IMAGE_WRITER_UTILITY_CLIENT_H_ 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/files/file_path.h" 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/weak_ptr.h" 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/threading/sequenced_worker_pool.h" 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/utility_process_host.h" 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/utility_process_host_client.h" 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Writes a disk image to a device inside the utility process. 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ImageWriterUtilityClient : public content::UtilityProcessHostClient { 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public: 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) typedef base::Callback<void()> CancelCallback; 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) typedef base::Callback<void()> SuccessCallback; 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) typedef base::Callback<void(int64)> ProgressCallback; 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) typedef base::Callback<void(const std::string&)> ErrorCallback; 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ImageWriterUtilityClient(); 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Starts the write. 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |progress_callback|: Called periodically with the count of bytes processed. 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |success_callback|: Called at successful completion. 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |error_callback|: Called with an error message on failure. 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |source|: The path to the source file to read data from. 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |target|: The path to the target device to write the image file to. 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void Write(const ProgressCallback& progress_callback, 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const SuccessCallback& success_callback, 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const ErrorCallback& error_callback, 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const base::FilePath& source, 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const base::FilePath& target); 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Starts a verification. 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |progress_callback|: Called periodically with the count of bytes processed. 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |success_callback|: Called at successful completion. 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |error_callback|: Called with an error message on failure. 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |source|: The path to the source file to read data from. 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |target|: The path to the target device to write the image file to. 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void Verify(const ProgressCallback& progress_callback, 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const SuccessCallback& success_callback, 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const ErrorCallback& error_callback, 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const base::FilePath& source, 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const base::FilePath& target); 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Cancels any pending write or verification. 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // |cancel_callback|: Called when the cancel has actually occurred. 50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void Cancel(const CancelCallback& cancel_callback); 51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Shuts down the Utility thread that may have been created. 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void Shutdown(); 54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) protected: 56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // It's a reference-counted object, so destructor is not public. 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual ~ImageWriterUtilityClient(); 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private: 60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Ensures the UtilityProcessHost has been created. 61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void StartHost(); 62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // UtilityProcessHostClient implementation. 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void OnProcessCrashed(int exit_code) OVERRIDE; 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void OnProcessLaunchFailed() OVERRIDE; 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual bool Send(IPC::Message* msg); 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // IPC message handlers. 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void OnWriteImageSucceeded(); 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void OnWriteImageCancelled(); 72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void OnWriteImageFailed(const std::string& message); 73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void OnWriteImageProgress(int64 progress); 74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) CancelCallback cancel_callback_; 76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ProgressCallback progress_callback_; 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) SuccessCallback success_callback_; 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ErrorCallback error_callback_; 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::WeakPtr<content::UtilityProcessHost> utility_process_host_; 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ImageWriterUtilityClient); 85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_IMAGE_WRITER_UTILITY_CLIENT_H_ 88