Home | History | Annotate | Download | only in daemon
      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 SYSTEM_NATIVEPOWER_DAEMON_WAKE_LOCK_MANAGER_H_
     18 #define SYSTEM_NATIVEPOWER_DAEMON_WAKE_LOCK_MANAGER_H_
     19 
     20 #include <sys/types.h>
     21 
     22 #include <map>
     23 #include <string>
     24 
     25 #include <base/files/file_path.h>
     26 #include <base/macros.h>
     27 #include <base/time/time.h>
     28 #include <utils/StrongPointer.h>
     29 
     30 namespace android {
     31 
     32 class IBinder;
     33 
     34 class WakeLockManagerInterface {
     35  public:
     36   WakeLockManagerInterface() {}
     37   virtual ~WakeLockManagerInterface() {}
     38 
     39   virtual bool AddRequest(sp<IBinder> client_binder,
     40                           const std::string& tag,
     41                           const std::string& package,
     42                           uid_t uid) = 0;
     43   virtual bool RemoveRequest(sp<IBinder> client_binder) = 0;
     44 
     45  protected:
     46   // Information about a request from a client.
     47   struct Request {
     48     Request(const std::string& tag, const std::string& package, uid_t uid);
     49     Request(const Request& request);
     50     Request();
     51 
     52     std::string tag;
     53     std::string package;
     54     uid_t uid;
     55   };
     56 };
     57 
     58 class WakeLockManager : public WakeLockManagerInterface {
     59  public:
     60   // Name of the kernel wake lock created by this class.
     61   static const char kLockName[];
     62 
     63   WakeLockManager();
     64   ~WakeLockManager() override;
     65 
     66   void set_paths_for_testing(const base::FilePath& lock_path,
     67                              const base::FilePath& unlock_path) {
     68     lock_path_ = lock_path;
     69     unlock_path_ = unlock_path;
     70   }
     71 
     72   bool Init();
     73 
     74   // WakeLockManagerInterface:
     75   bool AddRequest(sp<IBinder> client_binder,
     76                   const std::string& tag,
     77                   const std::string& package,
     78                   uid_t uid) override;
     79   bool RemoveRequest(sp<IBinder> client_binder) override;
     80 
     81  private:
     82   void HandleBinderDeath(sp<IBinder> binder);
     83 
     84   base::FilePath lock_path_;
     85   base::FilePath unlock_path_;
     86 
     87   // Currently-active requests, keyed by client binders.
     88   std::map<sp<IBinder>, Request> requests_;
     89 
     90   DISALLOW_COPY_AND_ASSIGN(WakeLockManager);
     91 };
     92 
     93 }  // namespace android
     94 
     95 #endif  // SYSTEM_NATIVEPOWER_DAEMON_WAKE_LOCK_MANAGER_H_
     96