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.
5 #include "xwalk/runtime/extension/screen_orientation_extension.h"
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"
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"
21 using content::BrowserThread;
25 using application::Application;
29 MultiOrientationScreen* GetMultiOrientationScreen(Application*) { // NOLINT
36 ScreenOrientationExtension::ScreenOrientationExtension(
37 Application* app, OrientationMask ua_default)
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");
47 set_name("xwalk.screen");
48 set_entry_points(entry_points);
50 std::ostringstream js_source;
51 js_source << "var uaDefault = ";
52 js_source << ua_default;
54 js_source << ResourceBundle::GetSharedInstance().GetRawDataResource(
55 IDR_XWALK_SCREEN_ORIENTATION_API).as_string();
57 set_javascript_api(js_source.str());
60 ScreenOrientationExtension::~ScreenOrientationExtension() {
63 XWalkExtensionInstance* ScreenOrientationExtension::CreateInstance() {
64 return new ScreenOrientationInstance(application_);
67 ScreenOrientationInstance::ScreenOrientationInstance(Application* app)
69 , screen_(GetMultiOrientationScreen(app))
71 screen_->SetObserver(this);
73 handler_.Register("lock",
74 base::Bind(&ScreenOrientationInstance::OnAllowedOrientationsChanged,
75 base::Unretained(this)));
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()));
85 ScreenOrientationInstance::~ScreenOrientationInstance() {
88 void ScreenOrientationInstance::OnOrientationChanged(Orientation orientation) {
89 PostMessageToJS(scoped_ptr<base::Value>(
90 base::Value::CreateIntegerValue(orientation)));
93 void ScreenOrientationInstance::HandleMessage(
94 scoped_ptr<base::Value> msg) {
95 handler_.HandleMessage(msg.Pass());
98 void ScreenOrientationInstance::HandleSyncMessage(
99 scoped_ptr<base::Value> msg) {
101 msg->GetAsString(&message);
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();
110 SendSyncReplyToJS(scoped_ptr<base::Value>(
111 base::Value::CreateIntegerValue(screen_->GetCurrentOrientation())));
114 void ScreenOrientationInstance::OnAllowedOrientationsChanged(
115 scoped_ptr<XWalkExtensionFunctionInfo> info) {
117 if (!info->arguments()->GetInteger(0, &value)) {
118 LOG(WARNING) << "Malformed message passed to " << info->name();
122 OrientationMask orientations = static_cast<OrientationMask>(value);
123 screen_->OnAllowedOrientationsChanged(orientations);