| 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 #include "chrome/renderer/greasemonkey_slave.h" | 5 #include "chrome/renderer/user_script_slave.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "base/shared_memory.h" | 9 #include "base/shared_memory.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| 11 | 11 |
| 12 | 12 |
| 13 // GreasemonkeyScript | 13 // UserScript |
| 14 | 14 |
| 15 void GreasemonkeyScript::Parse(const StringPiece& script_text) { | 15 void UserScript::Parse(const StringPiece& script_text) { |
| 16 ParseMetadata(script_text); | 16 ParseMetadata(script_text); |
| 17 | 17 |
| 18 // TODO(aa): Set body to just the part after the metadata block? This would | 18 // TODO(aa): Set body to just the part after the metadata block? This would |
| 19 // significantly cut down on the size of the injected script in some cases. | 19 // significantly cut down on the size of the injected script in some cases. |
| 20 // Would require remembering the line number the body begins at, for correct | 20 // Would require remembering the line number the body begins at, for correct |
| 21 // error line number reporting. | 21 // error line number reporting. |
| 22 body_ = script_text; | 22 body_ = script_text; |
| 23 } | 23 } |
| 24 | 24 |
| 25 bool GreasemonkeyScript::MatchesUrl(const GURL& url) { | 25 bool UserScript::MatchesUrl(const GURL& url) { |
| 26 for (std::vector<std::string>::iterator pattern = include_patterns_.begin(); | 26 for (std::vector<std::string>::iterator pattern = include_patterns_.begin(); |
| 27 pattern != include_patterns_.end(); ++pattern) { | 27 pattern != include_patterns_.end(); ++pattern) { |
| 28 if (MatchPattern(url.spec(), *pattern)) { | 28 if (MatchPattern(url.spec(), *pattern)) { |
| 29 return true; | 29 return true; |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 return false; | 33 return false; |
| 34 } | 34 } |
| 35 | 35 |
| 36 void GreasemonkeyScript::ParseMetadata(const StringPiece& script_text) { | 36 void UserScript::ParseMetadata(const StringPiece& script_text) { |
| 37 // http://wiki.greasespot.net/Metadata_block | 37 // http://wiki.greasespot.net/Metadata_block |
| 38 StringPiece line; | 38 StringPiece line; |
| 39 size_t line_start = 0; | 39 size_t line_start = 0; |
| 40 size_t line_end = 0; | 40 size_t line_end = 0; |
| 41 bool in_metadata = false; | 41 bool in_metadata = false; |
| 42 | 42 |
| 43 static const StringPiece kUserScriptBegin("// ==UserScript=="); | 43 static const StringPiece kUserScriptBegin("// ==UserScript=="); |
| 44 static const StringPiece kUserScriptEng("// ==/UserScript=="); | 44 static const StringPiece kUserScriptEng("// ==/UserScript=="); |
| 45 static const StringPiece kIncludeDeclaration("// @include "); | 45 static const StringPiece kIncludeDeclaration("// @include "); |
| 46 | 46 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 71 AddInclude(pattern_trimmed); | 71 AddInclude(pattern_trimmed); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // TODO(aa): Handle more types of metadata. | 74 // TODO(aa): Handle more types of metadata. |
| 75 } | 75 } |
| 76 | 76 |
| 77 line_start = line_end + 1; | 77 line_start = line_end + 1; |
| 78 } | 78 } |
| 79 | 79 |
| 80 // If no @include patterns were specified, default to @include *. | 80 // If no @include patterns were specified, default to @include *. |
| 81 // This is what Greasemonkey for Firefox does. | 81 // This is what Greasemonkey does. |
| 82 if (include_patterns_.size() == 0) { | 82 if (include_patterns_.size() == 0) { |
| 83 AddInclude("*"); | 83 AddInclude("*"); |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 void GreasemonkeyScript::AddInclude(const std::string &glob_pattern) { | 87 void UserScript::AddInclude(const std::string &glob_pattern) { |
| 88 include_patterns_.push_back(EscapeGlob(glob_pattern)); | 88 include_patterns_.push_back(EscapeGlob(glob_pattern)); |
| 89 } | 89 } |
| 90 | 90 |
| 91 std::string GreasemonkeyScript::EscapeGlob(const std::string& input_pattern) { | 91 std::string UserScript::EscapeGlob(const std::string& input_pattern) { |
| 92 std::string output_pattern; | 92 std::string output_pattern; |
| 93 | 93 |
| 94 for (size_t i = 0; i < input_pattern.length(); ++i) { | 94 for (size_t i = 0; i < input_pattern.length(); ++i) { |
| 95 switch (input_pattern[i]) { | 95 switch (input_pattern[i]) { |
| 96 // These characters have special meaning to the MatchPattern() function, | 96 // These characters have special meaning to the MatchPattern() function, |
| 97 // so we escape them. | 97 // so we escape them. |
| 98 case '\\': | 98 case '\\': |
| 99 case '?': | 99 case '?': |
| 100 output_pattern += '\\'; | 100 output_pattern += '\\'; |
| 101 // fall through | 101 // fall through |
| 102 | 102 |
| 103 default: | 103 default: |
| 104 output_pattern += input_pattern[i]; | 104 output_pattern += input_pattern[i]; |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 return output_pattern; | 108 return output_pattern; |
| 109 } | 109 } |
| 110 | 110 |
| 111 | 111 |
| 112 // GreasemonkeySlave | 112 // UserScriptSlave |
| 113 GreasemonkeySlave::GreasemonkeySlave() : shared_memory_(NULL) { | 113 UserScriptSlave::UserScriptSlave() : shared_memory_(NULL) { |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool GreasemonkeySlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { | 116 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { |
| 117 scripts_.clear(); | 117 scripts_.clear(); |
| 118 | 118 |
| 119 // Create the shared memory object (read only). | 119 // Create the shared memory object (read only). |
| 120 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); | 120 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); |
| 121 if (!shared_memory_.get()) | 121 if (!shared_memory_.get()) |
| 122 return false; | 122 return false; |
| 123 | 123 |
| 124 // First get the size of the memory block. | 124 // First get the size of the memory block. |
| 125 if (!shared_memory_->Map(sizeof(Pickle::Header))) | 125 if (!shared_memory_->Map(sizeof(Pickle::Header))) |
| 126 return false; | 126 return false; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 142 | 142 |
| 143 for (int i = 0; i < num_scripts; ++i) { | 143 for (int i = 0; i < num_scripts; ++i) { |
| 144 const char* url = NULL; | 144 const char* url = NULL; |
| 145 int url_length = 0; | 145 int url_length = 0; |
| 146 const char* body = NULL; | 146 const char* body = NULL; |
| 147 int body_length = 0; | 147 int body_length = 0; |
| 148 | 148 |
| 149 pickle.ReadData(&iter, &url, &url_length); | 149 pickle.ReadData(&iter, &url, &url_length); |
| 150 pickle.ReadData(&iter, &body, &body_length); | 150 pickle.ReadData(&iter, &body, &body_length); |
| 151 | 151 |
| 152 scripts_.push_back(GreasemonkeyScript(StringPiece(url, url_length))); | 152 scripts_.push_back(UserScript(StringPiece(url, url_length))); |
| 153 GreasemonkeyScript& script = scripts_.back(); | 153 UserScript& script = scripts_.back(); |
| 154 script.Parse(StringPiece(body, body_length)); | 154 script.Parse(StringPiece(body, body_length)); |
| 155 } | 155 } |
| 156 | 156 |
| 157 return true; | 157 return true; |
| 158 } | 158 } |
| 159 | 159 |
| 160 bool GreasemonkeySlave::InjectScripts(WebFrame* frame) { | 160 bool UserScriptSlave::InjectScripts(WebFrame* frame) { |
| 161 for (std::vector<GreasemonkeyScript>::iterator script = scripts_.begin(); | 161 for (std::vector<UserScript>::iterator script = scripts_.begin(); |
| 162 script != scripts_.end(); ++script) { | 162 script != scripts_.end(); ++script) { |
| 163 if (script->MatchesUrl(frame->GetURL())) { | 163 if (script->MatchesUrl(frame->GetURL())) { |
| 164 frame->ExecuteJavaScript(script->GetBody().as_string(), | 164 frame->ExecuteJavaScript(script->GetBody().as_string(), |
| 165 GURL(script->GetURL().as_string())); | 165 GURL(script->GetURL().as_string())); |
| 166 } | 166 } |
| 167 } | 167 } |
| 168 | 168 |
| 169 return true; | 169 return true; |
| 170 } | 170 } |
| OLD | NEW |