| 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/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" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
| 14 #include "base/stl_util-inl.h" | 14 #include "base/stl_util-inl.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/thread.h" | 16 #include "base/thread.h" |
| 17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/extensions/extensions_service.h" | 18 #include "chrome/browser/extensions/extensions_service.h" |
| 19 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/notification_service.h" | 20 #include "chrome/common/notification_service.h" |
| 21 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 22 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 23 | 23 |
| 24 | 24 |
| 25 // Helper function to parse greasesmonkey headers | 25 // Helper function to parse greasesmonkey headers |
| 26 static bool GetDeclarationValue(const StringPiece& line, | 26 static bool GetDeclarationValue(const base::StringPiece& line, |
| 27 const StringPiece& prefix, | 27 const base::StringPiece& prefix, |
| 28 std::string* value) { | 28 std::string* value) { |
| 29 if (!line.starts_with(prefix)) | 29 if (!line.starts_with(prefix)) |
| 30 return false; | 30 return false; |
| 31 | 31 |
| 32 std::string temp(line.data() + prefix.length(), | 32 std::string temp(line.data() + prefix.length(), |
| 33 line.length() - prefix.length()); | 33 line.length() - prefix.length()); |
| 34 TrimWhitespaceASCII(temp, TRIM_ALL, value); | 34 TrimWhitespaceASCII(temp, TRIM_ALL, value); |
| 35 return true; | 35 return true; |
| 36 } | 36 } |
| 37 | 37 |
| 38 UserScriptMaster::ScriptReloader::ScriptReloader(UserScriptMaster* master) | 38 UserScriptMaster::ScriptReloader::ScriptReloader(UserScriptMaster* master) |
| 39 : master_(master), | 39 : master_(master), |
| 40 master_message_loop_(MessageLoop::current()) { | 40 master_message_loop_(MessageLoop::current()) { |
| 41 } | 41 } |
| 42 | 42 |
| 43 // static | 43 // static |
| 44 bool UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 44 bool UserScriptMaster::ScriptReloader::ParseMetadataHeader( |
| 45 const 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 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 StringPiece kUserScriptBegin("// ==UserScript=="); | 52 static const base::StringPiece kUserScriptBegin("// ==UserScript=="); |
| 53 static const StringPiece kUserScriptEng("// ==/UserScript=="); | 53 static const base::StringPiece kUserScriptEng("// ==/UserScript=="); |
| 54 static const StringPiece kIncludeDeclaration("// @include "); | 54 static const base::StringPiece kIncludeDeclaration("// @include "); |
| 55 static const StringPiece kMatchDeclaration("// @match "); | 55 static const base::StringPiece kMatchDeclaration("// @match "); |
| 56 static const StringPiece kRunAtDeclaration("// @run-at "); | 56 static const base::StringPiece kRunAtDeclaration("// @run-at "); |
| 57 static const StringPiece kRunAtDocumentStartValue("document-start"); | 57 static const base::StringPiece kRunAtDocumentStartValue("document-start"); |
| 58 static const StringPiece kRunAtDocumentEndValue("document-end"); | 58 static const base::StringPiece kRunAtDocumentEndValue("document-end"); |
| 59 | 59 |
| 60 while (line_start < script_text.length()) { | 60 while (line_start < script_text.length()) { |
| 61 line_end = script_text.find('\n', line_start); | 61 line_end = script_text.find('\n', line_start); |
| 62 | 62 |
| 63 // Handle the case where there is no trailing newline in the file. | 63 // Handle the case where there is no trailing newline in the file. |
| 64 if (line_end == std::string::npos) | 64 if (line_end == std::string::npos) |
| 65 line_end = script_text.length() - 1; | 65 line_end = script_text.length() - 1; |
| 66 | 66 |
| 67 line.set(script_text.data() + line_start, line_end - line_start); | 67 line.set(script_text.data() + line_start, line_end - line_start); |
| 68 | 68 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 // Pickle user scripts and return pointer to the shared memory. | 197 // Pickle user scripts and return pointer to the shared memory. |
| 198 static base::SharedMemory* Serialize(const UserScriptList& scripts) { | 198 static base::SharedMemory* Serialize(const UserScriptList& scripts) { |
| 199 Pickle pickle; | 199 Pickle pickle; |
| 200 pickle.WriteSize(scripts.size()); | 200 pickle.WriteSize(scripts.size()); |
| 201 for (size_t i = 0; i < scripts.size(); i++) { | 201 for (size_t i = 0; i < scripts.size(); i++) { |
| 202 const UserScript& script = scripts[i]; | 202 const UserScript& script = scripts[i]; |
| 203 script.Pickle(&pickle); | 203 script.Pickle(&pickle); |
| 204 // Write scripts as 'data' so that we can read it out in the slave without | 204 // Write scripts as 'data' so that we can read it out in the slave without |
| 205 // allocating a new string. | 205 // allocating a new string. |
| 206 for (size_t j = 0; j < script.js_scripts().size(); j++) { | 206 for (size_t j = 0; j < script.js_scripts().size(); j++) { |
| 207 StringPiece contents = script.js_scripts()[j].GetContent(); | 207 base::StringPiece contents = script.js_scripts()[j].GetContent(); |
| 208 pickle.WriteData(contents.data(), contents.length()); | 208 pickle.WriteData(contents.data(), contents.length()); |
| 209 } | 209 } |
| 210 for (size_t j = 0; j < script.css_scripts().size(); j++) { | 210 for (size_t j = 0; j < script.css_scripts().size(); j++) { |
| 211 StringPiece contents = script.css_scripts()[j].GetContent(); | 211 base::StringPiece contents = script.css_scripts()[j].GetContent(); |
| 212 pickle.WriteData(contents.data(), contents.length()); | 212 pickle.WriteData(contents.data(), contents.length()); |
| 213 } | 213 } |
| 214 } | 214 } |
| 215 | 215 |
| 216 // Create the shared memory object. | 216 // Create the shared memory object. |
| 217 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); | 217 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); |
| 218 | 218 |
| 219 if (!shared_memory->Create(std::wstring(), // anonymous | 219 if (!shared_memory->Create(std::wstring(), // anonymous |
| 220 false, // read-only | 220 false, // read-only |
| 221 false, // open existing | 221 false, // open existing |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 DCHECK(false); | 376 DCHECK(false); |
| 377 } | 377 } |
| 378 } | 378 } |
| 379 | 379 |
| 380 void UserScriptMaster::StartScan() { | 380 void UserScriptMaster::StartScan() { |
| 381 if (!script_reloader_) | 381 if (!script_reloader_) |
| 382 script_reloader_ = new ScriptReloader(this); | 382 script_reloader_ = new ScriptReloader(this); |
| 383 | 383 |
| 384 script_reloader_->StartScan(worker_loop_, user_script_dir_, lone_scripts_); | 384 script_reloader_->StartScan(worker_loop_, user_script_dir_, lone_scripts_); |
| 385 } | 385 } |
| OLD | NEW |