10b0dd7cc41859cd372d05ce6396ea74f7c1c6d6
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / ui / color_chooser_aura.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "xwalk/runtime/browser/ui/color_chooser.h"
7
8 #include "base/message_loop/message_loop.h"
9 #include "base/threading/thread.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/color_chooser.h"
12 #include "content/public/browser/web_contents.h"
13 #include "ui/views/color_chooser/color_chooser_listener.h"
14 #include "ui/views/color_chooser/color_chooser_view.h"
15 #include "ui/views/widget/widget.h"
16
17
18 class ColorChooserAura : public xwalk::ColorChooser,
19                          public views::ColorChooserListener {
20  public:
21   static ColorChooserAura* Open(content::WebContents* web_contents,
22                                 SkColor initial_color);
23
24   ColorChooserAura(content::WebContents* web_contents, SkColor initial_color);
25
26  private:
27   static ColorChooserAura* current_color_chooser_;
28
29   // content::ColorChooser overrides:
30   virtual void End() OVERRIDE;
31   virtual void SetSelectedColor(SkColor color) OVERRIDE;
32
33   // views::ColorChooserListener overrides:
34   virtual void OnColorChosen(SkColor color) OVERRIDE;
35   virtual void OnColorChooserDialogClosed() OVERRIDE;
36
37   void DidEndColorChooser();
38
39   // The actual view of the color chooser.
40   // Ownership handled by parent.
41   views::ColorChooserView* view_;
42
43   // The widget for the color chooser.
44   // Releases automatically when closed.
45   views::Widget* widget_;
46
47   // The web contents invoking the color chooser.
48   // Outlives this class
49   content::WebContents* web_contents_;
50
51   DISALLOW_COPY_AND_ASSIGN(ColorChooserAura);
52 };
53
54 ColorChooserAura* ColorChooserAura::current_color_chooser_ = NULL;
55
56 ColorChooserAura::ColorChooserAura(content::WebContents* web_contents,
57                                    SkColor initial_color)
58     : web_contents_(web_contents) {
59   view_ = new views::ColorChooserView(this, initial_color);
60   widget_ = views::Widget::CreateWindowWithContext(
61       view_, web_contents->GetNativeView());
62   widget_->SetAlwaysOnTop(true);
63   widget_->Show();
64   if (IsTesting()) {
65     content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
66         base::Bind(&ColorChooserAura::SetSelectedColor,
67                    base::Unretained(this),
68                    GetColorForBrowserTest()));
69   }
70 }
71
72 void ColorChooserAura::OnColorChosen(SkColor color) {
73   if (web_contents_)
74     web_contents_->DidChooseColorInColorChooser(color);
75   if (IsTesting()) {
76     content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
77         base::Bind(&ColorChooserAura::End, base::Unretained(this)));
78   }
79 }
80
81 void ColorChooserAura::OnColorChooserDialogClosed() {
82   view_ = NULL;
83   widget_ = NULL;
84   DidEndColorChooser();
85 }
86
87 void ColorChooserAura::End() {
88   if (widget_ && widget_->IsVisible()) {
89     view_->set_listener(NULL);
90     widget_->Close();
91     view_ = NULL;
92     widget_ = NULL;
93     DidEndColorChooser();
94   }
95 }
96
97 void ColorChooserAura::DidEndColorChooser() {
98   DCHECK(current_color_chooser_ == this);
99   current_color_chooser_ = NULL;
100   if (web_contents_)
101     web_contents_->DidEndColorChooser();
102 }
103
104 void ColorChooserAura::SetSelectedColor(SkColor color) {
105   if (view_) {
106     view_->OnColorChanged(color);
107     view_->OnSaturationValueChosen(view_->saturation(), view_->value());
108   }
109 }
110
111 // static
112 ColorChooserAura* ColorChooserAura::Open(
113     content::WebContents* web_contents, SkColor initial_color) {
114   if (current_color_chooser_)
115     current_color_chooser_->End();
116   DCHECK(!current_color_chooser_);
117   current_color_chooser_ = new ColorChooserAura(web_contents, initial_color);
118   return current_color_chooser_;
119 }
120
121 namespace xwalk {
122
123 content::ColorChooser* ShowColorChooser(content::WebContents* web_contents,
124                                         SkColor initial_color) {
125   return ColorChooserAura::Open(web_contents, initial_color);
126 }
127
128 }  // namespace xwalk