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

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

Issue 340057: Add first-class support for user scripts (Closed)
Patch Set: newness Created 11 years, 1 month 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 <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/file_path.h" 10 #include "base/file_path.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 bool UserScriptMaster::ScriptReloader::ParseMetadataHeader( 44 bool UserScriptMaster::ScriptReloader::ParseMetadataHeader(
45 const base::StringPiece& script_text, UserScript* script) { 45 const base::StringPiece& script_text, UserScript* script) {
46 // http://wiki.greasespot.net/Metadata_block 46 // http://wiki.greasespot.net/Metadata_block
47 base::StringPiece line; 47 base::StringPiece line;
48 size_t line_start = 0; 48 size_t line_start = 0;
49 size_t line_end = 0; 49 size_t line_end = 0;
50 bool in_metadata = false; 50 bool in_metadata = false;
51 51
52 static const base::StringPiece kUserScriptBegin("// ==UserScript=="); 52 static const base::StringPiece kUserScriptBegin("// ==UserScript==");
53 static const base::StringPiece kUserScriptEng("// ==/UserScript=="); 53 static const base::StringPiece kUserScriptEng("// ==/UserScript==");
54 static const base::StringPiece kNamespaceDeclaration("// @namespace ");
55 static const base::StringPiece kNameDeclaration("// @name ");
56 static const base::StringPiece kDescriptionDeclaration("// @description ");
54 static const base::StringPiece kIncludeDeclaration("// @include "); 57 static const base::StringPiece kIncludeDeclaration("// @include ");
58 static const base::StringPiece kExcludeDeclaration("// @exclude ");
55 static const base::StringPiece kMatchDeclaration("// @match "); 59 static const base::StringPiece kMatchDeclaration("// @match ");
56 static const base::StringPiece kRunAtDeclaration("// @run-at "); 60 static const base::StringPiece kRunAtDeclaration("// @run-at ");
57 static const base::StringPiece kRunAtDocumentStartValue("document-start"); 61 static const base::StringPiece kRunAtDocumentStartValue("document-start");
58 static const base::StringPiece kRunAtDocumentEndValue("document-end"); 62 static const base::StringPiece kRunAtDocumentEndValue("document-end");
59 63
60 while (line_start < script_text.length()) { 64 while (line_start < script_text.length()) {
61 line_end = script_text.find('\n', line_start); 65 line_end = script_text.find('\n', line_start);
62 66
63 // Handle the case where there is no trailing newline in the file. 67 // Handle the case where there is no trailing newline in the file.
64 if (line_end == std::string::npos) 68 if (line_end == std::string::npos)
65 line_end = script_text.length() - 1; 69 line_end = script_text.length() - 1;
66 70
67 line.set(script_text.data() + line_start, line_end - line_start); 71 line.set(script_text.data() + line_start, line_end - line_start);
68 72
69 if (!in_metadata) { 73 if (!in_metadata) {
70 if (line.starts_with(kUserScriptBegin)) 74 if (line.starts_with(kUserScriptBegin))
71 in_metadata = true; 75 in_metadata = true;
72 } else { 76 } else {
73 if (line.starts_with(kUserScriptEng)) 77 if (line.starts_with(kUserScriptEng))
74 break; 78 break;
75 79
76 std::string value; 80 std::string value;
77 if (GetDeclarationValue(line, kIncludeDeclaration, &value)) { 81 if (GetDeclarationValue(line, kIncludeDeclaration, &value)) {
78 // We escape some characters that MatchPattern() considers special. 82 // We escape some characters that MatchPattern() considers special.
79 ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\"); 83 ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
80 ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?"); 84 ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
81 script->add_glob(value); 85 script->add_glob(value);
86 } else if (GetDeclarationValue(line, kExcludeDeclaration, &value)) {
87 ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
88 ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
89 script->add_exclude_glob(value);
90 } else if (GetDeclarationValue(line, kNamespaceDeclaration, &value)) {
91 script->set_name_space(value);
92 } else if (GetDeclarationValue(line, kNameDeclaration, &value)) {
93 script->set_name(value);
94 } else if (GetDeclarationValue(line, kDescriptionDeclaration, &value)) {
95 script->set_description(value);
82 } else if (GetDeclarationValue(line, kMatchDeclaration, &value)) { 96 } else if (GetDeclarationValue(line, kMatchDeclaration, &value)) {
83 URLPattern pattern; 97 URLPattern pattern;
84 if (!pattern.Parse(value)) 98 if (!pattern.Parse(value))
85 return false; 99 return false;
86 script->add_url_pattern(pattern); 100 script->add_url_pattern(pattern);
87 } else if (GetDeclarationValue(line, kRunAtDeclaration, &value)) { 101 } else if (GetDeclarationValue(line, kRunAtDeclaration, &value)) {
88 if (value == kRunAtDocumentStartValue) 102 if (value == kRunAtDocumentStartValue)
89 script->set_run_location(UserScript::DOCUMENT_START); 103 script->set_run_location(UserScript::DOCUMENT_START);
90 else if (value != kRunAtDocumentEndValue) 104 else if (value != kRunAtDocumentEndValue)
91 return false; 105 return false;
92 } 106 }
93 107
94 // TODO(aa): Handle more types of metadata. 108 // TODO(aa): Handle more types of metadata.
95 } 109 }
96 110
97 line_start = line_end + 1; 111 line_start = line_end + 1;
98 } 112 }
99 113
100 // It is probably a mistake to declare both @include and @match rules.
101 if (script->globs().size() > 0 && script->url_patterns().size() > 0)
102 return false;
103
104 // If no patterns were specified, default to @include *. This is what 114 // If no patterns were specified, default to @include *. This is what
105 // Greasemonkey does. 115 // Greasemonkey does.
106 if (script->globs().size() == 0 && script->url_patterns().size() == 0) 116 if (script->globs().size() == 0 && script->url_patterns().size() == 0)
107 script->add_glob("*"); 117 script->add_glob("*");
108 118
109 return true; 119 return true;
110 } 120 }
111 121
112 void UserScriptMaster::ScriptReloader::StartScan( 122 void UserScriptMaster::ScriptReloader::StartScan(
113 const FilePath& script_dir, const UserScriptList& lone_scripts) { 123 const FilePath& script_dir, const UserScriptList& lone_scripts) {
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 DCHECK(false); 386 DCHECK(false);
377 } 387 }
378 } 388 }
379 389
380 void UserScriptMaster::StartScan() { 390 void UserScriptMaster::StartScan() {
381 if (!script_reloader_) 391 if (!script_reloader_)
382 script_reloader_ = new ScriptReloader(this); 392 script_reloader_ = new ScriptReloader(this);
383 393
384 script_reloader_->StartScan(user_script_dir_, lone_scripts_); 394 script_reloader_->StartScan(user_script_dir_, lone_scripts_);
385 } 395 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698