|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set sw=4 ts=8 et tw=80 : |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "ColorPickerParent.h" |
|
8 #include "nsComponentManagerUtils.h" |
|
9 #include "nsIDocument.h" |
|
10 #include "nsIDOMWindow.h" |
|
11 #include "mozilla/unused.h" |
|
12 #include "mozilla/dom/Element.h" |
|
13 #include "mozilla/dom/TabParent.h" |
|
14 |
|
15 using mozilla::unused; |
|
16 using namespace mozilla::dom; |
|
17 |
|
18 NS_IMPL_ISUPPORTS(ColorPickerParent::ColorPickerShownCallback, |
|
19 nsIColorPickerShownCallback); |
|
20 |
|
21 NS_IMETHODIMP |
|
22 ColorPickerParent::ColorPickerShownCallback::Update(const nsAString& aColor) |
|
23 { |
|
24 if (mColorPickerParent) { |
|
25 unused << mColorPickerParent->SendUpdate(nsString(aColor)); |
|
26 } |
|
27 return NS_OK; |
|
28 } |
|
29 |
|
30 NS_IMETHODIMP |
|
31 ColorPickerParent::ColorPickerShownCallback::Done(const nsAString& aColor) |
|
32 { |
|
33 if (mColorPickerParent) { |
|
34 unused << mColorPickerParent->Send__delete__(mColorPickerParent, |
|
35 nsString(aColor)); |
|
36 } |
|
37 return NS_OK; |
|
38 } |
|
39 |
|
40 void |
|
41 ColorPickerParent::ColorPickerShownCallback::Destroy() |
|
42 { |
|
43 mColorPickerParent = nullptr; |
|
44 } |
|
45 |
|
46 bool |
|
47 ColorPickerParent::CreateColorPicker() |
|
48 { |
|
49 mPicker = do_CreateInstance("@mozilla.org/colorpicker;1"); |
|
50 if (!mPicker) { |
|
51 return false; |
|
52 } |
|
53 |
|
54 Element* ownerElement = static_cast<TabParent*>(Manager())->GetOwnerElement(); |
|
55 if (!ownerElement) { |
|
56 return false; |
|
57 } |
|
58 |
|
59 nsCOMPtr<nsIDOMWindow> window = do_QueryInterface(ownerElement->OwnerDoc()->GetWindow()); |
|
60 if (!window) { |
|
61 return false; |
|
62 } |
|
63 |
|
64 return NS_SUCCEEDED(mPicker->Init(window, mTitle, mInitialColor)); |
|
65 } |
|
66 |
|
67 bool |
|
68 ColorPickerParent::RecvOpen() |
|
69 { |
|
70 if (!CreateColorPicker()) { |
|
71 unused << Send__delete__(this, mInitialColor); |
|
72 return true; |
|
73 } |
|
74 |
|
75 mCallback = new ColorPickerShownCallback(this); |
|
76 |
|
77 mPicker->Open(mCallback); |
|
78 return true; |
|
79 }; |
|
80 |
|
81 void |
|
82 ColorPickerParent::ActorDestroy(ActorDestroyReason aWhy) |
|
83 { |
|
84 if (mCallback) { |
|
85 mCallback->Destroy(); |
|
86 } |
|
87 } |