// Copyright 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_ #define CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_ #include #include #include "base/file_path.h" #include "base/pickle.h" #include "chrome/common/extensions/url_pattern.h" #include "googleurl/src/gurl.h" // Represents a user script, either a standalone one, or one that is part of an // extension. class UserScript { public: // Locations that user scripts can be run inside the document. enum RunLocation { DOCUMENT_START, // After the documentElemnet is created, but before // anything else happens. DOCUMENT_END, // After the entire document is parsed. Same as // DOMContentLoaded. RUN_LOCATION_LAST // Leave this as the last item. }; // Constructor. Default the run location to document end, which is like // Greasemonkey and probably more useful for typical scripts. UserScript() : run_location_(DOCUMENT_END) {} // The URL to retrieve the content of this script at. const GURL& url() const { return url_; } void set_url(const GURL& url) { url_ = url; } // The path to find the script at. const FilePath& path() const { return path_; } void set_path(const FilePath& path) { path_ = path; } // The place in the document to run the script. RunLocation run_location() const { return run_location_; } void set_run_location(RunLocation location) { run_location_ = location; } // The globs, if any, that determine which pages this script runs against. // These are only used with "standalone" Greasemonkey-like user scripts. const std::vector& globs() const { return globs_; } void add_glob(const std::string& glob) { globs_.push_back(glob); } void clear_globs() { globs_.clear(); } // The URLPatterns, if any, that determine which pages this script runs // against. const std::vector& url_patterns() const { return url_patterns_; } void add_url_pattern(const URLPattern& pattern) { url_patterns_.push_back(pattern); } void clear_url_patterns() { url_patterns_.clear(); } // Returns true if the script should be applied to the specified URL, false // otherwise. bool MatchesUrl(const GURL& url); // Serialize the script into a pickle. void Pickle(::Pickle* pickle); // Deserialize the script from a pickle. Note that this always succeeds // because presumably we were the one that pickled it, and we did it // correctly. void Unpickle(const ::Pickle& pickle, void** iter); private: // The URL to the content of the script. GURL url_; // The path to the content of the script. FilePath path_; // The location to run the script inside the document. RunLocation run_location_; // Greasemonkey-style globs that determine pages to inject the script into. // These are only used with standalone scripts. std::vector globs_; // URLPatterns that determine pages to inject the script into. These are // only used with scripts that are part of extensions. std::vector url_patterns_; }; typedef std::vector UserScriptList; #endif // CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_