Home | History | Annotate | Download | only in init
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef _INIT_ACTION_H
     18 #define _INIT_ACTION_H
     19 
     20 #include <map>
     21 #include <queue>
     22 #include <string>
     23 #include <vector>
     24 
     25 #include "builtins.h"
     26 #include "init_parser.h"
     27 #include "keyword_map.h"
     28 
     29 class Command {
     30 public:
     31     Command(BuiltinFunction f, const std::vector<std::string>& args,
     32             const std::string& filename, int line);
     33 
     34     int InvokeFunc() const;
     35     std::string BuildCommandString() const;
     36     std::string BuildSourceString() const;
     37 
     38 private:
     39     BuiltinFunction func_;
     40     std::vector<std::string> args_;
     41     std::string filename_;
     42     int line_;
     43 };
     44 
     45 class Action {
     46 public:
     47     explicit Action(bool oneshot = false);
     48 
     49     bool AddCommand(const std::vector<std::string>& args,
     50                     const std::string& filename, int line, std::string* err);
     51     void AddCommand(BuiltinFunction f,
     52                     const std::vector<std::string>& args,
     53                     const std::string& filename = "", int line = 0);
     54     void CombineAction(const Action& action);
     55     bool InitTriggers(const std::vector<std::string>& args, std::string* err);
     56     bool InitSingleTrigger(const std::string& trigger);
     57     std::size_t NumCommands() const;
     58     void ExecuteOneCommand(std::size_t command) const;
     59     void ExecuteAllCommands() const;
     60     bool CheckEventTrigger(const std::string& trigger) const;
     61     bool CheckPropertyTrigger(const std::string& name,
     62                               const std::string& value) const;
     63     bool TriggersEqual(const Action& other) const;
     64     std::string BuildTriggersString() const;
     65     void DumpState() const;
     66 
     67     bool oneshot() const { return oneshot_; }
     68     static void set_function_map(const KeywordMap<BuiltinFunction>* function_map) {
     69         function_map_ = function_map;
     70     }
     71 
     72 
     73 private:
     74     void ExecuteCommand(const Command& command) const;
     75     bool CheckPropertyTriggers(const std::string& name = "",
     76                                const std::string& value = "") const;
     77     bool ParsePropertyTrigger(const std::string& trigger, std::string* err);
     78 
     79     std::map<std::string, std::string> property_triggers_;
     80     std::string event_trigger_;
     81     std::vector<Command> commands_;
     82     bool oneshot_;
     83     static const KeywordMap<BuiltinFunction>* function_map_;
     84 };
     85 
     86 class Trigger {
     87 public:
     88     virtual ~Trigger() { }
     89     virtual bool CheckTriggers(const Action& action) const = 0;
     90 };
     91 
     92 class ActionManager {
     93 public:
     94     static ActionManager& GetInstance();
     95 
     96     void AddAction(std::unique_ptr<Action> action);
     97     void QueueEventTrigger(const std::string& trigger);
     98     void QueuePropertyTrigger(const std::string& name, const std::string& value);
     99     void QueueAllPropertyTriggers();
    100     void QueueBuiltinAction(BuiltinFunction func, const std::string& name);
    101     void ExecuteOneCommand();
    102     bool HasMoreCommands() const;
    103     void DumpState() const;
    104 
    105 private:
    106     ActionManager();
    107 
    108     ActionManager(ActionManager const&) = delete;
    109     void operator=(ActionManager const&) = delete;
    110 
    111     std::vector<std::unique_ptr<Action>> actions_;
    112     std::queue<std::unique_ptr<Trigger>> trigger_queue_;
    113     std::queue<const Action*> current_executing_actions_;
    114     std::size_t current_command_;
    115 };
    116 
    117 class ActionParser : public SectionParser {
    118 public:
    119     ActionParser() : action_(nullptr) {
    120     }
    121     bool ParseSection(const std::vector<std::string>& args,
    122                       std::string* err) override;
    123     bool ParseLineSection(const std::vector<std::string>& args,
    124                           const std::string& filename, int line,
    125                           std::string* err) const override;
    126     void EndSection() override;
    127     void EndFile(const std::string&) override {
    128     }
    129 private:
    130     std::unique_ptr<Action> action_;
    131 };
    132 
    133 #endif
    134