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