| OLD | NEW |
| 1 // Copyright (c) 2011 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/json/json_value_serializer.h" | 13 #include "base/json/json_value_serializer.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/extensions/user_script_master.h" | 18 #include "chrome/browser/extensions/user_script_master.h" |
| 18 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/extensions/extension.h" | 20 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/extension_constants.h" | 21 #include "chrome/common/extensions/extension_constants.h" |
| 21 #include "chrome/common/extensions/extension_file_util.h" | 22 #include "chrome/common/extensions/extension_file_util.h" |
| 22 #include "chrome/common/extensions/user_script.h" | 23 #include "chrome/common/extensions/user_script.h" |
| 23 #include "crypto/sha2.h" | 24 #include "crypto/sha2.h" |
| 24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 25 | 26 |
| 26 namespace keys = extension_manifest_keys; | 27 namespace keys = extension_manifest_keys; |
| 27 namespace values = extension_manifest_values; | 28 namespace values = extension_manifest_values; |
| 28 | 29 |
| 29 scoped_refptr<Extension> ConvertUserScriptToExtension( | 30 scoped_refptr<Extension> ConvertUserScriptToExtension( |
| 30 const FilePath& user_script_path, const GURL& original_url, | 31 const FilePath& user_script_path, const GURL& original_url, |
| 31 std::string* error) { | 32 string16* error) { |
| 32 std::string content; | 33 std::string content; |
| 33 if (!file_util::ReadFileToString(user_script_path, &content)) { | 34 if (!file_util::ReadFileToString(user_script_path, &content)) { |
| 34 *error = "Could not read source file."; | 35 *error = ASCIIToUTF16("Could not read source file."); |
| 35 return NULL; | 36 return NULL; |
| 36 } | 37 } |
| 37 | 38 |
| 38 if (!IsStringUTF8(content)) { | 39 if (!IsStringUTF8(content)) { |
| 39 *error = "User script must be UTF8 encoded."; | 40 *error = ASCIIToUTF16("User script must be UTF8 encoded."); |
| 40 return NULL; | 41 return NULL; |
| 41 } | 42 } |
| 42 | 43 |
| 43 UserScript script; | 44 UserScript script; |
| 44 if (!UserScriptMaster::ScriptReloader::ParseMetadataHeader(content, | 45 if (!UserScriptMaster::ScriptReloader::ParseMetadataHeader(content, |
| 45 &script)) { | 46 &script)) { |
| 46 *error = "Invalid script header."; | 47 *error = ASCIIToUTF16("Invalid script header."); |
| 47 return NULL; | 48 return NULL; |
| 48 } | 49 } |
| 49 | 50 |
| 50 FilePath user_data_temp_dir = extension_file_util::GetUserDataTempDir(); | 51 FilePath user_data_temp_dir = extension_file_util::GetUserDataTempDir(); |
| 51 if (user_data_temp_dir.empty()) { | 52 if (user_data_temp_dir.empty()) { |
| 52 *error = "Could not get path to profile temporary directory."; | 53 *error = ASCIIToUTF16("Could not get path to profile temporary directory."); |
| 53 return NULL; | 54 return NULL; |
| 54 } | 55 } |
| 55 | 56 |
| 56 ScopedTempDir temp_dir; | 57 ScopedTempDir temp_dir; |
| 57 if (!temp_dir.CreateUniqueTempDirUnderPath(user_data_temp_dir)) { | 58 if (!temp_dir.CreateUniqueTempDirUnderPath(user_data_temp_dir)) { |
| 58 *error = "Could not create temporary directory."; | 59 *error = ASCIIToUTF16("Could not create temporary directory."); |
| 59 return NULL; | 60 return NULL; |
| 60 } | 61 } |
| 61 | 62 |
| 62 // Create the manifest | 63 // Create the manifest |
| 63 scoped_ptr<DictionaryValue> root(new DictionaryValue); | 64 scoped_ptr<DictionaryValue> root(new DictionaryValue); |
| 64 std::string script_name; | 65 std::string script_name; |
| 65 if (!script.name().empty() && !script.name_space().empty()) | 66 if (!script.name().empty() && !script.name_space().empty()) |
| 66 script_name = script.name_space() + "/" + script.name(); | 67 script_name = script.name_space() + "/" + script.name(); |
| 67 else | 68 else |
| 68 script_name = original_url.spec(); | 69 script_name = original_url.spec(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 138 |
| 138 ListValue* content_scripts = new ListValue(); | 139 ListValue* content_scripts = new ListValue(); |
| 139 content_scripts->Append(content_script); | 140 content_scripts->Append(content_script); |
| 140 | 141 |
| 141 root->Set(keys::kContentScripts, content_scripts); | 142 root->Set(keys::kContentScripts, content_scripts); |
| 142 | 143 |
| 143 FilePath manifest_path = temp_dir.path().Append( | 144 FilePath manifest_path = temp_dir.path().Append( |
| 144 Extension::kManifestFilename); | 145 Extension::kManifestFilename); |
| 145 JSONFileValueSerializer serializer(manifest_path); | 146 JSONFileValueSerializer serializer(manifest_path); |
| 146 if (!serializer.Serialize(*root)) { | 147 if (!serializer.Serialize(*root)) { |
| 147 *error = "Could not write JSON."; | 148 *error = ASCIIToUTF16("Could not write JSON."); |
| 148 return NULL; | 149 return NULL; |
| 149 } | 150 } |
| 150 | 151 |
| 151 // Write the script file. | 152 // Write the script file. |
| 152 if (!file_util::CopyFile(user_script_path, | 153 if (!file_util::CopyFile(user_script_path, |
| 153 temp_dir.path().AppendASCII("script.js"))) { | 154 temp_dir.path().AppendASCII("script.js"))) { |
| 154 *error = "Could not copy script file."; | 155 *error = ASCIIToUTF16("Could not copy script file."); |
| 155 return NULL; | 156 return NULL; |
| 156 } | 157 } |
| 157 | 158 |
| 159 // TODO(rdevlin.cronin): Continue removing std::string errors and replacing |
| 160 // with string16 |
| 161 std::string utf8_error; |
| 158 scoped_refptr<Extension> extension = Extension::Create( | 162 scoped_refptr<Extension> extension = Extension::Create( |
| 159 temp_dir.path(), | 163 temp_dir.path(), |
| 160 Extension::INTERNAL, | 164 Extension::INTERNAL, |
| 161 *root, | 165 *root, |
| 162 Extension::NO_FLAGS, | 166 Extension::NO_FLAGS, |
| 163 error); | 167 &utf8_error); |
| 168 *error = UTF8ToUTF16(utf8_error); |
| 164 if (!extension) { | 169 if (!extension) { |
| 165 NOTREACHED() << "Could not init extension " << *error; | 170 NOTREACHED() << "Could not init extension " << *error; |
| 166 return NULL; | 171 return NULL; |
| 167 } | 172 } |
| 168 | 173 |
| 169 temp_dir.Take(); // The caller takes ownership of the directory. | 174 temp_dir.Take(); // The caller takes ownership of the directory. |
| 170 return extension; | 175 return extension; |
| 171 } | 176 } |
| OLD | NEW |