color_chooser_win.cc revision 010d83a9304c5a91596085d917d248abff47903a
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 "ui/views/color_chooser/color_chooser_listener.h" 16 17#if defined(USE_ASH) 18#include "chrome/browser/ui/views/color_chooser_aura.h" 19#endif 20 21class ColorChooserWin : public content::ColorChooser, 22 public views::ColorChooserListener { 23 public: 24 static ColorChooserWin* Open(content::WebContents* web_contents, 25 SkColor initial_color); 26 27 ColorChooserWin(content::WebContents* web_contents, 28 SkColor initial_color); 29 ~ColorChooserWin(); 30 31 // content::ColorChooser overrides: 32 virtual void End() OVERRIDE; 33 virtual void SetSelectedColor(SkColor color) OVERRIDE {} 34 35 // views::ColorChooserListener overrides: 36 virtual void OnColorChosen(SkColor color); 37 virtual void OnColorChooserDialogClosed(); 38 39 private: 40 static ColorChooserWin* current_color_chooser_; 41 42 // The web contents invoking the color chooser. No ownership. because it will 43 // outlive this class. 44 content::WebContents* web_contents_; 45 46 // The color chooser dialog which maintains the native color chooser UI. 47 scoped_refptr<ColorChooserDialog> color_chooser_dialog_; 48}; 49 50ColorChooserWin* ColorChooserWin::current_color_chooser_ = NULL; 51 52ColorChooserWin* ColorChooserWin::Open(content::WebContents* web_contents, 53 SkColor initial_color) { 54 if (current_color_chooser_) 55 return NULL; 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->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