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