OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_SANDBOXED_EXTENSION_UNPACKER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_SANDBOXED_EXTENSION_UNPACKER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_SANDBOXED_EXTENSION_UNPACKER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_SANDBOXED_EXTENSION_UNPACKER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/utility_process_host.h" | 14 #include "chrome/browser/utility_process_host.h" |
15 | 15 |
16 class Extension; | 16 class Extension; |
17 class MessageLoop; | 17 class MessageLoop; |
18 class ResourceDispatcherHost; | 18 class ResourceDispatcherHost; |
19 | 19 |
20 class SandboxedExtensionUnpackerClient { | 20 class SandboxedExtensionUnpackerClient |
| 21 : public base::RefCountedThreadSafe<SandboxedExtensionUnpackerClient> { |
21 public: | 22 public: |
| 23 virtual ~SandboxedExtensionUnpackerClient(){ |
| 24 } |
| 25 |
22 // temp_dir - A temporary directoy containing the results of the extension | 26 // temp_dir - A temporary directoy containing the results of the extension |
23 // unpacking. The client is responsible for deleting this directory. | 27 // unpacking. The client is responsible for deleting this directory. |
24 // | 28 // |
25 // extension_root - The path to the extension root inside of temp_dir. | 29 // extension_root - The path to the extension root inside of temp_dir. |
26 // | 30 // |
27 // extension - The extension that was unpacked. The client is responsible | 31 // extension - The extension that was unpacked. The client is responsible |
28 // for deleting this memory. | 32 // for deleting this memory. |
29 virtual void OnUnpackSuccess(const FilePath& temp_dir, | 33 virtual void OnUnpackSuccess(const FilePath& temp_dir, |
30 const FilePath& extension_root, | 34 const FilePath& extension_root, |
31 Extension* extension) = 0; | 35 Extension* extension) = 0; |
32 virtual void OnUnpackFailure(const std::string& error) = 0; | 36 virtual void OnUnpackFailure(const std::string& error) = 0; |
33 }; | 37 }; |
34 | 38 |
35 // SandboxedExtensionUnpacker unpacks extensions from the CRX format into a | 39 // SandboxedExtensionUnpacker unpacks extensions from the CRX format into a |
36 // directory. This is done in a sandboxed subprocess to protect the browser | 40 // directory. This is done in a sandboxed subprocess to protect the browser |
37 // process from parsing complex formats like JPEG or JSON from untrusted | 41 // process from parsing complex formats like JPEG or JSON from untrusted |
38 // sources. | 42 // sources. |
39 // | 43 // |
40 // Unpacking an extension using this class makes minor changes to its source, | 44 // Unpacking an extension using this class makes minor changes to its source, |
41 // such as transcoding all images to PNG and rewriting the manifest JSON. As | 45 // such as transcoding all images to PNG and rewriting the manifest JSON. As |
42 // such, it should not be used when the output is not intended to be given back | 46 // such, it should not be used when the output is not intended to be given back |
43 // to the author. | 47 // to the author. |
44 // | 48 // |
| 49 // |
| 50 // Lifetime management: |
| 51 // |
| 52 // This class is ref-counted by each call it makes to itself on another thread, |
| 53 // and by UtilityProcessHost. |
| 54 // |
| 55 // Additionally, we hold a reference to our own client so that it lives at least |
| 56 // long enough to receive the result of unpacking. |
| 57 // |
| 58 // |
45 // NOTE: This class should only be used on the file thread. | 59 // NOTE: This class should only be used on the file thread. |
46 | |
47 | |
48 class SandboxedExtensionUnpacker : public UtilityProcessHost::Client { | 60 class SandboxedExtensionUnpacker : public UtilityProcessHost::Client { |
49 public: | 61 public: |
50 // The size of the magic character sequence at the beginning of each crx | 62 // The size of the magic character sequence at the beginning of each crx |
51 // file, in bytes. This should be a multiple of 4. | 63 // file, in bytes. This should be a multiple of 4. |
52 static const size_t kExtensionHeaderMagicSize = 4; | 64 static const size_t kExtensionHeaderMagicSize = 4; |
53 | 65 |
54 // This header is the first data at the beginning of an extension. Its | 66 // This header is the first data at the beginning of an extension. Its |
55 // contents are purposely 32-bit aligned so that it can just be slurped into | 67 // contents are purposely 32-bit aligned so that it can just be slurped into |
56 // a struct without manual parsing. | 68 // a struct without manual parsing. |
57 struct ExtensionHeader { | 69 struct ExtensionHeader { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 void OnUnpackExtensionSucceeded(const DictionaryValue& manifest); | 119 void OnUnpackExtensionSucceeded(const DictionaryValue& manifest); |
108 void OnUnpackExtensionFailed(const std::string& error_message); | 120 void OnUnpackExtensionFailed(const std::string& error_message); |
109 void OnProcessCrashed(); | 121 void OnProcessCrashed(); |
110 | 122 |
111 void ReportFailure(const std::string& message); | 123 void ReportFailure(const std::string& message); |
112 void ReportSuccess(); | 124 void ReportSuccess(); |
113 | 125 |
114 FilePath crx_path_; | 126 FilePath crx_path_; |
115 MessageLoop* client_loop_; | 127 MessageLoop* client_loop_; |
116 ResourceDispatcherHost* rdh_; | 128 ResourceDispatcherHost* rdh_; |
117 SandboxedExtensionUnpackerClient* client_; | 129 scoped_refptr<SandboxedExtensionUnpackerClient> client_; |
118 ScopedTempDir temp_dir_; | 130 ScopedTempDir temp_dir_; |
119 FilePath extension_root_; | 131 FilePath extension_root_; |
120 scoped_ptr<Extension> extension_; | 132 scoped_ptr<Extension> extension_; |
121 bool got_response_; | 133 bool got_response_; |
122 std::string public_key_; | 134 std::string public_key_; |
123 }; | 135 }; |
124 | 136 |
125 #endif // CHROME_BROWSER_EXTENSIONS_SANDBOXED_EXTENSION_UNPACKER_H_ | 137 #endif // CHROME_BROWSER_EXTENSIONS_SANDBOXED_EXTENSION_UNPACKER_H_ |
OLD | NEW |