1 // Copyright (c) 2012 The Chromium Authors. 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.
7 #include "chrome/browser/platform_util.h"
8 #include "chrome/browser/ui/browser_dialogs.h"
9 #include "chrome/browser/ui/host_desktop.h"
10 #include "chrome/browser/ui/views/color_chooser_dialog.h"
11 #include "content/public/browser/color_chooser.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "content/public/browser/web_contents.h"
15 #include "ui/views/color_chooser/color_chooser_listener.h"
18 #include "chrome/browser/ui/views/color_chooser_aura.h"
21 class ColorChooserWin : public content::ColorChooser,
22 public views::ColorChooserListener {
24 static ColorChooserWin* Open(content::WebContents* web_contents,
25 SkColor initial_color);
27 ColorChooserWin(content::WebContents* web_contents,
28 SkColor initial_color);
31 // content::ColorChooser overrides:
32 virtual void End() OVERRIDE;
33 virtual void SetSelectedColor(SkColor color) OVERRIDE {}
35 // views::ColorChooserListener overrides:
36 virtual void OnColorChosen(SkColor color);
37 virtual void OnColorChooserDialogClosed();
40 static ColorChooserWin* current_color_chooser_;
42 // The web contents invoking the color chooser. No ownership. because it will
43 // outlive this class.
44 content::WebContents* web_contents_;
46 // The color chooser dialog which maintains the native color chooser UI.
47 scoped_refptr<ColorChooserDialog> color_chooser_dialog_;
50 ColorChooserWin* ColorChooserWin::current_color_chooser_ = NULL;
52 ColorChooserWin* ColorChooserWin::Open(content::WebContents* web_contents,
53 SkColor initial_color) {
54 if (current_color_chooser_)
56 current_color_chooser_ = new ColorChooserWin(web_contents, initial_color);
57 return current_color_chooser_;
60 ColorChooserWin::ColorChooserWin(content::WebContents* web_contents,
61 SkColor initial_color)
62 : web_contents_(web_contents) {
63 gfx::NativeWindow owning_window = platform_util::GetTopLevel(
64 web_contents->GetRenderViewHost()->GetView()->GetNativeView());
65 color_chooser_dialog_ = new ColorChooserDialog(this,
70 ColorChooserWin::~ColorChooserWin() {
71 // Always call End() before destroying.
72 DCHECK(!color_chooser_dialog_);
75 void ColorChooserWin::End() {
76 // The ColorChooserDialog's listener is going away. Ideally we'd
77 // programmatically close the dialog at this point. Since that's impossible,
78 // we instead tell the dialog its listener is going away, so that the dialog
79 // doesn't try to communicate with a destroyed listener later. (We also tell
80 // the renderer the dialog is closed, since from the renderer's perspective
81 // it effectively is.)
82 OnColorChooserDialogClosed();
85 void ColorChooserWin::OnColorChosen(SkColor color) {
87 web_contents_->DidChooseColorInColorChooser(color);
90 void ColorChooserWin::OnColorChooserDialogClosed() {
91 if (color_chooser_dialog_.get()) {
92 color_chooser_dialog_->ListenerDestroyed();
93 color_chooser_dialog_ = NULL;
95 DCHECK(current_color_chooser_ == this);
96 current_color_chooser_ = NULL;
98 web_contents_->DidEndColorChooser();
103 content::ColorChooser* ShowColorChooser(content::WebContents* web_contents,
104 SkColor initial_color) {
106 gfx::NativeView native_view = web_contents->GetNativeView();
107 if (GetHostDesktopTypeForNativeView(native_view) == HOST_DESKTOP_TYPE_ASH)
108 return ColorChooserAura::Open(web_contents, initial_color);
110 return ColorChooserWin::Open(web_contents, initial_color);
113 } // namespace chrome