| 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" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 else | 129 else |
| 130 master_->NewScriptsAvailable(memory); | 130 master_->NewScriptsAvailable(memory); |
| 131 | 131 |
| 132 // Drop our self-reference. | 132 // Drop our self-reference. |
| 133 // Balances StartScan(). | 133 // Balances StartScan(). |
| 134 Release(); | 134 Release(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 static bool LoadScriptContent(UserScript::File* script_file) { | 137 static bool LoadScriptContent(UserScript::File* script_file) { |
| 138 std::string content; | 138 std::string content; |
| 139 if (!file_util::ReadFileToString(script_file->path(), &content)) { | 139 FilePath path = script_file->resource().GetFilePath(); |
| 140 LOG(WARNING) << "Failed to load user script file: " | 140 if (path.empty() || !file_util::ReadFileToString(path, &content)) { |
| 141 << script_file->path().value(); | 141 LOG(WARNING) << "Failed to load user script file: " << path.value(); |
| 142 return false; | 142 return false; |
| 143 } | 143 } |
| 144 | 144 |
| 145 script_file->set_content(content); | 145 script_file->set_content(content); |
| 146 LOG(INFO) << "Loaded user script file: " << script_file->path().value(); | 146 LOG(INFO) << "Loaded user script file: " << path.value(); |
| 147 return true; | 147 return true; |
| 148 } | 148 } |
| 149 | 149 |
| 150 void UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( | 150 void UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( |
| 151 const FilePath& script_dir, UserScriptList* result) { | 151 const FilePath& script_dir, UserScriptList* result) { |
| 152 // Clear the list. We will populate it with the scrips found in script_dir. | 152 // Clear the list. We will populate it with the scripts found in script_dir. |
| 153 result->clear(); | 153 result->clear(); |
| 154 | 154 |
| 155 // Find all the scripts in |script_dir|. | 155 // Find all the scripts in |script_dir|. |
| 156 if (!script_dir.value().empty()) { | 156 if (!script_dir.value().empty()) { |
| 157 // Create the "<Profile>/User Scripts" directory if it doesn't exist | 157 // Create the "<Profile>/User Scripts" directory if it doesn't exist |
| 158 if (!file_util::DirectoryExists(script_dir)) | 158 if (!file_util::DirectoryExists(script_dir)) |
| 159 file_util::CreateDirectory(script_dir); | 159 file_util::CreateDirectory(script_dir); |
| 160 | 160 |
| 161 file_util::FileEnumerator enumerator(script_dir, false, | 161 file_util::FileEnumerator enumerator(script_dir, false, |
| 162 file_util::FileEnumerator::FILES, | 162 file_util::FileEnumerator::FILES, |
| 163 FILE_PATH_LITERAL("*.user.js")); | 163 FILE_PATH_LITERAL("*.user.js")); |
| 164 for (FilePath file = enumerator.Next(); !file.value().empty(); | 164 for (FilePath file = enumerator.Next(); !file.value().empty(); |
| 165 file = enumerator.Next()) { | 165 file = enumerator.Next()) { |
| 166 result->push_back(UserScript()); | 166 result->push_back(UserScript()); |
| 167 UserScript& user_script = result->back(); | 167 UserScript& user_script = result->back(); |
| 168 // Push single js file in this UserScript. | 168 // Push single js file in this UserScript. |
| 169 GURL url(std::string(chrome::kUserScriptScheme) + ":/" + | 169 GURL url(std::string(chrome::kUserScriptScheme) + ":/" + |
| 170 net::FilePathToFileURL(file).ExtractFileName()); | 170 net::FilePathToFileURL(file).ExtractFileName()); |
| 171 user_script.js_scripts().push_back(UserScript::File(file, url)); | 171 ExtensionResource resource(script_dir, file.BaseName()); |
| 172 user_script.js_scripts().push_back(UserScript::File(resource, url)); |
| 172 UserScript::File& script_file = user_script.js_scripts().back(); | 173 UserScript::File& script_file = user_script.js_scripts().back(); |
| 173 if (!LoadScriptContent(&script_file)) | 174 if (!LoadScriptContent(&script_file)) |
| 174 result->pop_back(); | 175 result->pop_back(); |
| 175 else | 176 else |
| 176 ParseMetadataHeader(script_file.GetContent(), &user_script); | 177 ParseMetadataHeader(script_file.GetContent(), &user_script); |
| 177 } | 178 } |
| 178 } | 179 } |
| 179 } | 180 } |
| 180 | 181 |
| 181 static void LoadLoneScripts(UserScriptList* lone_scripts) { | 182 static void LoadLoneScripts(UserScriptList* lone_scripts) { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 DCHECK(false); | 377 DCHECK(false); |
| 377 } | 378 } |
| 378 } | 379 } |
| 379 | 380 |
| 380 void UserScriptMaster::StartScan() { | 381 void UserScriptMaster::StartScan() { |
| 381 if (!script_reloader_) | 382 if (!script_reloader_) |
| 382 script_reloader_ = new ScriptReloader(this); | 383 script_reloader_ = new ScriptReloader(this); |
| 383 | 384 |
| 384 script_reloader_->StartScan(worker_loop_, user_script_dir_, lone_scripts_); | 385 script_reloader_->StartScan(worker_loop_, user_script_dir_, lone_scripts_); |
| 385 } | 386 } |
| OLD | NEW |