Initial Implementation of Node prelaunch
[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 #include <dlog.h>
11
12 namespace atom {
13
14 namespace api {
15
16 PWRT::PWRT(v8::Isolate* isolate) {
17   LOG(ERROR) << "PWRT::PWRT";
18   Init(isolate);
19 }
20
21 PWRT::~PWRT() {
22   LOG(ERROR) << "PWRT::~PWRT";
23 }
24
25 std::string PWRT::GetMessage() {
26   LOG(ERROR) << "PWRT::GetMessage";
27   return "message from C++";
28 }
29
30 std::string PWRT::GetPath() {
31   LOG(ERROR) << "PWRT::GetPath";
32   common::CommandLine* runtime_cmd = common::CommandLine::ForCurrentProcess();
33   if (runtime_cmd) {
34     std::string appid = runtime_cmd->GetAppIdFromCommandLine("/usr/bin/electron");
35     auto appdata_manager = common::ApplicationDataManager::GetInstance();
36     common::ApplicationData* app_data = appdata_manager->GetApplicationData(appid);
37     // TODO: Use resource-manager's GetStartResource() for localized urls
38     std::string app_path = "file://" + app_data->application_path() + app_data->content_info()->src();
39     return app_path;
40   }
41   return "";
42 }
43
44 bool PWRT::isTizenWebApp() {
45   LOG(ERROR) << "PWRT::isTizenWebApp";
46   common::CommandLine* runtime_cmd = common::CommandLine::ForCurrentProcess();
47   std::string appid = runtime_cmd->GetAppIdFromCommandLine("/usr/bin/electron");
48   if (appid != "electron") { // TODO: Any better distinguishing feature?
49     return true;
50   } else {
51     return false;
52   }
53 }
54
55 void PWRT::Log(const std::string& message) {
56   std::string output = "[JS LOG] " + message;
57   dlog_print(DLOG_INFO, "WRT", output.c_str());
58 }
59
60 // static
61 mate::Handle<PWRT> PWRT::Create(v8::Isolate* isolate) {
62   LOG(ERROR) << "PWRT::Create";
63   return mate::CreateHandle(isolate, new PWRT(isolate));
64 }
65
66 // static
67 void PWRT::BuildPrototype(
68     v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
69   LOG(ERROR) << "PWRT::BuildPrototype";
70   prototype->SetClassName(mate::StringToV8(isolate, "PWRT"));
71   // TODO: Needs adding necessary interface methods
72   mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
73     .SetMethod("getMessage", &PWRT::GetMessage)
74     .SetMethod("getPath", &PWRT::GetPath)
75     .SetMethod("isTizenWebApp", &PWRT::isTizenWebApp)
76     .SetMethod("log", &PWRT::Log);
77 }
78
79 }  // namespace api
80
81 }  // namespace atom
82
83
84 namespace {
85
86 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
87                 v8::Local<v8::Context> context, void* priv) {
88   LOG(ERROR) << "PWRT::Initialize";
89   v8::Isolate* isolate = context->GetIsolate();
90   mate::Dictionary dict(isolate, exports);
91   // TODO: Expose this attribute only for Tizen web apps
92   dict.Set("pwrt", atom::api::PWRT::Create(isolate));
93 }
94
95 }  // namespace
96
97 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_pwrt, Initialize)