Revert of simplify bitlocker, stop calling deprecated accessBitmap() (patchset #1...
[chromium-blink-merge.git] / tools / gn / loader.h
blob5a379ecf9a19f773dd04d7bc2cd01704284c3f2f
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.
5 #ifndef TOOLS_GN_LOADER_H_
6 #define TOOLS_GN_LOADER_H_
8 #include <map>
9 #include <set>
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "tools/gn/label.h"
14 #include "tools/gn/scope.h"
16 namespace base {
17 class MessageLoop;
20 class BuildSettings;
21 class LocationRange;
22 class Settings;
23 class SourceFile;
24 class Toolchain;
26 // The loader manages execution of the different build files. It receives
27 // requests (normally from the Builder) when new references are found, and also
28 // manages loading the build config files.
30 // This loader class is abstract so it can be mocked out for testing the
31 // Builder.
32 class Loader : public base::RefCountedThreadSafe<Loader> {
33 public:
34 Loader();
36 // Loads the given file in the conext of the given toolchain. The initial
37 // call to this (the one that actually starts the generation) should have an
38 // empty toolchain name, which will trigger the load of the default build
39 // config.
40 virtual void Load(const SourceFile& file,
41 const LocationRange& origin,
42 const Label& toolchain_name) = 0;
44 // Notification that the given toolchain has loaded. This will unblock files
45 // waiting on this definition.
46 virtual void ToolchainLoaded(const Toolchain* toolchain) = 0;
48 // Returns the label of the default toolchain.
49 virtual Label GetDefaultToolchain() const = 0;
51 // Returns information about the toolchain with the given label. Will return
52 // false if we haven't processed this toolchain yet.
53 virtual const Settings* GetToolchainSettings(const Label& label) const = 0;
55 // Helper function that extracts the file and toolchain name from the given
56 // label, and calls Load().
57 void Load(const Label& label, const LocationRange& origin);
59 // Returns the build file that the given label references.
60 static SourceFile BuildFileForLabel(const Label& label);
62 // When processing the default build config, we want to capture the argument
63 // of set_default_build_config. The implementation of that function uses this
64 // constant as a property key to get the Label* out of the scope where the
65 // label should be stored.
66 static const void* kDefaultToolchainKey;
68 protected:
69 friend class base::RefCountedThreadSafe<Loader>;
70 virtual ~Loader();
73 class LoaderImpl : public Loader {
74 public:
75 // Callback to emulate InputFileManager::AsyncLoadFile.
76 typedef base::Callback<bool(const LocationRange&,
77 const BuildSettings*,
78 const SourceFile&,
79 const base::Callback<void(const ParseNode*)>&,
80 Err*)> AsyncLoadFileCallback;
82 explicit LoaderImpl(const BuildSettings* build_settings);
84 // Loader implementation.
85 void Load(const SourceFile& file,
86 const LocationRange& origin,
87 const Label& toolchain_name) override;
88 void ToolchainLoaded(const Toolchain* toolchain) override;
89 Label GetDefaultToolchain() const override;
90 const Settings* GetToolchainSettings(const Label& label) const override;
92 // Sets the message loop corresponding to the main thread. By default this
93 // class will use the thread active during construction, but there is not
94 // a message loop active during construction all the time.
95 void set_main_loop(base::MessageLoop* loop) { main_loop_ = loop; }
97 // The complete callback is called whenever there are no more pending loads.
98 // Called on the main thread only. This may be called more than once if the
99 // queue is drained, but then more stuff gets added.
100 void set_complete_callback(const base::Closure& cb) {
101 complete_callback_ = cb;
104 // This callback is used when the loader finds it wants to load a file.
105 void set_async_load_file(const AsyncLoadFileCallback& cb) {
106 async_load_file_ = cb;
109 const Label& default_toolchain_label() const {
110 return default_toolchain_label_;
113 private:
114 struct LoadID;
115 struct ToolchainRecord;
117 ~LoaderImpl() override;
119 // Schedules the input file manager to load the given file.
120 void ScheduleLoadFile(const Settings* settings,
121 const LocationRange& origin,
122 const SourceFile& file);
123 void ScheduleLoadBuildConfig(
124 Settings* settings,
125 const Scope::KeyValueMap& toolchain_overrides);
127 // Runs the given file on the background thread. These are called by the
128 // input file manager.
129 void BackgroundLoadFile(const Settings* settings,
130 const SourceFile& file_name,
131 const ParseNode* root);
132 void BackgroundLoadBuildConfig(
133 Settings* settings,
134 const Scope::KeyValueMap& toolchain_overrides,
135 const ParseNode* root);
137 // Posted to the main thread when any file other than a build config file
138 // file has completed running.
139 void DidLoadFile();
141 // Posted to the main thread when any build config file has completed
142 // running. The label should be the name of the toolchain.
144 // If there is no defauled toolchain loaded yet, we'll assume that the first
145 // call to this indicates to the default toolchain, and this function will
146 // set the default toolchain name to the given label.
147 void DidLoadBuildConfig(const Label& label);
149 // Decrements the pending_loads_ variable and issues the complete callback if
150 // necessary.
151 void DecrementPendingLoads();
153 // Forwards to the appropriate location to load the file.
154 bool AsyncLoadFile(const LocationRange& origin,
155 const BuildSettings* build_settings,
156 const SourceFile& file_name,
157 const base::Callback<void(const ParseNode*)>& callback,
158 Err* err);
160 base::MessageLoop* main_loop_;
162 int pending_loads_;
163 base::Closure complete_callback_;
165 // When non-null, use this callback instead of the InputFileManager for
166 // mocking purposes.
167 AsyncLoadFileCallback async_load_file_;
169 typedef std::set<LoadID> LoadIDSet;
170 LoadIDSet invocations_;
172 const BuildSettings* build_settings_;
173 Label default_toolchain_label_;
175 // Records for the build config file loads.
176 // Owning pointers.
177 typedef std::map<Label, ToolchainRecord*> ToolchainRecordMap;
178 ToolchainRecordMap toolchain_records_;
181 #endif // TOOLS_GN_LOADER_H_