Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / screen_orientation / screen_orientation_dispatcher_host.cc
index 67174d0..310b54b 100644 (file)
@@ -7,18 +7,37 @@
 #include "content/browser/screen_orientation/screen_orientation_provider.h"
 #include "content/common/screen_orientation_messages.h"
 #include "content/public/browser/render_frame_host.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/render_view_host.h"
+#include "content/public/browser/render_widget_host.h"
 #include "content/public/browser/web_contents.h"
+#include "third_party/WebKit/public/platform/WebScreenInfo.h"
 
 namespace content {
 
+ScreenOrientationDispatcherHost::LockInformation::LockInformation(
+    int request_id, int process_id, int routing_id)
+    : request_id(request_id),
+      process_id(process_id),
+      routing_id(routing_id) {
+}
+
 ScreenOrientationDispatcherHost::ScreenOrientationDispatcherHost(
     WebContents* web_contents)
-    : WebContentsObserver(web_contents) {
-  if (!provider_.get())
-    provider_.reset(CreateProvider());
+  : WebContentsObserver(web_contents),
+    current_lock_(NULL) {
+  provider_.reset(ScreenOrientationProvider::Create(this, web_contents));
 }
 
 ScreenOrientationDispatcherHost::~ScreenOrientationDispatcherHost() {
+  ResetCurrentLock();
+}
+
+void ScreenOrientationDispatcherHost::ResetCurrentLock() {
+  if (current_lock_) {
+    delete current_lock_;
+    current_lock_ = 0;
+  }
 }
 
 bool ScreenOrientationDispatcherHost::OnMessageReceived(
@@ -36,12 +55,52 @@ bool ScreenOrientationDispatcherHost::OnMessageReceived(
   return handled;
 }
 
-void ScreenOrientationDispatcherHost::OnOrientationChange(
-    blink::WebScreenOrientationType orientation) {
-  Send(new ScreenOrientationMsg_OrientationChange(orientation));
+RenderFrameHost*
+ScreenOrientationDispatcherHost::GetRenderFrameHostForRequestID(
+    int request_id) {
+  if (!current_lock_ || current_lock_->request_id != request_id)
+    return NULL;
+
+  return RenderFrameHost::FromID(current_lock_->process_id,
+                                 current_lock_->routing_id);
 }
 
-void ScreenOrientationDispatcherHost::SetProviderForTests(
+void ScreenOrientationDispatcherHost::NotifyLockSuccess(int request_id) {
+  RenderFrameHost* render_frame_host =
+      GetRenderFrameHostForRequestID(request_id);
+  if (!render_frame_host)
+    return;
+
+  render_frame_host->Send(new ScreenOrientationMsg_LockSuccess(
+      render_frame_host->GetRoutingID(), request_id));
+  ResetCurrentLock();
+}
+
+void ScreenOrientationDispatcherHost::NotifyLockError(
+    int request_id, blink::WebLockOrientationError error) {
+  RenderFrameHost* render_frame_host =
+      GetRenderFrameHostForRequestID(request_id);
+  if (!render_frame_host)
+    return;
+
+  NotifyLockError(request_id, render_frame_host, error);
+}
+
+void ScreenOrientationDispatcherHost::NotifyLockError(
+    int request_id,
+    RenderFrameHost* render_frame_host,
+    blink::WebLockOrientationError error) {
+  render_frame_host->Send(new ScreenOrientationMsg_LockError(
+      render_frame_host->GetRoutingID(), request_id, error));
+  ResetCurrentLock();
+}
+
+void ScreenOrientationDispatcherHost::OnOrientationChange() {
+  if (provider_)
+    provider_->OnOrientationChange();
+}
+
+void ScreenOrientationDispatcherHost::SetProvider(
     ScreenOrientationProvider* provider) {
   provider_.reset(provider);
 }
@@ -50,36 +109,35 @@ void ScreenOrientationDispatcherHost::OnLockRequest(
     RenderFrameHost* render_frame_host,
     blink::WebScreenOrientationLockType orientation,
     int request_id) {
+  if (current_lock_) {
+    NotifyLockError(current_lock_->request_id, render_frame_host,
+                    blink::WebLockOrientationErrorCanceled);
+  }
+
   if (!provider_) {
-    render_frame_host->Send(new ScreenOrientationMsg_LockError(
-        render_frame_host->GetRoutingID(),
-        request_id,
-        blink::WebLockOrientationCallback::ErrorTypeNotAvailable));
+    NotifyLockError(request_id, render_frame_host,
+                    blink::WebLockOrientationErrorNotAvailable);
     return;
   }
 
-  // TODO(mlamouri): pass real values.
-  render_frame_host->Send(new ScreenOrientationMsg_LockSuccess(
-      render_frame_host->GetRoutingID(),
-      request_id,
-      0,
-      blink::WebScreenOrientationPortraitPrimary));
-  provider_->LockOrientation(orientation);
+  current_lock_ = new LockInformation(request_id,
+                                      render_frame_host->GetProcess()->GetID(),
+                                      render_frame_host->GetRoutingID());
+
+  provider_->LockOrientation(request_id, orientation);
 }
 
 void ScreenOrientationDispatcherHost::OnUnlockRequest(
     RenderFrameHost* render_frame_host) {
-  if (!provider_.get())
+  if (current_lock_) {
+    NotifyLockError(current_lock_->request_id, render_frame_host,
+                    blink::WebLockOrientationErrorCanceled);
+  }
+
+  if (!provider_)
     return;
 
   provider_->UnlockOrientation();
 }
 
-#if !defined(OS_ANDROID)
-// static
-ScreenOrientationProvider* ScreenOrientationDispatcherHost::CreateProvider() {
-  return NULL;
-}
-#endif
-
 }  // namespace content