Add a new api - PWRT::GetPath()
[platform/framework/web/crosswalk-tizen.git] / atom / browser / api / atom_api_pwrt.cc
1 #include "atom/browser/api/atom_api_pwrt.h"
2
3 #include "atom/browser/browser.h"
4 #include "native_mate/dictionary.h"
5 #include "base/logging.h"
6
7 #include "atom/common/node_includes.h"
8 #include "tizen/common/application_data.h"
9 #include "tizen/common/command_line.h"
10
11 namespace atom {
12
13 namespace api {
14
15 PWRT::PWRT(v8::Isolate* isolate) {
16   LOG(INFO) << "PWRT::PWRT";
17   Init(isolate);
18 }
19
20 PWRT::~PWRT() {
21   LOG(INFO) << "PWRT::~PWRT";
22 }
23
24 std::string PWRT::GetMessage() {
25   LOG(INFO) << "PWRT::GetMessage";
26   return "message from C++";
27 }
28
29 std::string PWRT::GetPath() {
30   LOG(INFO) << "PWRT::GetPath";
31   common::CommandLine* runtime_cmd = common::CommandLine::ForCurrentProcess();
32   std::string appid = runtime_cmd->GetAppIdFromCommandLine("/usr/bin/electron");
33   auto appdata_manager = common::ApplicationDataManager::GetInstance();
34   common::ApplicationData* app_data = appdata_manager->GetApplicationData(appid);
35   std::string app_path = "file://" + app_data->application_path() + "index.html";
36   return app_path;
37 }
38
39 // static
40 mate::Handle<PWRT> PWRT::Create(v8::Isolate* isolate) {
41   LOG(INFO) << "PWRT::Create";
42   return mate::CreateHandle(isolate, new PWRT(isolate));
43 }
44
45 // static
46 void PWRT::BuildPrototype(
47     v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
48   LOG(INFO) << "PWRT::BuildPrototype";
49   prototype->SetClassName(mate::StringToV8(isolate, "PWRT"));
50   // TODO: Needs adding necessary interface methods
51   mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
52     .SetMethod("getMessage", &PWRT::GetMessage)
53     .SetMethod("getPath", &PWRT::GetPath);
54 }
55
56 }  // namespace api
57
58 }  // namespace atom
59
60
61 namespace {
62
63 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
64                 v8::Local<v8::Context> context, void* priv) {
65   LOG(INFO) << "PWRT::Initialize";
66   v8::Isolate* isolate = context->GetIsolate();
67   mate::Dictionary dict(isolate, exports);
68   // TODO: Expose this attribute only for Tizen web apps
69   dict.Set("pwrt", atom::api::PWRT::Create(isolate));
70 }
71
72 }  // namespace
73
74 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_pwrt, Initialize)