| 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 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/directory_watcher.h" | 10 #include "base/directory_watcher.h" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "base/shared_memory.h" | 13 #include "base/shared_memory.h" |
| 14 #include "chrome/browser/chrome_thread.h" |
| 14 #include "chrome/common/extensions/user_script.h" | 15 #include "chrome/common/extensions/user_script.h" |
| 15 #include "chrome/common/notification_registrar.h" | 16 #include "chrome/common/notification_registrar.h" |
| 16 #include "testing/gtest/include/gtest/gtest_prod.h" | 17 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 17 | 18 |
| 18 class MessageLoop; | |
| 19 namespace base { | 19 namespace base { |
| 20 class StringPiece; | 20 class StringPiece; |
| 21 } | 21 } |
| 22 | 22 |
| 23 // Manages a segment of shared memory that contains the user scripts the user | 23 // Manages a segment of shared memory that contains the user scripts the user |
| 24 // has installed. Lives on the UI thread. | 24 // has installed. Lives on the UI thread. |
| 25 class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>, | 25 class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>, |
| 26 public DirectoryWatcher::Delegate, | 26 public DirectoryWatcher::Delegate, |
| 27 public NotificationObserver { | 27 public NotificationObserver { |
| 28 public: | 28 public: |
| 29 // For testability, the constructor takes the MessageLoop to run the | 29 // For testability, the constructor takes the path the scripts live in. |
| 30 // script-reloading worker on as well as the path the scripts live in. | 30 // This is normally a directory inside the profile. |
| 31 // These are normally the file thread and a directory inside the profile. | 31 UserScriptMaster(const FilePath& script_dir); |
| 32 UserScriptMaster(MessageLoop* worker, const FilePath& script_dir); | |
| 33 virtual ~UserScriptMaster(); | 32 virtual ~UserScriptMaster(); |
| 34 | 33 |
| 35 // Add a watched directory. All scripts will be reloaded when any file in | 34 // Add a watched directory. All scripts will be reloaded when any file in |
| 36 // this directory changes. | 35 // this directory changes. |
| 37 void AddWatchedPath(const FilePath& path); | 36 void AddWatchedPath(const FilePath& path); |
| 38 | 37 |
| 39 // Kicks off a process on the file thread to reload scripts from disk | 38 // Kicks off a process on the file thread to reload scripts from disk |
| 40 // into a new chunk of shared memory and notify renderers. | 39 // into a new chunk of shared memory and notify renderers. |
| 41 virtual void StartScan(); | 40 virtual void StartScan(); |
| 42 | 41 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 74 static bool ParseMetadataHeader(const base::StringPiece& script_text, |
| 76 UserScript* script); | 75 UserScript* script); |
| 77 | 76 |
| 78 static void LoadScriptsFromDirectory(const FilePath& script_dir, | 77 static void LoadScriptsFromDirectory(const FilePath& script_dir, |
| 79 UserScriptList* result); | 78 UserScriptList* result); |
| 80 | 79 |
| 81 explicit ScriptReloader(UserScriptMaster* master); | 80 explicit ScriptReloader(UserScriptMaster* master); |
| 82 | 81 |
| 83 // Start a scan for scripts. | 82 // Start a scan for scripts. |
| 84 // Will always send a message to the master upon completion. | 83 // Will always send a message to the master upon completion. |
| 85 void StartScan(MessageLoop* work_loop, const FilePath& script_dir, | 84 void StartScan(const FilePath& script_dir, |
| 86 const UserScriptList& external_scripts); | 85 const UserScriptList& external_scripts); |
| 87 | 86 |
| 88 // The master is going away; don't call it back. | 87 // The master is going away; don't call it back. |
| 89 void DisownMaster() { | 88 void DisownMaster() { |
| 90 master_ = NULL; | 89 master_ = NULL; |
| 91 } | 90 } |
| 92 | 91 |
| 93 private: | 92 private: |
| 94 // Where functions are run: | 93 // Where functions are run: |
| 95 // master file | 94 // master file |
| (...skipping 11 matching lines...) Expand all Loading... |
| 107 // done. The parameters are intentionally passed by value so their lifetimes | 106 // done. The parameters are intentionally passed by value so their lifetimes |
| 108 // aren't tied to the caller. | 107 // aren't tied to the caller. |
| 109 void RunScan(const FilePath script_dir, UserScriptList lone_scripts); | 108 void RunScan(const FilePath script_dir, UserScriptList lone_scripts); |
| 110 | 109 |
| 111 // A pointer back to our master. | 110 // A pointer back to our master. |
| 112 // May be NULL if DisownMaster() is called. | 111 // May be NULL if DisownMaster() is called. |
| 113 UserScriptMaster* master_; | 112 UserScriptMaster* master_; |
| 114 | 113 |
| 115 // The message loop to call our master back on. | 114 // The message loop to call our master back on. |
| 116 // Expected to always outlive us. | 115 // Expected to always outlive us. |
| 117 MessageLoop* master_message_loop_; | 116 ChromeThread::ID master_thread_id_; |
| 118 | 117 |
| 119 DISALLOW_COPY_AND_ASSIGN(ScriptReloader); | 118 DISALLOW_COPY_AND_ASSIGN(ScriptReloader); |
| 120 }; | 119 }; |
| 121 | 120 |
| 122 // DirectoryWatcher::Delegate implementation. | 121 // DirectoryWatcher::Delegate implementation. |
| 123 virtual void OnDirectoryChanged(const FilePath& path); | 122 virtual void OnDirectoryChanged(const FilePath& path); |
| 124 | 123 |
| 125 // NotificationObserver implementation. | 124 // NotificationObserver implementation. |
| 126 virtual void Observe(NotificationType type, | 125 virtual void Observe(NotificationType type, |
| 127 const NotificationSource& source, | 126 const NotificationSource& source, |
| 128 const NotificationDetails& details); | 127 const NotificationDetails& details); |
| 129 | 128 |
| 130 // Manages our notification registrations. | 129 // Manages our notification registrations. |
| 131 NotificationRegistrar registrar_; | 130 NotificationRegistrar registrar_; |
| 132 | 131 |
| 133 // The directories containing user scripts. | 132 // The directories containing user scripts. |
| 134 FilePath user_script_dir_; | 133 FilePath user_script_dir_; |
| 135 | 134 |
| 136 // The watcher watches the profile's user scripts directory for new scripts. | 135 // The watcher watches the profile's user scripts directory for new scripts. |
| 137 std::vector<DirectoryWatcher*> dir_watchers_; | 136 std::vector<DirectoryWatcher*> dir_watchers_; |
| 138 | 137 |
| 139 // The MessageLoop that the scanner worker runs on. | |
| 140 // Typically the file thread; configurable for testing. | |
| 141 MessageLoop* worker_loop_; | |
| 142 | |
| 143 // ScriptReloader (in another thread) reloads script off disk. | 138 // ScriptReloader (in another thread) reloads script off disk. |
| 144 // We hang on to our pointer to know if we've already got one running. | 139 // We hang on to our pointer to know if we've already got one running. |
| 145 scoped_refptr<ScriptReloader> script_reloader_; | 140 scoped_refptr<ScriptReloader> script_reloader_; |
| 146 | 141 |
| 147 // Contains the scripts that were found the last time scripts were updated. | 142 // Contains the scripts that were found the last time scripts were updated. |
| 148 scoped_ptr<base::SharedMemory> shared_memory_; | 143 scoped_ptr<base::SharedMemory> shared_memory_; |
| 149 | 144 |
| 150 // List of scripts outside of script directories we should also load. | 145 // List of scripts outside of script directories we should also load. |
| 151 UserScriptList lone_scripts_; | 146 UserScriptList lone_scripts_; |
| 152 | 147 |
| 153 // If the extensions service has finished loading its initial set of | 148 // If the extensions service has finished loading its initial set of |
| 154 // extensions. | 149 // extensions. |
| 155 bool extensions_service_ready_; | 150 bool extensions_service_ready_; |
| 156 | 151 |
| 157 // If the script directory is modified while we're rescanning it, we note | 152 // If the script directory is modified while we're rescanning it, we note |
| 158 // that we're currently mid-scan and then start over again once the scan | 153 // that we're currently mid-scan and then start over again once the scan |
| 159 // finishes. This boolean tracks whether another scan is pending. | 154 // finishes. This boolean tracks whether another scan is pending. |
| 160 bool pending_scan_; | 155 bool pending_scan_; |
| 161 | 156 |
| 162 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); | 157 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); |
| 163 }; | 158 }; |
| 164 | 159 |
| 165 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | 160 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ |
| OLD | NEW |