Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: chrome/browser/extensions/user_script_master.cc

Issue 19624: Add early-injection capability to user scripts. I haven't yet (Closed)
Patch Set: Use new documentElementAvailable() callback Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/browser/extensions/user_script_master.h" 5 #include "chrome/browser/extensions/user_script_master.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 27 matching lines...) Expand all
38 // http://wiki.greasespot.net/Metadata_block 38 // http://wiki.greasespot.net/Metadata_block
39 StringPiece line; 39 StringPiece line;
40 size_t line_start = 0; 40 size_t line_start = 0;
41 size_t line_end = 0; 41 size_t line_end = 0;
42 bool in_metadata = false; 42 bool in_metadata = false;
43 43
44 static const StringPiece kUserScriptBegin("// ==UserScript=="); 44 static const StringPiece kUserScriptBegin("// ==UserScript==");
45 static const StringPiece kUserScriptEng("// ==/UserScript=="); 45 static const StringPiece kUserScriptEng("// ==/UserScript==");
46 static const StringPiece kIncludeDeclaration("// @include "); 46 static const StringPiece kIncludeDeclaration("// @include ");
47 static const StringPiece kMatchDeclaration("// @match "); 47 static const StringPiece kMatchDeclaration("// @match ");
48 static const StringPiece kRunAtDeclaration("// @run-at ");
49 static const StringPiece kRunAtDocumentStartValue("document-start");
50 static const StringPiece kRunAtDocumentEndValue("document-end");
48 51
49 while (line_start < script_text.length()) { 52 while (line_start < script_text.length()) {
50 line_end = script_text.find('\n', line_start); 53 line_end = script_text.find('\n', line_start);
51 54
52 // Handle the case where there is no trailing newline in the file. 55 // Handle the case where there is no trailing newline in the file.
53 if (line_end == std::string::npos) { 56 if (line_end == std::string::npos) {
54 line_end = script_text.length() - 1; 57 line_end = script_text.length() - 1;
55 } 58 }
56 59
57 line.set(script_text.data() + line_start, line_end - line_start); 60 line.set(script_text.data() + line_start, line_end - line_start);
(...skipping 11 matching lines...) Expand all
69 if (GetDeclarationValue(line, kIncludeDeclaration, &value)) { 72 if (GetDeclarationValue(line, kIncludeDeclaration, &value)) {
70 // We escape some characters that MatchPattern() considers special. 73 // We escape some characters that MatchPattern() considers special.
71 ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\"); 74 ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
72 ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?"); 75 ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
73 script->add_glob(value); 76 script->add_glob(value);
74 } else if (GetDeclarationValue(line, kMatchDeclaration, &value)) { 77 } else if (GetDeclarationValue(line, kMatchDeclaration, &value)) {
75 URLPattern pattern; 78 URLPattern pattern;
76 if (!pattern.Parse(value)) 79 if (!pattern.Parse(value))
77 return false; 80 return false;
78 script->add_url_pattern(pattern); 81 script->add_url_pattern(pattern);
82 } else if (GetDeclarationValue(line, kRunAtDeclaration, &value)) {
83 if (value == kRunAtDocumentStartValue)
84 script->set_run_location(UserScript::DOCUMENT_START);
85 else if (value != kRunAtDocumentEndValue)
86 return false;
79 } 87 }
80 88
81 // TODO(aa): Handle more types of metadata. 89 // TODO(aa): Handle more types of metadata.
82 } 90 }
83 91
84 line_start = line_end + 1; 92 line_start = line_end + 1;
85 } 93 }
86 94
87 // It is probably a mistake to declare both @include and @match rules. 95 // It is probably a mistake to declare both @include and @match rules.
88 if (script->globs().size() > 0 && script->url_patterns().size() > 0) 96 if (script->globs().size() > 0 && script->url_patterns().size() > 0)
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 270
263 StartScan(); 271 StartScan();
264 } 272 }
265 273
266 void UserScriptMaster::StartScan() { 274 void UserScriptMaster::StartScan() {
267 if (!script_reloader_) 275 if (!script_reloader_)
268 script_reloader_ = new ScriptReloader(this); 276 script_reloader_ = new ScriptReloader(this);
269 277
270 script_reloader_->StartScan(worker_loop_, user_script_dir_, lone_scripts_); 278 script_reloader_->StartScan(worker_loop_, user_script_dir_, lone_scripts_);
271 } 279 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698