OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 #ifndef CHROME_RENDERER_GREASEMONKEY_SLAVE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_SLAVE_H_ |
6 #define CHROME_RENDERER_GREASEMONKEY_SLAVE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_SLAVE_H_ |
7 | 7 |
8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
9 #include "base/shared_memory.h" | 9 #include "base/shared_memory.h" |
10 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "testing/gtest/include/gtest/gtest_prod.h" | 12 #include "testing/gtest/include/gtest/gtest_prod.h" |
13 #include "webkit/glue/webframe.h" | 13 #include "webkit/glue/webframe.h" |
14 | 14 |
15 | 15 |
16 // Parsed representation of a Greasemonkey script. | 16 // Parsed representation of a user script. |
17 class GreasemonkeyScript { | 17 class UserScript { |
18 public: | 18 public: |
19 GreasemonkeyScript(const StringPiece& script_url) | 19 UserScript(const StringPiece& script_url) |
20 : url_(script_url) {} | 20 : url_(script_url) {} |
21 | 21 |
22 // Gets the script body that should be injected into matching content. | 22 // Gets the script body that should be injected into matching content. |
23 const StringPiece& GetBody() const { | 23 const StringPiece& GetBody() const { |
24 return body_; | 24 return body_; |
25 } | 25 } |
26 | 26 |
27 // Gets a URL where this script can be found. | 27 // Gets a URL where this script can be found. |
28 const StringPiece& GetURL() const { | 28 const StringPiece& GetURL() const { |
29 return url_; | 29 return url_; |
30 } | 30 } |
31 | 31 |
32 // Parses the text content of a user script file. | 32 // Parses the text content of a user script file. |
33 void Parse(const StringPiece& script_text); | 33 void Parse(const StringPiece& script_text); |
34 | 34 |
35 // Returns true if the script should be applied to the specified URL, false | 35 // Returns true if the script should be applied to the specified URL, false |
36 // otherwise. | 36 // otherwise. |
37 bool MatchesUrl(const GURL& url); | 37 bool MatchesUrl(const GURL& url); |
38 | 38 |
39 private: | 39 private: |
40 FRIEND_TEST(GreasemonkeySlaveTest, EscapeGlob); | 40 FRIEND_TEST(UserScriptSlaveTest, EscapeGlob); |
41 FRIEND_TEST(GreasemonkeySlaveTest, Parse1); | 41 FRIEND_TEST(UserScriptSlaveTest, Parse1); |
42 FRIEND_TEST(GreasemonkeySlaveTest, Parse2); | 42 FRIEND_TEST(UserScriptSlaveTest, Parse2); |
43 FRIEND_TEST(GreasemonkeySlaveTest, Parse3); | 43 FRIEND_TEST(UserScriptSlaveTest, Parse3); |
44 | 44 |
45 // Helper function to convert the Greasemonkey glob format to the patterns | 45 // Helper function to convert the user script glob format to the patterns |
46 // used internally to test URLs. | 46 // used internally to test URLs. |
47 static std::string EscapeGlob(const std::string& glob); | 47 static std::string EscapeGlob(const std::string& glob); |
48 | 48 |
49 // Parses the metadata block from the script. | 49 // Parses the metadata block from the script. |
50 void ParseMetadata(const StringPiece& script_text); | 50 void ParseMetadata(const StringPiece& script_text); |
51 | 51 |
52 // Adds an include pattern that will be checked to determine whether to | 52 // Adds an include pattern that will be checked to determine whether to |
53 // include a script on a given page. | 53 // include a script on a given page. |
54 void AddInclude(const std::string &glob_pattern); | 54 void AddInclude(const std::string &glob_pattern); |
55 | 55 |
56 // The body of the script, which will be injected into content pages. This | 56 // The body of the script, which will be injected into content pages. This |
57 // references shared_memory_, and is valid until that memory is either | 57 // references shared_memory_, and is valid until that memory is either |
58 // deleted or Unmap()'d. | 58 // deleted or Unmap()'d. |
59 StringPiece body_; | 59 StringPiece body_; |
60 | 60 |
61 // The url of the file the script came from. This references shared_memory_, | 61 // The url of the file the script came from. This references shared_memory_, |
62 // and is valid until that memory is either deleted or Unmap()'d. | 62 // and is valid until that memory is either deleted or Unmap()'d. |
63 StringPiece url_; | 63 StringPiece url_; |
64 | 64 |
65 // List of patterns to test URLs against for this script. These patterns have | 65 // List of patterns to test URLs against for this script. These patterns have |
66 // been escaped for use with MatchPattern() in string_utils ('?' and '\' are | 66 // been escaped for use with MatchPattern() in string_utils ('?' and '\' are |
67 // escaped). | 67 // escaped). |
68 std::vector<std::string> include_patterns_; | 68 std::vector<std::string> include_patterns_; |
69 }; | 69 }; |
70 | 70 |
71 | 71 |
72 // Manages installed GreasemonkeyScripts for a render process. | 72 // Manages installed UserScripts for a render process. |
73 class GreasemonkeySlave { | 73 class UserScriptSlave { |
74 public: | 74 public: |
75 GreasemonkeySlave(); | 75 UserScriptSlave(); |
76 | 76 |
77 // Update the parsed scripts from shared memory. | 77 // Update the parsed scripts from shared memory. |
78 bool UpdateScripts(base::SharedMemoryHandle shared_memory); | 78 bool UpdateScripts(base::SharedMemoryHandle shared_memory); |
79 | 79 |
80 // Inject the appropriate scripts into a frame based on its URL. | 80 // Inject the appropriate scripts into a frame based on its URL. |
81 // TODO(aa): Extract a GreasemonkeyFrame interface out of this to improve | 81 // TODO(aa): Extract a UserScriptFrame interface out of this to improve |
82 // testability. | 82 // testability. |
83 bool InjectScripts(WebFrame* frame); | 83 bool InjectScripts(WebFrame* frame); |
84 | 84 |
85 private: | 85 private: |
86 // Shared memory containing raw script data. | 86 // Shared memory containing raw script data. |
87 scoped_ptr<base::SharedMemory> shared_memory_; | 87 scoped_ptr<base::SharedMemory> shared_memory_; |
88 | 88 |
89 // Parsed script data. | 89 // Parsed script data. |
90 std::vector<GreasemonkeyScript> scripts_; | 90 std::vector<UserScript> scripts_; |
91 | 91 |
92 DISALLOW_COPY_AND_ASSIGN(GreasemonkeySlave); | 92 DISALLOW_COPY_AND_ASSIGN(UserScriptSlave); |
93 }; | 93 }; |
94 | 94 |
95 #endif // CHROME_RENDERER_GREASEMONKEY_SLAVE_H_ | 95 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_SLAVE_H_ |
OLD | NEW |