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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 // missing. | 94 // missing. |
95 if (!script.version().empty()) | 95 if (!script.version().empty()) |
96 root->SetString(keys::kVersion, script.version()); | 96 root->SetString(keys::kVersion, script.version()); |
97 else | 97 else |
98 root->SetString(keys::kVersion, "1.0"); | 98 root->SetString(keys::kVersion, "1.0"); |
99 | 99 |
100 root->SetString(keys::kDescription, script.description()); | 100 root->SetString(keys::kDescription, script.description()); |
101 root->SetString(keys::kPublicKey, key); | 101 root->SetString(keys::kPublicKey, key); |
102 root->SetBoolean(keys::kConvertedFromUserScript, true); | 102 root->SetBoolean(keys::kConvertedFromUserScript, true); |
103 | 103 |
104 ListValue* js_files = new ListValue(); | 104 base::ListValue* js_files = new base::ListValue(); |
105 js_files->Append(Value::CreateStringValue("script.js")); | 105 js_files->Append(Value::CreateStringValue("script.js")); |
106 | 106 |
107 // If the script provides its own match patterns, we use those. Otherwise, we | 107 // If the script provides its own match patterns, we use those. Otherwise, we |
108 // generate some using the include globs. | 108 // generate some using the include globs. |
109 ListValue* matches = new ListValue(); | 109 base::ListValue* matches = new base::ListValue(); |
110 if (!script.url_patterns().is_empty()) { | 110 if (!script.url_patterns().is_empty()) { |
111 for (URLPatternSet::const_iterator i = script.url_patterns().begin(); | 111 for (URLPatternSet::const_iterator i = script.url_patterns().begin(); |
112 i != script.url_patterns().end(); ++i) { | 112 i != script.url_patterns().end(); ++i) { |
113 matches->Append(Value::CreateStringValue(i->GetAsString())); | 113 matches->Append(Value::CreateStringValue(i->GetAsString())); |
114 } | 114 } |
115 } else { | 115 } else { |
116 // TODO(aa): Derive tighter matches where possible. | 116 // TODO(aa): Derive tighter matches where possible. |
117 matches->Append(Value::CreateStringValue("http://*/*")); | 117 matches->Append(Value::CreateStringValue("http://*/*")); |
118 matches->Append(Value::CreateStringValue("https://*/*")); | 118 matches->Append(Value::CreateStringValue("https://*/*")); |
119 } | 119 } |
120 | 120 |
121 // Read the exclude matches, if any are present. | 121 // Read the exclude matches, if any are present. |
122 ListValue* exclude_matches = new ListValue(); | 122 base::ListValue* exclude_matches = new base::ListValue(); |
123 if (!script.exclude_url_patterns().is_empty()) { | 123 if (!script.exclude_url_patterns().is_empty()) { |
124 for (URLPatternSet::const_iterator i = | 124 for (URLPatternSet::const_iterator i = |
125 script.exclude_url_patterns().begin(); | 125 script.exclude_url_patterns().begin(); |
126 i != script.exclude_url_patterns().end(); ++i) { | 126 i != script.exclude_url_patterns().end(); ++i) { |
127 exclude_matches->Append(Value::CreateStringValue(i->GetAsString())); | 127 exclude_matches->Append(Value::CreateStringValue(i->GetAsString())); |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 ListValue* includes = new ListValue(); | 131 base::ListValue* includes = new base::ListValue(); |
132 for (size_t i = 0; i < script.globs().size(); ++i) | 132 for (size_t i = 0; i < script.globs().size(); ++i) |
133 includes->Append(Value::CreateStringValue(script.globs().at(i))); | 133 includes->Append(Value::CreateStringValue(script.globs().at(i))); |
134 | 134 |
135 ListValue* excludes = new ListValue(); | 135 base::ListValue* excludes = new base::ListValue(); |
136 for (size_t i = 0; i < script.exclude_globs().size(); ++i) | 136 for (size_t i = 0; i < script.exclude_globs().size(); ++i) |
137 excludes->Append(Value::CreateStringValue(script.exclude_globs().at(i))); | 137 excludes->Append(Value::CreateStringValue(script.exclude_globs().at(i))); |
138 | 138 |
139 DictionaryValue* content_script = new DictionaryValue(); | 139 DictionaryValue* content_script = new DictionaryValue(); |
140 content_script->Set(keys::kMatches, matches); | 140 content_script->Set(keys::kMatches, matches); |
141 content_script->Set(keys::kExcludeMatches, exclude_matches); | 141 content_script->Set(keys::kExcludeMatches, exclude_matches); |
142 content_script->Set(keys::kIncludeGlobs, includes); | 142 content_script->Set(keys::kIncludeGlobs, includes); |
143 content_script->Set(keys::kExcludeGlobs, excludes); | 143 content_script->Set(keys::kExcludeGlobs, excludes); |
144 content_script->Set(keys::kJs, js_files); | 144 content_script->Set(keys::kJs, js_files); |
145 | 145 |
146 if (script.run_location() == UserScript::DOCUMENT_START) | 146 if (script.run_location() == UserScript::DOCUMENT_START) |
147 content_script->SetString(keys::kRunAt, values::kRunAtDocumentStart); | 147 content_script->SetString(keys::kRunAt, values::kRunAtDocumentStart); |
148 else if (script.run_location() == UserScript::DOCUMENT_END) | 148 else if (script.run_location() == UserScript::DOCUMENT_END) |
149 content_script->SetString(keys::kRunAt, values::kRunAtDocumentEnd); | 149 content_script->SetString(keys::kRunAt, values::kRunAtDocumentEnd); |
150 else if (script.run_location() == UserScript::DOCUMENT_IDLE) | 150 else if (script.run_location() == UserScript::DOCUMENT_IDLE) |
151 // This is the default, but store it just in case we change that. | 151 // This is the default, but store it just in case we change that. |
152 content_script->SetString(keys::kRunAt, values::kRunAtDocumentIdle); | 152 content_script->SetString(keys::kRunAt, values::kRunAtDocumentIdle); |
153 | 153 |
154 ListValue* content_scripts = new ListValue(); | 154 base::ListValue* content_scripts = new base::ListValue(); |
155 content_scripts->Append(content_script); | 155 content_scripts->Append(content_script); |
156 | 156 |
157 root->Set(keys::kContentScripts, content_scripts); | 157 root->Set(keys::kContentScripts, content_scripts); |
158 | 158 |
159 base::FilePath manifest_path = temp_dir.path().Append(kManifestFilename); | 159 base::FilePath manifest_path = temp_dir.path().Append(kManifestFilename); |
160 JSONFileValueSerializer serializer(manifest_path); | 160 JSONFileValueSerializer serializer(manifest_path); |
161 if (!serializer.Serialize(*root)) { | 161 if (!serializer.Serialize(*root)) { |
162 *error = ASCIIToUTF16("Could not write JSON."); | 162 *error = ASCIIToUTF16("Could not write JSON."); |
163 return NULL; | 163 return NULL; |
164 } | 164 } |
(...skipping 18 matching lines...) Expand all Loading... |
183 if (!extension.get()) { | 183 if (!extension.get()) { |
184 NOTREACHED() << "Could not init extension " << *error; | 184 NOTREACHED() << "Could not init extension " << *error; |
185 return NULL; | 185 return NULL; |
186 } | 186 } |
187 | 187 |
188 temp_dir.Take(); // The caller takes ownership of the directory. | 188 temp_dir.Take(); // The caller takes ownership of the directory. |
189 return extension; | 189 return extension; |
190 } | 190 } |
191 | 191 |
192 } // namespace extensions | 192 } // namespace extensions |
OLD | NEW |