OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/extensions/convert_user_script.h" | 5 #include "chrome/browser/extensions/convert_user_script.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/scoped_temp_dir.h" | 14 #include "base/scoped_temp_dir.h" |
15 #include "base/sha2.h" | 15 #include "base/sha2.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "chrome/browser/extensions/user_script_master.h" | 17 #include "chrome/browser/extensions/user_script_master.h" |
18 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
19 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
20 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
| 21 #include "chrome/common/extensions/extension_file_util.h" |
21 #include "chrome/common/extensions/user_script.h" | 22 #include "chrome/common/extensions/user_script.h" |
22 #include "chrome/common/json_value_serializer.h" | 23 #include "chrome/common/json_value_serializer.h" |
23 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
24 | 25 |
25 namespace keys = extension_manifest_keys; | 26 namespace keys = extension_manifest_keys; |
26 | 27 |
27 scoped_refptr<Extension> ConvertUserScriptToExtension( | 28 scoped_refptr<Extension> ConvertUserScriptToExtension( |
28 const FilePath& user_script_path, const GURL& original_url, | 29 const FilePath& user_script_path, const GURL& original_url, |
29 std::string* error) { | 30 std::string* error) { |
30 std::string content; | 31 std::string content; |
31 if (!file_util::ReadFileToString(user_script_path, &content)) { | 32 if (!file_util::ReadFileToString(user_script_path, &content)) { |
32 *error = "Could not read source file."; | 33 *error = "Could not read source file."; |
33 return NULL; | 34 return NULL; |
34 } | 35 } |
35 | 36 |
36 if (!IsStringUTF8(content)) { | 37 if (!IsStringUTF8(content)) { |
37 *error = "User script must be UTF8 encoded."; | 38 *error = "User script must be UTF8 encoded."; |
38 return NULL; | 39 return NULL; |
39 } | 40 } |
40 | 41 |
41 UserScript script; | 42 UserScript script; |
42 if (!UserScriptMaster::ScriptReloader::ParseMetadataHeader(content, | 43 if (!UserScriptMaster::ScriptReloader::ParseMetadataHeader(content, |
43 &script)) { | 44 &script)) { |
44 *error = "Invalid script header."; | 45 *error = "Invalid script header."; |
45 return NULL; | 46 return NULL; |
46 } | 47 } |
47 | 48 |
48 FilePath user_data_temp_dir; | 49 FilePath user_data_temp_dir = extension_file_util::GetUserDataTempDir(); |
49 CHECK(PathService::Get(chrome::DIR_USER_DATA_TEMP, &user_data_temp_dir)); | 50 if (user_data_temp_dir.empty()) { |
| 51 *error = "Could not get path to profile temporary directory."; |
| 52 return NULL; |
| 53 } |
50 | 54 |
51 ScopedTempDir temp_dir; | 55 ScopedTempDir temp_dir; |
52 if (!temp_dir.CreateUniqueTempDirUnderPath(user_data_temp_dir)) { | 56 if (!temp_dir.CreateUniqueTempDirUnderPath(user_data_temp_dir)) { |
53 *error = "Could not create temporary directory."; | 57 *error = "Could not create temporary directory."; |
54 return NULL; | 58 return NULL; |
55 } | 59 } |
56 | 60 |
57 // Create the manifest | 61 // Create the manifest |
58 scoped_ptr<DictionaryValue> root(new DictionaryValue); | 62 scoped_ptr<DictionaryValue> root(new DictionaryValue); |
59 std::string script_name; | 63 std::string script_name; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 scoped_refptr<Extension> extension = Extension::Create( | 149 scoped_refptr<Extension> extension = Extension::Create( |
146 temp_dir.path(), Extension::INTERNAL, *root, false, error); | 150 temp_dir.path(), Extension::INTERNAL, *root, false, error); |
147 if (!extension) { | 151 if (!extension) { |
148 NOTREACHED() << "Could not init extension " << *error; | 152 NOTREACHED() << "Could not init extension " << *error; |
149 return NULL; | 153 return NULL; |
150 } | 154 } |
151 | 155 |
152 temp_dir.Take(); // The caller takes ownership of the directory. | 156 temp_dir.Take(); // The caller takes ownership of the directory. |
153 return extension; | 157 return extension; |
154 } | 158 } |
OLD | NEW |