summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/user_script.h
blob: 671f158285a57b55e7e803ce110e030f6ead0d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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 <vector>
#include <string>

#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<std::string>& 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<URLPattern>& 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<std::string> globs_;

  // URLPatterns that determine pages to inject the script into. These are
  // only used with scripts that are part of extensions.
  std::vector<URLPattern> url_patterns_;
};

typedef std::vector<UserScript> UserScriptList;

#endif  // CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_