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