"Fossies" - the Fresh Open Source Software Archive

Member "google-gadgets-for-linux-0.11.2/extensions/linux_system_framework/linux_system_framework.cc" (24 Mar 2009, 7639 Bytes) of package /linux/misc/old/google-gadgets-for-linux-0.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2   Copyright 2008 Google Inc.
    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 #include <ggadget/common.h>
   18 #include <ggadget/gadget_consts.h>
   19 #include <ggadget/slot.h>
   20 #include <ggadget/framework_interface.h>
   21 #include <ggadget/file_system_interface.h>
   22 #include <ggadget/file_manager_interface.h>
   23 #include <ggadget/registerable_interface.h>
   24 #include <ggadget/scriptable_interface.h>
   25 #include <ggadget/scriptable_framework.h>
   26 #include <ggadget/scriptable_file_system.h>
   27 #include <ggadget/permissions.h>
   28 #include <ggadget/gadget.h>
   29 
   30 #include "file_system.h"
   31 #include "runtime.h"
   32 #include "memory.h"
   33 #include "perfmon.h"
   34 #include "process.h"
   35 
   36 #ifdef HAVE_DBUS_LIBRARY
   37 #include "machine.h"
   38 #include "power.h"
   39 #include "user.h"
   40 #ifdef HAVE_NETWORK_MANAGER
   41 #include "network.h"
   42 #endif
   43 #endif
   44 
   45 #define Initialize linux_system_framework_LTX_Initialize
   46 #define Finalize linux_system_framework_LTX_Finalize
   47 #define RegisterFrameworkExtension \
   48     linux_system_framework_LTX_RegisterFrameworkExtension
   49 
   50 namespace ggadget {
   51 namespace framework {
   52 
   53 // To avoid naming conflicts.
   54 namespace linux_system{
   55 
   56 static Runtime *g_runtime_ = NULL;
   57 static Memory *g_memory_ = NULL;
   58 static Process *g_process_ = NULL;
   59 static FileSystem *g_filesystem_ = NULL;
   60 static Perfmon *g_perfmon_ = NULL;
   61 
   62 static ScriptableRuntime *g_script_runtime_ = NULL;
   63 static ScriptableMemory *g_script_memory_ = NULL;
   64 static ScriptableProcess *g_script_process_ = NULL;
   65 
   66 #ifdef HAVE_DBUS_LIBRARY
   67 static Machine *g_machine_ = NULL;
   68 static Power *g_power_ = NULL;
   69 static User *g_user_ = NULL;
   70 
   71 static ScriptableBios *g_script_bios_ = NULL;
   72 static ScriptableMachine *g_script_machine_ = NULL;
   73 static ScriptablePower *g_script_power_ = NULL;
   74 static ScriptableProcessor *g_script_processor_ = NULL;
   75 static ScriptableUser *g_script_user_ = NULL;
   76 
   77 #ifdef HAVE_NETWORK_MANAGER
   78 static Network *g_network_ = NULL;
   79 static ScriptableNetwork *g_script_network_ = NULL;
   80 #endif
   81 #endif
   82 
   83 } // namespace linux_system
   84 } // namespace framework
   85 } // namespace ggadget
   86 
   87 using namespace ggadget;
   88 using namespace ggadget::framework;
   89 using namespace ggadget::framework::linux_system;
   90 
   91 extern "C" {
   92   bool Initialize() {
   93     LOGI("Initialize linux_system_framework extension.");
   94 
   95     g_runtime_ = new Runtime;
   96     g_memory_ = new Memory;
   97     g_process_ = new Process;
   98     g_filesystem_ = new FileSystem;
   99     g_perfmon_ = new Perfmon;
  100 
  101     g_script_runtime_ = new ScriptableRuntime(g_runtime_);
  102     g_script_memory_ = new ScriptableMemory(g_memory_);
  103     g_script_process_ = new ScriptableProcess(g_process_);
  104 
  105 #ifdef HAVE_DBUS_LIBRARY
  106     g_machine_ = new Machine;
  107     g_power_ = new Power;
  108     g_user_ = new User;
  109 
  110     g_script_bios_ = new ScriptableBios(g_machine_);
  111     g_script_machine_ = new ScriptableMachine(g_machine_);
  112     g_script_power_ = new ScriptablePower(g_power_);
  113     g_script_processor_ = new ScriptableProcessor(g_machine_);
  114     g_script_user_ = new ScriptableUser(g_user_);
  115 
  116 #ifdef HAVE_NETWORK_MANAGER
  117     g_network_ = new Network;
  118     g_script_network_ = new ScriptableNetwork(g_network_);
  119 #endif
  120 #endif
  121     return true;
  122   }
  123 
  124   void Finalize() {
  125     LOGI("Finalize linux_system_framework extension.");
  126 
  127     delete g_script_runtime_;
  128     delete g_script_memory_;
  129     delete g_script_process_;
  130 
  131     delete g_runtime_;
  132     delete g_memory_;
  133     delete g_process_;
  134     delete g_filesystem_;
  135     delete g_perfmon_;
  136 
  137 #ifdef HAVE_DBUS_LIBRARY
  138     delete g_script_bios_;
  139     delete g_script_machine_;
  140     delete g_script_power_;
  141     delete g_script_processor_;
  142     delete g_script_user_;
  143 
  144     delete g_machine_;
  145     delete g_power_;
  146     delete g_user_;
  147 
  148 #ifdef HAVE_NETWORK_MANAGER
  149     delete g_script_network_;
  150     delete g_network_;
  151 #endif
  152 #endif
  153   }
  154 
  155   bool RegisterFrameworkExtension(ScriptableInterface *framework,
  156                                   Gadget *gadget) {
  157     LOGI("Register linux_system_framework extension.");
  158     ASSERT(framework && gadget);
  159 
  160     if (!framework)
  161       return false;
  162 
  163     RegisterableInterface *reg_framework = framework->GetRegisterable();
  164     if (!reg_framework) {
  165       LOG("Specified framework is not registerable.");
  166       return false;
  167     }
  168 
  169     ScriptableInterface *system = NULL;
  170     // Gets or adds system object.
  171     ResultVariant prop = framework->GetProperty("system");
  172     if (prop.v().type() != Variant::TYPE_SCRIPTABLE) {
  173       // property "system" is not available or have wrong type, then add one
  174       // with correct type.
  175       // Using SharedScriptable here, so that it can be destroyed correctly
  176       // when framework is destroyed.
  177       system = new SharedScriptable<UINT64_C(0xa5cc5f6479d1441f)>();
  178       reg_framework->RegisterVariantConstant("system", Variant(system));
  179     } else {
  180       system = VariantValue<ScriptableInterface *>()(prop.v());
  181     }
  182 
  183     if (!system) {
  184       LOG("Failed to retrieve or add framework.system object.");
  185       return false;
  186     }
  187 
  188     RegisterableInterface *reg_system = system->GetRegisterable();
  189     if (!reg_system) {
  190       LOG("framework.system object is not registerable.");
  191       return false;
  192     }
  193 
  194     const Permissions *permissions = gadget->GetPermissions();
  195     if (permissions->IsRequiredAndGranted(Permissions::FILE_READ) ||
  196         permissions->IsRequiredAndGranted(Permissions::FILE_WRITE)) {
  197       ScriptableFileSystem *script_filesystem =
  198           new ScriptableFileSystem(g_filesystem_, gadget);
  199       reg_system->RegisterVariantConstant("filesystem",
  200                                           Variant(script_filesystem));
  201     }
  202 
  203     // Check permissions.
  204     if (!permissions->IsRequiredAndGranted(Permissions::DEVICE_STATUS)) {
  205       DLOG("No permission to access device status.");
  206       return true;
  207     }
  208 
  209     // FIXME: Should runtime be restricted by <devicestatus/> ?
  210     reg_framework->RegisterVariantConstant("runtime",
  211                                            Variant(g_script_runtime_));
  212     reg_system->RegisterVariantConstant("memory",
  213                                         Variant(g_script_memory_));
  214     reg_system->RegisterVariantConstant("process",
  215                                         Variant(g_script_process_));
  216 
  217     // ScriptablePerfmon is per gadget, so create a new instance here.
  218     ScriptablePerfmon *script_perfmon =
  219         new ScriptablePerfmon(g_perfmon_, gadget);
  220 
  221     reg_system->RegisterVariantConstant("perfmon", Variant(script_perfmon));
  222 
  223 #ifdef HAVE_DBUS_LIBRARY
  224     reg_system->RegisterVariantConstant("bios",
  225                                         Variant(g_script_bios_));
  226     reg_system->RegisterVariantConstant("machine",
  227                                         Variant(g_script_machine_));
  228 #ifdef HAVE_NETWORK_MANAGER
  229     reg_system->RegisterVariantConstant("network",
  230                                         Variant(g_script_network_));
  231 #endif
  232     reg_system->RegisterVariantConstant("power",
  233                                         Variant(g_script_power_));
  234     reg_system->RegisterVariantConstant("processor",
  235                                         Variant(g_script_processor_));
  236     reg_system->RegisterVariantConstant("user",
  237                                         Variant(g_script_user_));
  238 #endif
  239     return true;
  240   }
  241 }