Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / extension / screen_orientation_extension.cc
1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/runtime/extension/screen_orientation_extension.h"
6
7 #include <set>
8 #include <string>
9 #include <sstream>
10 #include <vector>
11
12 #include "content/public/browser/browser_thread.h"
13 #include "grit/xwalk_resources.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "third_party/jsoncpp/source/include/json/json.h"
16
17 #include "xwalk/application/browser/application.h"
18 #include "xwalk/runtime/browser/runtime.h"
19 #include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
20
21 using content::BrowserThread;
22
23 namespace xwalk {
24
25 using application::Application;
26
27 namespace {
28
29 MultiOrientationScreen* GetMultiOrientationScreen(Application*) {  // NOLINT
30   NOTREACHED();
31   return NULL;
32 }
33
34 }  // namespace.
35
36 ScreenOrientationExtension::ScreenOrientationExtension(
37     Application* app, OrientationMask ua_default)
38   : application_(app) {
39   DCHECK(application_);
40
41   std::vector<std::string> entry_points;
42   entry_points.push_back("screen.orientation");
43   entry_points.push_back("screen.lockOrientation");
44   entry_points.push_back("screen.unlockOrientation");
45   entry_points.push_back("screen.onorientationchange");
46
47   set_name("xwalk.screen");
48   set_entry_points(entry_points);
49
50   std::ostringstream js_source;
51   js_source << "var uaDefault = ";
52   js_source << ua_default;
53   js_source << ";\n";
54   js_source << ResourceBundle::GetSharedInstance().GetRawDataResource(
55       IDR_XWALK_SCREEN_ORIENTATION_API).as_string();
56
57   set_javascript_api(js_source.str());
58 }
59
60 ScreenOrientationExtension::~ScreenOrientationExtension() {
61 }
62
63 XWalkExtensionInstance* ScreenOrientationExtension::CreateInstance() {
64   return new ScreenOrientationInstance(application_);
65 }
66
67 ScreenOrientationInstance::ScreenOrientationInstance(Application* app)
68   : handler_(this)
69   , screen_(GetMultiOrientationScreen(app))
70   , application_(app) {
71   screen_->SetObserver(this);
72
73   handler_.Register("lock",
74       base::Bind(&ScreenOrientationInstance::OnAllowedOrientationsChanged,
75       base::Unretained(this)));
76
77   // If the orientation property is not queried immediately after loading
78   // we can avoid querying it synchronously.
79   // FIXME: Make sure the .orientation property is 100% correct when queried.
80   content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
81     base::Bind(&ScreenOrientationInstance::OnOrientationChanged,
82         base::Unretained(this), screen_->GetCurrentOrientation()));
83 }
84
85 ScreenOrientationInstance::~ScreenOrientationInstance() {
86 }
87
88 void ScreenOrientationInstance::OnOrientationChanged(Orientation orientation) {
89   PostMessageToJS(scoped_ptr<base::Value>(
90       base::Value::CreateIntegerValue(orientation)));
91 }
92
93 void ScreenOrientationInstance::HandleMessage(
94     scoped_ptr<base::Value> msg) {
95   handler_.HandleMessage(msg.Pass());
96 }
97
98 void ScreenOrientationInstance::HandleSyncMessage(
99     scoped_ptr<base::Value> msg) {
100   std::string message;
101   msg->GetAsString(&message);
102
103   Json::Value input;
104   Json::Reader reader;
105   if (!reader.parse(message, input))
106     LOG(ERROR) << "Failed to parse message: " << message;
107   else if (input["cmd"].asString() != "GetScreenOrientation")
108     LOG(ERROR) << "Got unknown command: " << input["cmd"].asString();
109
110   SendSyncReplyToJS(scoped_ptr<base::Value>(
111       base::Value::CreateIntegerValue(screen_->GetCurrentOrientation())));
112 }
113
114 void ScreenOrientationInstance::OnAllowedOrientationsChanged(
115     scoped_ptr<XWalkExtensionFunctionInfo> info) {
116   int value;
117   if (!info->arguments()->GetInteger(0, &value)) {
118     LOG(WARNING) << "Malformed message passed to " << info->name();
119     return;
120   }
121
122   OrientationMask orientations = static_cast<OrientationMask>(value);
123   screen_->OnAllowedOrientationsChanged(orientations);
124 }
125
126 }  // namespace xwalk