| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/base64.h" | 14 #include "base/base64.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 18 #include "base/json/json_file_value_serializer.h" | 18 #include "base/json/json_file_value_serializer.h" |
| 19 #include "base/memory/ptr_util.h" |
| 19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/values.h" |
| 21 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 22 #include "crypto/sha2.h" | 24 #include "crypto/sha2.h" |
| 23 #include "extensions/browser/extension_user_script_loader.h" | 25 #include "extensions/browser/extension_user_script_loader.h" |
| 24 #include "extensions/common/constants.h" | 26 #include "extensions/common/constants.h" |
| 25 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
| 26 #include "extensions/common/file_util.h" | 28 #include "extensions/common/file_util.h" |
| 27 #include "extensions/common/manifest_constants.h" | 29 #include "extensions/common/manifest_constants.h" |
| 28 #include "extensions/common/user_script.h" | 30 #include "extensions/common/user_script.h" |
| 29 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 30 | 32 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // missing. | 99 // missing. |
| 98 if (!script.version().empty()) | 100 if (!script.version().empty()) |
| 99 root->SetString(keys::kVersion, script.version()); | 101 root->SetString(keys::kVersion, script.version()); |
| 100 else | 102 else |
| 101 root->SetString(keys::kVersion, "1.0"); | 103 root->SetString(keys::kVersion, "1.0"); |
| 102 | 104 |
| 103 root->SetString(keys::kDescription, script.description()); | 105 root->SetString(keys::kDescription, script.description()); |
| 104 root->SetString(keys::kPublicKey, key); | 106 root->SetString(keys::kPublicKey, key); |
| 105 root->SetBoolean(keys::kConvertedFromUserScript, true); | 107 root->SetBoolean(keys::kConvertedFromUserScript, true); |
| 106 | 108 |
| 107 base::ListValue* js_files = new base::ListValue(); | 109 auto js_files = base::MakeUnique<base::ListValue>(); |
| 108 js_files->AppendString("script.js"); | 110 js_files->AppendString("script.js"); |
| 109 | 111 |
| 110 // If the script provides its own match patterns, we use those. Otherwise, we | 112 // If the script provides its own match patterns, we use those. Otherwise, we |
| 111 // generate some using the include globs. | 113 // generate some using the include globs. |
| 112 base::ListValue* matches = new base::ListValue(); | 114 auto matches = base::MakeUnique<base::ListValue>(); |
| 113 if (!script.url_patterns().is_empty()) { | 115 if (!script.url_patterns().is_empty()) { |
| 114 for (URLPatternSet::const_iterator i = script.url_patterns().begin(); | 116 for (URLPatternSet::const_iterator i = script.url_patterns().begin(); |
| 115 i != script.url_patterns().end(); ++i) { | 117 i != script.url_patterns().end(); ++i) { |
| 116 matches->AppendString(i->GetAsString()); | 118 matches->AppendString(i->GetAsString()); |
| 117 } | 119 } |
| 118 } else { | 120 } else { |
| 119 // TODO(aa): Derive tighter matches where possible. | 121 // TODO(aa): Derive tighter matches where possible. |
| 120 matches->AppendString("http://*/*"); | 122 matches->AppendString("http://*/*"); |
| 121 matches->AppendString("https://*/*"); | 123 matches->AppendString("https://*/*"); |
| 122 } | 124 } |
| 123 | 125 |
| 124 // Read the exclude matches, if any are present. | 126 // Read the exclude matches, if any are present. |
| 125 base::ListValue* exclude_matches = new base::ListValue(); | 127 auto exclude_matches = base::MakeUnique<base::ListValue>(); |
| 126 if (!script.exclude_url_patterns().is_empty()) { | 128 if (!script.exclude_url_patterns().is_empty()) { |
| 127 for (URLPatternSet::const_iterator i = | 129 for (URLPatternSet::const_iterator i = |
| 128 script.exclude_url_patterns().begin(); | 130 script.exclude_url_patterns().begin(); |
| 129 i != script.exclude_url_patterns().end(); ++i) { | 131 i != script.exclude_url_patterns().end(); ++i) { |
| 130 exclude_matches->AppendString(i->GetAsString()); | 132 exclude_matches->AppendString(i->GetAsString()); |
| 131 } | 133 } |
| 132 } | 134 } |
| 133 | 135 |
| 134 base::ListValue* includes = new base::ListValue(); | 136 auto includes = base::MakeUnique<base::ListValue>(); |
| 135 for (size_t i = 0; i < script.globs().size(); ++i) | 137 for (size_t i = 0; i < script.globs().size(); ++i) |
| 136 includes->AppendString(script.globs().at(i)); | 138 includes->AppendString(script.globs().at(i)); |
| 137 | 139 |
| 138 base::ListValue* excludes = new base::ListValue(); | 140 auto excludes = base::MakeUnique<base::ListValue>(); |
| 139 for (size_t i = 0; i < script.exclude_globs().size(); ++i) | 141 for (size_t i = 0; i < script.exclude_globs().size(); ++i) |
| 140 excludes->AppendString(script.exclude_globs().at(i)); | 142 excludes->AppendString(script.exclude_globs().at(i)); |
| 141 | 143 |
| 142 std::unique_ptr<base::DictionaryValue> content_script( | 144 auto content_script = base::MakeUnique<base::DictionaryValue>(); |
| 143 new base::DictionaryValue()); | 145 content_script->Set(keys::kMatches, std::move(matches)); |
| 144 content_script->Set(keys::kMatches, matches); | 146 content_script->Set(keys::kExcludeMatches, std::move(exclude_matches)); |
| 145 content_script->Set(keys::kExcludeMatches, exclude_matches); | 147 content_script->Set(keys::kIncludeGlobs, std::move(includes)); |
| 146 content_script->Set(keys::kIncludeGlobs, includes); | 148 content_script->Set(keys::kExcludeGlobs, std::move(excludes)); |
| 147 content_script->Set(keys::kExcludeGlobs, excludes); | 149 content_script->Set(keys::kJs, std::move(js_files)); |
| 148 content_script->Set(keys::kJs, js_files); | |
| 149 | 150 |
| 150 if (script.run_location() == UserScript::DOCUMENT_START) | 151 if (script.run_location() == UserScript::DOCUMENT_START) |
| 151 content_script->SetString(keys::kRunAt, values::kRunAtDocumentStart); | 152 content_script->SetString(keys::kRunAt, values::kRunAtDocumentStart); |
| 152 else if (script.run_location() == UserScript::DOCUMENT_END) | 153 else if (script.run_location() == UserScript::DOCUMENT_END) |
| 153 content_script->SetString(keys::kRunAt, values::kRunAtDocumentEnd); | 154 content_script->SetString(keys::kRunAt, values::kRunAtDocumentEnd); |
| 154 else if (script.run_location() == UserScript::DOCUMENT_IDLE) | 155 else if (script.run_location() == UserScript::DOCUMENT_IDLE) |
| 155 // This is the default, but store it just in case we change that. | 156 // This is the default, but store it just in case we change that. |
| 156 content_script->SetString(keys::kRunAt, values::kRunAtDocumentIdle); | 157 content_script->SetString(keys::kRunAt, values::kRunAtDocumentIdle); |
| 157 | 158 |
| 158 base::ListValue* content_scripts = new base::ListValue(); | 159 auto content_scripts = base::MakeUnique<base::ListValue>(); |
| 159 content_scripts->Append(std::move(content_script)); | 160 content_scripts->Append(std::move(content_script)); |
| 160 | 161 |
| 161 root->Set(keys::kContentScripts, content_scripts); | 162 root->Set(keys::kContentScripts, std::move(content_scripts)); |
| 162 | 163 |
| 163 base::FilePath manifest_path = temp_dir.GetPath().Append(kManifestFilename); | 164 base::FilePath manifest_path = temp_dir.GetPath().Append(kManifestFilename); |
| 164 JSONFileValueSerializer serializer(manifest_path); | 165 JSONFileValueSerializer serializer(manifest_path); |
| 165 if (!serializer.Serialize(*root)) { | 166 if (!serializer.Serialize(*root)) { |
| 166 *error = base::ASCIIToUTF16("Could not write JSON."); | 167 *error = base::ASCIIToUTF16("Could not write JSON."); |
| 167 return NULL; | 168 return NULL; |
| 168 } | 169 } |
| 169 | 170 |
| 170 // Write the script file. | 171 // Write the script file. |
| 171 if (!base::CopyFile(user_script_path, | 172 if (!base::CopyFile(user_script_path, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 184 if (!extension.get()) { | 185 if (!extension.get()) { |
| 185 NOTREACHED() << "Could not init extension " << *error; | 186 NOTREACHED() << "Could not init extension " << *error; |
| 186 return NULL; | 187 return NULL; |
| 187 } | 188 } |
| 188 | 189 |
| 189 temp_dir.Take(); // The caller takes ownership of the directory. | 190 temp_dir.Take(); // The caller takes ownership of the directory. |
| 190 return extension; | 191 return extension; |
| 191 } | 192 } |
| 192 | 193 |
| 193 } // namespace extensions | 194 } // namespace extensions |
| OLD | NEW |