LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SuspendBlocker.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2009-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef SUSPENDBLOCKER_H
23 #define SUSPENDBLOCKER_H
24 
25 #include "Common.h"
26 
27 #include <glib.h>
28 #include <lunaservice.h>
29 
31 {
32 public:
33 
34  SuspendBlockerBase(GMainLoop* mainLoop);
36 
37 protected:
38 
39  virtual bool allowSuspend() = 0;
40  virtual void setSuspended(bool) = 0;
41 
42 private:
43 
44  void setupService(LSHandle*& service, GMainLoop* loop);
45  void callService(LSHandle* service, LSFilterFunc callback,
46  const char* url, const char* message);
47  void registerSuspendRequest();
48  void registerPrepareSuspend();
49 
50  static bool cbSuspendRequest(LSHandle* sh, LSMessage* message, void* ctx);
51  static bool cbPrepareSuspend(LSHandle* sh, LSMessage* message, void* ctx);
52  static bool cbResume(LSHandle* sh, LSMessage* message, void* ctx);
53  static bool cbPowerdUp(LSHandle* sh, LSMessage* message, void* ctx);
54  static bool cbIdentify(LSHandle* sh, LSMessage* message, void* ctx);
55 
56 private:
57 
58  char* m_name;
59  char* m_id;
60 
61  GMainLoop* m_mainLoop;
62  LSHandle* m_service;
63 
64  LSHandle* m_nestedService;
65  GMainContext* m_nestedCtxt;
66  GMainLoop* m_nestedLoop;
67 
68  bool m_registeredSuspendRequest;
69  bool m_registeredPrepareSuspend;
70 
71 private:
72 
74  SuspendBlockerBase& operator=(const SuspendBlockerBase&);
75 };
76 
77 template<class Target>
79 {
80 public:
81 
82  typedef bool (Target::*AllowSuspendFunction)();
83  typedef void (Target::*SetSuspendedFunction)(bool);
84 
85  SuspendBlocker(GMainLoop* loop, Target* target, AllowSuspendFunction f1, SetSuspendedFunction f2)
86  : SuspendBlockerBase(loop)
87  , m_target(target)
88  , m_allowSuspendFunction(f1)
89  , m_setSuspendedFunction(f2) {}
90 
91 private:
92 
93  virtual bool allowSuspend() { return (m_target->*m_allowSuspendFunction)(); }
94  virtual void setSuspended(bool isSuspended) { (m_target->*m_setSuspendedFunction)(isSuspended); }
95 
96 private:
97 
98  Target* m_target;
99  AllowSuspendFunction m_allowSuspendFunction;
100  SetSuspendedFunction m_setSuspendedFunction;
101 };
102 
103 
104 #endif /* SUSPENDBLOCKER_H */