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