1// Copyright (c) 2011 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_PACK_EXTENSION_JOB_H_
6#define CHROME_BROWSER_EXTENSIONS_PACK_EXTENSION_JOB_H_
7#pragma once
8
9#include <string>
10
11#include "base/file_path.h"
12#include "base/memory/ref_counted.h"
13#include "base/string16.h"
14#include "content/browser/browser_thread.h"
15
16
17// Manages packing an extension on the file thread and reporting the result
18// back to the UI.
19class PackExtensionJob : public base::RefCountedThreadSafe<PackExtensionJob> {
20 public:
21
22  // Interface for people who want to use PackExtensionJob to implement.
23  class Client {
24   public:
25    virtual void OnPackSuccess(const FilePath& crx_file,
26                               const FilePath& key_file) = 0;
27    virtual void OnPackFailure(const std::string& message) = 0;
28
29   protected:
30    virtual ~Client() {}
31  };
32
33  PackExtensionJob(Client* client,
34                   const FilePath& root_directory,
35                   const FilePath& key_file);
36
37  // Starts the packing job.
38  void Start();
39
40  // The client should call this when it is destroyed to prevent
41  // PackExtensionJob from attempting to access it.
42  void ClearClient();
43
44  // The standard packing success message.
45  static string16 StandardSuccessMessage(const FilePath& crx_file,
46                                         const FilePath& key_file);
47
48  void set_asynchronous(bool async) { asynchronous_ = async; }
49
50 private:
51  friend class base::RefCountedThreadSafe<PackExtensionJob>;
52
53  virtual ~PackExtensionJob();
54
55  // If |asynchronous_| is false, this is run on whichever thread calls it.
56  void Run();
57  void ReportSuccessOnClientThread();
58  void ReportFailureOnClientThread(const std::string& error);
59
60  BrowserThread::ID client_thread_id_;
61  Client* client_;
62  FilePath root_directory_;
63  FilePath key_file_;
64  FilePath crx_file_out_;
65  FilePath key_file_out_;
66  bool asynchronous_;
67
68  DISALLOW_COPY_AND_ASSIGN(PackExtensionJob);
69};
70
71#endif  // CHROME_BROWSER_EXTENSIONS_PACK_EXTENSION_JOB_H_
72