591cb069a2f2aa5ea8bd902d370f79a4c7199ea0
[platform/framework/web/crosswalk-tizen.git] / src / extension / extension.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. 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.
4
5 #include "extension/extension.h"
6
7 #include <dlfcn.h>
8 #include <string>
9
10 #include "common/logger.h"
11 #include "extension/extension_adapter.h"
12 #include "extension/xwalk/XW_Extension.h"
13
14 namespace wrt {
15
16 Extension::Extension(const std::string& path, ExtensionDelegate* delegate)
17   : initialized_(false),
18     library_path_(path),
19     xw_extension_(0),
20     use_trampoline_(true),
21     delegate_(delegate),
22     created_instance_callback_(NULL),
23     destroyed_instance_callback_(NULL),
24     shutdown_callback_(NULL),
25     handle_msg_callback_(NULL),
26     handle_sync_msg_callback_(NULL) {
27 }
28
29 Extension::~Extension() {
30   if (!initialized_)
31     return;
32
33   if (shutdown_callback_)
34     shutdown_callback_(xw_extension_);
35   ExtensionAdapter::GetInstance()->UnregisterExtension(this);
36 }
37
38 bool Extension::Initialize() {
39   if (initialized_)
40     return true;
41
42   void* handle = dlopen(library_path_.c_str(), RTLD_LAZY);
43   if (!handle) {
44     LOGGER(ERROR) << "Error loading extension '"
45                   << library_path_ << "' : " << dlerror();
46     return false;
47   }
48
49   XW_Initialize_Func initialize = reinterpret_cast<XW_Initialize_Func>(
50       dlsym(handle, "XW_Initialize"));
51   if (!initialize) {
52     LOGGER(ERROR) << "Error loading extension '" << library_path_
53                   << "' : couldn't get XW_Initialize function.";
54     dlclose(handle);
55     return false;
56   }
57
58   ExtensionAdapter* adapter = ExtensionAdapter::GetInstance();
59   xw_extension_ = adapter->GetNextXWExtension();
60   adapter->RegisterExtension(this);
61
62   int ret = initialize(xw_extension_, ExtensionAdapter::GetInterface);
63   if (ret != XW_OK) {
64     LOGGER(ERROR) << "Error loading extension '" << library_path_
65                   << "' : XW_Initialize() returned error value.";
66     dlclose(handle);
67     return false;
68   }
69
70   initialized_ = true;
71   return true;
72 }
73
74 ExtensionInstance* Extension::CreateInstance() {
75   ExtensionAdapter* adapter = ExtensionAdapter::GetInstance();
76   XW_Instance xw_instance = adapter->GetNextXWInstance();
77   return new ExtensionInstance(this, xw_instance);
78 }
79
80 void Extension::GetRuntimeVariable(const char* key, char* value,
81     size_t value_len) {
82   if (delegate_) {
83     delegate_->GetRuntimeVariable(key, value, value_len);
84   }
85 }
86 int Extension::CheckAPIAccessControl(const char* /*api_name*/) {
87   // Not Supported
88   return XW_OK;
89 }
90
91 int Extension::RegisterPermissions(const char* /*perm_table*/) {
92   // Not Supported
93   return XW_OK;
94 }
95
96 }  // namespace wrt
97