| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef TOOLS_GN_INPUT_FILE_MANAGER_H_ | |
| 6 #define TOOLS_GN_INPUT_FILE_MANAGER_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 #include <utility> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/callback.h" | |
| 14 #include "base/containers/hash_tables.h" | |
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/synchronization/lock.h" | |
| 18 #include "base/synchronization/waitable_event.h" | |
| 19 #include "tools/gn/build_settings.h" | |
| 20 #include "tools/gn/input_file.h" | |
| 21 #include "tools/gn/parse_tree.h" | |
| 22 #include "tools/gn/settings.h" | |
| 23 | |
| 24 class Err; | |
| 25 class LocationRange; | |
| 26 class ParseNode; | |
| 27 class Token; | |
| 28 | |
| 29 // Manages loading and parsing files from disk. This doesn't actually have | |
| 30 // any context for executing the results, so potentially multiple configs | |
| 31 // could use the same input file (saving parsing). | |
| 32 // | |
| 33 // This class is threadsafe. | |
| 34 // | |
| 35 // InputFile objects must never be deleted while the program is running since | |
| 36 // various state points into them. | |
| 37 class InputFileManager : public base::RefCountedThreadSafe<InputFileManager> { | |
| 38 public: | |
| 39 // Callback issued when a file is laoded. On auccess, the parse node will | |
| 40 // refer to the root block of the file. On failure, this will be NULL. | |
| 41 typedef base::Callback<void(const ParseNode*)> FileLoadCallback; | |
| 42 | |
| 43 InputFileManager(); | |
| 44 | |
| 45 // Loads the given file and executes the callback on the worker pool. | |
| 46 // | |
| 47 // There are two types of errors. For errors known synchronously, the error | |
| 48 // will be set, it will return false, and no work will be scheduled. | |
| 49 // | |
| 50 // For parse errors and such that happen in the future, the error will be | |
| 51 // logged to the scheduler and the callback will be invoked with a null | |
| 52 // ParseNode pointer. The given |origin| will be blamed for the invocation. | |
| 53 bool AsyncLoadFile(const LocationRange& origin, | |
| 54 const BuildSettings* build_settings, | |
| 55 const SourceFile& file_name, | |
| 56 const FileLoadCallback& callback, | |
| 57 Err* err); | |
| 58 | |
| 59 // Loads and parses the given file synchronously, returning the root block | |
| 60 // corresponding to the parsed result. On error, return NULL and the given | |
| 61 // Err is set. | |
| 62 const ParseNode* SyncLoadFile(const LocationRange& origin, | |
| 63 const BuildSettings* build_settings, | |
| 64 const SourceFile& file_name, | |
| 65 Err* err); | |
| 66 | |
| 67 int GetInputFileCount() const; | |
| 68 | |
| 69 void GetAllInputFileNames(std::vector<SourceFile>* result) const; | |
| 70 | |
| 71 private: | |
| 72 friend class base::RefCountedThreadSafe<InputFileManager>; | |
| 73 | |
| 74 struct InputFileData { | |
| 75 InputFileData(const SourceFile& file_name); | |
| 76 ~InputFileData(); | |
| 77 | |
| 78 // Don't touch this outside the lock until it's marked loaded. | |
| 79 InputFile file; | |
| 80 | |
| 81 bool loaded; | |
| 82 | |
| 83 bool sync_invocation; | |
| 84 | |
| 85 // Lists all invocations that need to be executed when the file completes | |
| 86 // loading. | |
| 87 std::vector<FileLoadCallback> scheduled_callbacks; | |
| 88 | |
| 89 // Event to signal when the load is complete (or fails). This is lazily | |
| 90 // created only when a thread is synchronously waiting for this load (which | |
| 91 // only happens for imports). | |
| 92 scoped_ptr<base::WaitableEvent> completion_event; | |
| 93 | |
| 94 std::vector<Token> tokens; | |
| 95 | |
| 96 // Null before the file is loaded or if loading failed. | |
| 97 scoped_ptr<ParseNode> parsed_root; | |
| 98 }; | |
| 99 | |
| 100 virtual ~InputFileManager(); | |
| 101 | |
| 102 void BackgroundLoadFile(const LocationRange& origin, | |
| 103 const BuildSettings* build_settings, | |
| 104 const SourceFile& name, | |
| 105 InputFile* file); | |
| 106 | |
| 107 // Loads the given file. On error, sets the Err and return false. | |
| 108 bool LoadFile(const LocationRange& origin, | |
| 109 const BuildSettings* build_settings, | |
| 110 const SourceFile& name, | |
| 111 InputFile* file, | |
| 112 Err* err); | |
| 113 | |
| 114 mutable base::Lock lock_; | |
| 115 | |
| 116 // Maps repo-relative filenames to the corresponding owned pointer. | |
| 117 typedef base::hash_map<SourceFile, InputFileData*> InputFileMap; | |
| 118 InputFileMap input_files_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(InputFileManager); | |
| 121 }; | |
| 122 | |
| 123 #endif // TOOLS_GN_INPUT_FILE_MANAGER_H_ | |
| OLD | NEW |