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