color_chooser_win.cc revision f2477e01787aa58f445919b809d89e252beef54f
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.
4
5#include <windows.h>
6
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 "content/public/browser/web_contents_view.h"
16#include "ui/views/color_chooser/color_chooser_listener.h"
17
18#if defined(USE_ASH)
19#include "chrome/browser/ui/views/color_chooser_aura.h"
20#endif
21
22class ColorChooserWin : public content::ColorChooser,
23                        public views::ColorChooserListener {
24 public:
25  static ColorChooserWin* Open(content::WebContents* web_contents,
26                               SkColor initial_color);
27
28  ColorChooserWin(content::WebContents* web_contents,
29                  SkColor initial_color);
30  ~ColorChooserWin();
31
32  // content::ColorChooser overrides:
33  virtual void End() OVERRIDE;
34  virtual void SetSelectedColor(SkColor color) OVERRIDE {}
35
36  // views::ColorChooserListener overrides:
37  virtual void OnColorChosen(SkColor color);
38  virtual void OnColorChooserDialogClosed();
39
40 private:
41  static ColorChooserWin* current_color_chooser_;
42
43  // The web contents invoking the color chooser.  No ownership. because it will
44  // outlive this class.
45  content::WebContents* web_contents_;
46
47  // The color chooser dialog which maintains the native color chooser UI.
48  scoped_refptr<ColorChooserDialog> color_chooser_dialog_;
49};
50
51ColorChooserWin* ColorChooserWin::current_color_chooser_ = NULL;
52
53ColorChooserWin* ColorChooserWin::Open(content::WebContents* web_contents,
54                                       SkColor initial_color) {
55  if (!current_color_chooser_)
56    current_color_chooser_ = new ColorChooserWin(web_contents, initial_color);
57  return current_color_chooser_;
58}
59
60ColorChooserWin::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,
66                                                 initial_color,
67                                                 owning_window);
68}
69
70ColorChooserWin::~ColorChooserWin() {
71  // Always call End() before destroying.
72  DCHECK(!color_chooser_dialog_);
73}
74
75void 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();
83}
84
85void ColorChooserWin::OnColorChosen(SkColor color) {
86  if (web_contents_)
87    web_contents_->DidChooseColorInColorChooser(color);
88}
89
90void ColorChooserWin::OnColorChooserDialogClosed() {
91  if (color_chooser_dialog_.get()) {
92    color_chooser_dialog_->ListenerDestroyed();
93    color_chooser_dialog_ = NULL;
94  }
95  DCHECK(current_color_chooser_ == this);
96  current_color_chooser_ = NULL;
97  if (web_contents_)
98    web_contents_->DidEndColorChooser();
99}
100
101namespace chrome {
102
103content::ColorChooser* ShowColorChooser(content::WebContents* web_contents,
104                                        SkColor initial_color) {
105#if defined(USE_ASH)
106  gfx::NativeView native_view = web_contents->GetView()->GetNativeView();
107  if (GetHostDesktopTypeForNativeView(native_view) == HOST_DESKTOP_TYPE_ASH)
108    return ColorChooserAura::Open(web_contents, initial_color);
109#endif
110  return ColorChooserWin::Open(web_contents, initial_color);
111}
112
113}  // namespace chrome
114