| 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_GREASEMONKEY_MASTER_H__ | 5 #ifndef CHROME_BROWSER_GREASEMONKEY_MASTER_H_ |
| 6 #define CHROME_BROWSER_GREASEMONKEY_MASTER_H__ | 6 #define CHROME_BROWSER_GREASEMONKEY_MASTER_H_ |
| 7 | 7 |
| 8 #include "base/directory_watcher.h" | 8 #include "base/directory_watcher.h" |
| 9 #include "base/process.h" | 9 #include "base/process.h" |
| 10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| 11 #include "base/shared_memory.h" | 11 #include "base/shared_memory.h" |
| 12 | 12 |
| 13 class MessageLoop; | 13 class MessageLoop; |
| 14 | 14 |
| 15 // Manages a segment of shared memory that contains the Greasemonkey scripts the | 15 // Manages a segment of shared memory that contains the Greasemonkey scripts the |
| 16 // user has installed. Lives on the UI thread. | 16 // user has installed. Lives on the UI thread. |
| 17 class GreasemonkeyMaster : public base::RefCounted<GreasemonkeyMaster>, | 17 class GreasemonkeyMaster : public base::RefCounted<GreasemonkeyMaster>, |
| 18 public DirectoryWatcher::Delegate { | 18 public DirectoryWatcher::Delegate { |
| 19 public: | 19 public: |
| 20 // For testability, the constructor takes the MessageLoop to run the | 20 // For testability, the constructor takes the MessageLoop to run the |
| 21 // script-reloading worker on as well as the path the scripts live in. | 21 // script-reloading worker on as well as the path the scripts live in. |
| 22 // These are normally the file thread and DIR_USER_SCRIPTS. | 22 // These are normally the file thread and DIR_USER_SCRIPTS. |
| 23 GreasemonkeyMaster(MessageLoop* worker, const FilePath& script_dir); | 23 GreasemonkeyMaster(MessageLoop* worker, const FilePath& script_dir); |
| 24 ~GreasemonkeyMaster(); | 24 ~GreasemonkeyMaster(); |
| 25 | 25 |
| 26 // Gets the segment of shared memory for the scripts. | 26 // Gets the segment of shared memory for the scripts. |
| 27 SharedMemory* GetSharedMemory() const { | 27 base::SharedMemory* GetSharedMemory() const { |
| 28 return shared_memory_.get(); | 28 return shared_memory_.get(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Called by the script reloader when new scripts have been loaded. | 31 // Called by the script reloader when new scripts have been loaded. |
| 32 void NewScriptsAvailable(SharedMemory* handle); | 32 void NewScriptsAvailable(base::SharedMemory* handle); |
| 33 | 33 |
| 34 // Return true if we have any scripts ready. | 34 // Return true if we have any scripts ready. |
| 35 bool ScriptsReady() const { return shared_memory_.get() != NULL; } | 35 bool ScriptsReady() const { return shared_memory_.get() != NULL; } |
| 36 | 36 |
| 37 private: | 37 private: |
| 38 class ScriptReloader; | 38 class ScriptReloader; |
| 39 | 39 |
| 40 // DirectoryWatcher::Delegate implementation. | 40 // DirectoryWatcher::Delegate implementation. |
| 41 virtual void OnDirectoryChanged(const FilePath& path); | 41 virtual void OnDirectoryChanged(const FilePath& path); |
| 42 | 42 |
| 43 // Kicks off a process on the file thread to reload scripts from disk | 43 // Kicks off a process on the file thread to reload scripts from disk |
| 44 // into a new chunk of shared memory and notify renderers. | 44 // into a new chunk of shared memory and notify renderers. |
| 45 void StartScan(); | 45 void StartScan(); |
| 46 | 46 |
| 47 // The directory containing user scripts. | 47 // The directory containing user scripts. |
| 48 scoped_ptr<FilePath> user_script_dir_; | 48 scoped_ptr<FilePath> user_script_dir_; |
| 49 | 49 |
| 50 // The watcher watches the profile's user scripts directory for new scripts. | 50 // The watcher watches the profile's user scripts directory for new scripts. |
| 51 scoped_ptr<DirectoryWatcher> dir_watcher_; | 51 scoped_ptr<DirectoryWatcher> dir_watcher_; |
| 52 | 52 |
| 53 // The MessageLoop that the scanner worker runs on. | 53 // The MessageLoop that the scanner worker runs on. |
| 54 // Typically the file thread; configurable for testing. | 54 // Typically the file thread; configurable for testing. |
| 55 MessageLoop* worker_loop_; | 55 MessageLoop* worker_loop_; |
| 56 | 56 |
| 57 // ScriptReloader (in another thread) reloads script off disk. | 57 // ScriptReloader (in another thread) reloads script off disk. |
| 58 // We hang on to our pointer to know if we've already got one running. | 58 // We hang on to our pointer to know if we've already got one running. |
| 59 scoped_refptr<ScriptReloader> script_reloader_; | 59 scoped_refptr<ScriptReloader> script_reloader_; |
| 60 | 60 |
| 61 // Contains the scripts that were found the last time scripts were updated. | 61 // Contains the scripts that were found the last time scripts were updated. |
| 62 scoped_ptr<SharedMemory> shared_memory_; | 62 scoped_ptr<base::SharedMemory> shared_memory_; |
| 63 | 63 |
| 64 // If the script directory is modified while we're rescanning it, we note | 64 // If the script directory is modified while we're rescanning it, we note |
| 65 // that we're currently mid-scan and then start over again once the scan | 65 // that we're currently mid-scan and then start over again once the scan |
| 66 // finishes. This boolean tracks whether another scan is pending. | 66 // finishes. This boolean tracks whether another scan is pending. |
| 67 bool pending_scan_; | 67 bool pending_scan_; |
| 68 | 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(GreasemonkeyMaster); | 69 DISALLOW_COPY_AND_ASSIGN(GreasemonkeyMaster); |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 #endif // CHROME_BROWSER_GREASEMONKEY_MASTER_H__ | 72 #endif // CHROME_BROWSER_GREASEMONKEY_MASTER_H_ |
| OLD | NEW |