Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / ui / splash_screen_tizen.cc
1 // Copyright (c) 2014 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/browser/ui/splash_screen_tizen.h"
6
7 #include "base/location.h"
8 #include "ui/compositor/layer.h"
9 #include "ui/compositor/scoped_layer_animation_settings.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/views/widget/widget.h"
13 #include "xwalk/runtime/browser/image_util.h"
14
15 namespace xwalk {
16
17 namespace {
18 const int kHideAnimationDuration = 1;  // second
19 }  // namespace
20
21 class SplashScreenTizen::SplashScreenLayerDelegate : public ui::LayerDelegate {
22  public:
23   SplashScreenLayerDelegate() {}
24
25   virtual ~SplashScreenLayerDelegate() {}
26
27   void set_image(const gfx::Image& image) { image_ = image; }
28   const gfx::Image& image() const { return image_; }
29
30   virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
31     if (!image_.IsEmpty()) {
32       canvas->DrawImageInt(image_.AsImageSkia(), 0, 0);
33     } else {
34       LOG(WARNING) << "The splash screen image is not loaded.";
35     }
36   }
37
38   virtual void OnDelegatedFrameDamage(
39       const gfx::Rect& damage_rect_in_dip) OVERRIDE {}
40
41   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
42
43   virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
44     return base::Closure();
45   }
46
47  private:
48   gfx::Image image_;
49
50   DISALLOW_COPY_AND_ASSIGN(SplashScreenLayerDelegate);
51 };
52
53 SplashScreenTizen::SplashScreenTizen(views::Widget* host,
54                                      const base::FilePath& file,
55                                      content::WebContents* web_contents)
56     : content::WebContentsObserver(web_contents),
57       widget_host_(host),
58       splash_screen_image_(file),
59       layer_(new ui::Layer(ui::LAYER_TEXTURED)),
60       layer_delegate_(new SplashScreenLayerDelegate()),
61       is_started(false) {
62   DCHECK(widget_host_);
63   layer_->set_delegate(layer_delegate_.get());
64 }
65
66 SplashScreenTizen::~SplashScreenTizen() {}
67
68 void SplashScreenTizen::Start() {
69   DCHECK(widget_host_);
70   if (is_started)
71     return;
72
73   is_started = true;
74   gfx::Image image = xwalk_utils::LoadImageFromFilePath(splash_screen_image_);
75   if (!image.IsEmpty()) {
76     layer_delegate_->set_image(image);
77     ui::Layer* top_layer = widget_host_->GetLayer();
78     gfx::Rect rc = gfx::Rect(widget_host_->GetWindowBoundsInScreen());
79     // The bound of current layer locating at the host window.
80     gfx::Rect layer_bound((rc.width() - image.Width()) / 2,
81         (rc.height() - image.Height()) / 2, image.Width(), image.Height());
82     layer_->SetBounds(layer_bound);
83     top_layer->Add(layer_.get());
84     top_layer->StackAtTop(layer_.get());
85   }
86 }
87
88 void SplashScreenTizen::Stop() {
89   DCHECK(widget_host_);
90   if (!is_started)
91     return;
92
93   is_started = false;
94   ui::ScopedLayerAnimationSettings settings(layer_->GetAnimator());
95   settings.SetTransitionDuration(base::TimeDelta::FromSeconds(
96       kHideAnimationDuration));
97   settings.SetPreemptionStrategy(ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
98   settings.AddObserver(this);
99   layer_->SetOpacity(0.0f);
100 }
101
102 void SplashScreenTizen::DidFinishLoad(
103     content::RenderFrameHost* render_frame_host,
104     const GURL& validated_url) {
105   Stop();
106 }
107
108 void SplashScreenTizen::DidFailLoad(
109     content::RenderFrameHost* render_frame_host,
110     const GURL& validated_url,
111     int error_code,
112     const base::string16& error_description) {
113   Stop();
114 }
115
116 void SplashScreenTizen::OnImplicitAnimationsCompleted() {
117   DCHECK(widget_host_);
118   widget_host_->GetLayer()->Remove(layer_.get());
119 }
120
121 }  // namespace xwalk