OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/user_script_slave.h" | 5 #include "chrome/renderer/user_script_slave.h" |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
8 #include "base/histogram.h" | 8 #include "base/histogram.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/perftimer.h" | 10 #include "base/perftimer.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 static const char kUserScriptTail[] = "\n})(window);"; | 26 static const char kUserScriptTail[] = "\n})(window);"; |
27 | 27 |
28 // Creates a convenient reference to a content script's parent extension. | 28 // Creates a convenient reference to a content script's parent extension. |
29 static const char kInitExtension[] = | 29 static const char kInitExtension[] = |
30 "chrome.extension = new chrome.Extension('%s')"; | 30 "chrome.extension = new chrome.Extension('%s')"; |
31 | 31 |
32 UserScriptSlave::UserScriptSlave() | 32 UserScriptSlave::UserScriptSlave() |
33 : shared_memory_(NULL), | 33 : shared_memory_(NULL), |
34 script_deleter_(&scripts_), | 34 script_deleter_(&scripts_), |
35 user_script_start_line_(0) { | 35 user_script_start_line_(0) { |
36 // TODO: Only windows supports resources and only windows supports user | |
37 // scrips, so only load the Greasemonkey API on windows. Fix this when | |
38 // better cross platofrm support is available. | |
39 #if defined(OS_WIN) | |
40 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( | 36 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( |
41 IDR_GREASEMONKEY_API_JS); | 37 IDR_GREASEMONKEY_API_JS); |
42 #endif | |
43 | 38 |
44 // Count the number of lines that will be injected before the user script. | 39 // Count the number of lines that will be injected before the user script. |
45 StringPiece::size_type pos = 0; | 40 StringPiece::size_type pos = 0; |
46 while ((pos = api_js_.find('\n', pos)) != StringPiece::npos) { | 41 while ((pos = api_js_.find('\n', pos)) != StringPiece::npos) { |
47 user_script_start_line_++; | 42 user_script_start_line_++; |
48 pos++; | 43 pos++; |
49 } | 44 } |
50 | 45 |
51 // NOTE: There is actually one extra line in the injected script because the | 46 // NOTE: There is actually one extra line in the injected script because the |
52 // function header includes a newline as well. But WebKit expects the | 47 // function header includes a newline as well. But WebKit expects the |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 script->css_scripts()[j].set_external_content( | 99 script->css_scripts()[j].set_external_content( |
105 StringPiece(body, body_length)); | 100 StringPiece(body, body_length)); |
106 } | 101 } |
107 } | 102 } |
108 | 103 |
109 return true; | 104 return true; |
110 } | 105 } |
111 | 106 |
112 bool UserScriptSlave::InjectScripts(WebFrame* frame, | 107 bool UserScriptSlave::InjectScripts(WebFrame* frame, |
113 UserScript::RunLocation location) { | 108 UserScript::RunLocation location) { |
| 109 // Don't bother if this is not a URL we inject script into. |
| 110 if (!URLPattern::IsValidScheme(frame->GetURL().scheme())) |
| 111 return true; |
| 112 |
114 PerfTimer timer; | 113 PerfTimer timer; |
115 int num_matched = 0; | 114 int num_matched = 0; |
116 | 115 |
117 for (size_t i = 0; i < scripts_.size(); ++i) { | 116 for (size_t i = 0; i < scripts_.size(); ++i) { |
118 std::vector<WebScriptSource> sources; | 117 std::vector<WebScriptSource> sources; |
119 UserScript* script = scripts_[i]; | 118 UserScript* script = scripts_[i]; |
120 if (!script->MatchesUrl(frame->GetURL())) | 119 if (!script->MatchesUrl(frame->GetURL())) |
121 continue; // This frame doesn't match the script url pattern, skip it. | 120 continue; // This frame doesn't match the script url pattern, skip it. |
122 | 121 |
123 ++num_matched; | 122 ++num_matched; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 HISTOGRAM_TIMES("UserScripts:DocStart:Time", timer.Elapsed()); | 167 HISTOGRAM_TIMES("UserScripts:DocStart:Time", timer.Elapsed()); |
169 } else { | 168 } else { |
170 HISTOGRAM_COUNTS_100("UserScripts:DocEnd:Count", num_matched); | 169 HISTOGRAM_COUNTS_100("UserScripts:DocEnd:Count", num_matched); |
171 HISTOGRAM_TIMES("UserScripts:DocEnd:Time", timer.Elapsed()); | 170 HISTOGRAM_TIMES("UserScripts:DocEnd:Time", timer.Elapsed()); |
172 } | 171 } |
173 | 172 |
174 LOG(INFO) << "Injected " << num_matched << " user scripts into " << | 173 LOG(INFO) << "Injected " << num_matched << " user scripts into " << |
175 frame->GetURL().spec(); | 174 frame->GetURL().spec(); |
176 return true; | 175 return true; |
177 } | 176 } |
OLD | NEW |