Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / user_script.cc
blob0334e58497d6fc6f60ad65465b0ff35d388e981d
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
3 // found in the LICENSE file.
5 #include "chrome/common/extensions/user_script.h"
7 #include "base/pickle.h"
8 #include "base/string_util.h"
10 namespace {
12 bool UrlMatchesGlobs(const std::vector<std::string>* globs,
13 const GURL& url) {
14 for (std::vector<std::string>::const_iterator glob = globs->begin();
15 glob != globs->end(); ++glob) {
16 if (MatchPattern(url.spec(), *glob))
17 return true;
20 return false;
23 } // namespace
25 namespace extensions {
27 // static
28 const char UserScript::kFileExtension[] = ".user.js";
30 bool UserScript::IsURLUserScript(const GURL& url,
31 const std::string& mime_type) {
32 return EndsWith(url.ExtractFileName(), kFileExtension, false) &&
33 mime_type != "text/html";
36 UserScript::File::File(const FilePath& extension_root,
37 const FilePath& relative_path,
38 const GURL& url)
39 : extension_root_(extension_root),
40 relative_path_(relative_path),
41 url_(url) {
44 UserScript::File::File() {}
46 UserScript::File::~File() {}
48 UserScript::UserScript()
49 : run_location_(DOCUMENT_IDLE), emulate_greasemonkey_(false),
50 match_all_frames_(false), incognito_enabled_(false) {
53 UserScript::~UserScript() {
56 void UserScript::add_url_pattern(const URLPattern& pattern) {
57 url_set_.AddPattern(pattern);
60 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) {
61 exclude_url_set_.AddPattern(pattern);
64 bool UserScript::MatchesURL(const GURL& url) const {
65 if (!url_set_.is_empty()) {
66 if (!url_set_.MatchesURL(url))
67 return false;
70 if (!exclude_url_set_.is_empty()) {
71 if (exclude_url_set_.MatchesURL(url))
72 return false;
75 if (!globs_.empty()) {
76 if (!UrlMatchesGlobs(&globs_, url))
77 return false;
80 if (!exclude_globs_.empty()) {
81 if (UrlMatchesGlobs(&exclude_globs_, url))
82 return false;
85 return true;
88 void UserScript::File::Pickle(::Pickle* pickle) const {
89 pickle->WriteString(url_.spec());
90 // Do not write path. It's not needed in the renderer.
91 // Do not write content. It will be serialized by other means.
94 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
95 // Read the url from the pickle.
96 std::string url;
97 CHECK(pickle.ReadString(iter, &url));
98 set_url(GURL(url));
101 void UserScript::Pickle(::Pickle* pickle) const {
102 // Write the simple types to the pickle.
103 pickle->WriteInt(run_location());
104 pickle->WriteString(extension_id());
105 pickle->WriteBool(emulate_greasemonkey());
106 pickle->WriteBool(match_all_frames());
107 pickle->WriteBool(is_incognito_enabled());
109 PickleGlobs(pickle, globs_);
110 PickleGlobs(pickle, exclude_globs_);
111 PickleURLPatternSet(pickle, url_set_);
112 PickleURLPatternSet(pickle, exclude_url_set_);
113 PickleScripts(pickle, js_scripts_);
114 PickleScripts(pickle, css_scripts_);
117 void UserScript::PickleGlobs(::Pickle* pickle,
118 const std::vector<std::string>& globs) const {
119 pickle->WriteUInt64(globs.size());
120 for (std::vector<std::string>::const_iterator glob = globs.begin();
121 glob != globs.end(); ++glob) {
122 pickle->WriteString(*glob);
126 void UserScript::PickleURLPatternSet(::Pickle* pickle,
127 const URLPatternSet& pattern_list) const {
128 pickle->WriteUInt64(pattern_list.patterns().size());
129 for (URLPatternSet::const_iterator pattern = pattern_list.begin();
130 pattern != pattern_list.end(); ++pattern) {
131 pickle->WriteInt(pattern->valid_schemes());
132 pickle->WriteString(pattern->GetAsString());
136 void UserScript::PickleScripts(::Pickle* pickle,
137 const FileList& scripts) const {
138 pickle->WriteUInt64(scripts.size());
139 for (FileList::const_iterator file = scripts.begin();
140 file != scripts.end(); ++file) {
141 file->Pickle(pickle);
145 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
146 // Read the run location.
147 int run_location = 0;
148 CHECK(pickle.ReadInt(iter, &run_location));
149 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
150 run_location_ = static_cast<RunLocation>(run_location);
152 CHECK(pickle.ReadString(iter, &extension_id_));
153 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_));
154 CHECK(pickle.ReadBool(iter, &match_all_frames_));
155 CHECK(pickle.ReadBool(iter, &incognito_enabled_));
157 UnpickleGlobs(pickle, iter, &globs_);
158 UnpickleGlobs(pickle, iter, &exclude_globs_);
159 UnpickleURLPatternSet(pickle, iter, &url_set_);
160 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_);
161 UnpickleScripts(pickle, iter, &js_scripts_);
162 UnpickleScripts(pickle, iter, &css_scripts_);
165 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter,
166 std::vector<std::string>* globs) {
167 uint64 num_globs = 0;
168 CHECK(pickle.ReadUInt64(iter, &num_globs));
169 globs->clear();
170 for (uint64 i = 0; i < num_globs; ++i) {
171 std::string glob;
172 CHECK(pickle.ReadString(iter, &glob));
173 globs->push_back(glob);
177 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle,
178 PickleIterator* iter,
179 URLPatternSet* pattern_list) {
180 uint64 num_patterns = 0;
181 CHECK(pickle.ReadUInt64(iter, &num_patterns));
183 pattern_list->ClearPatterns();
184 for (uint64 i = 0; i < num_patterns; ++i) {
185 int valid_schemes;
186 CHECK(pickle.ReadInt(iter, &valid_schemes));
187 std::string pattern_str;
188 URLPattern pattern(valid_schemes);
189 CHECK(pickle.ReadString(iter, &pattern_str));
191 // We remove the file scheme if it's not actually allowed (see Extension::
192 // LoadUserScriptHelper), but we need it temporarily while loading the
193 // pattern so that it's valid.
194 bool had_file_scheme = (valid_schemes & URLPattern::SCHEME_FILE) != 0;
195 if (!had_file_scheme)
196 pattern.SetValidSchemes(valid_schemes | URLPattern::SCHEME_FILE);
197 CHECK(URLPattern::PARSE_SUCCESS == pattern.Parse(pattern_str));
198 if (!had_file_scheme)
199 pattern.SetValidSchemes(valid_schemes);
201 pattern_list->AddPattern(pattern);
205 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter,
206 FileList* scripts) {
207 uint64 num_files = 0;
208 CHECK(pickle.ReadUInt64(iter, &num_files));
209 scripts->clear();
210 for (uint64 i = 0; i < num_files; ++i) {
211 File file;
212 file.Unpickle(pickle, iter);
213 scripts->push_back(file);
217 } // namespace extensions