Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 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 0 : NS_IMPL_ISUPPORTS(ColorPickerParent::ColorPickerShownCallback,
19 : nsIColorPickerShownCallback);
20 :
21 : NS_IMETHODIMP
22 0 : ColorPickerParent::ColorPickerShownCallback::Update(const nsAString& aColor)
23 : {
24 0 : if (mColorPickerParent) {
25 0 : Unused << mColorPickerParent->SendUpdate(nsString(aColor));
26 : }
27 0 : return NS_OK;
28 : }
29 :
30 : NS_IMETHODIMP
31 0 : ColorPickerParent::ColorPickerShownCallback::Done(const nsAString& aColor)
32 : {
33 0 : if (mColorPickerParent) {
34 0 : Unused << mColorPickerParent->Send__delete__(mColorPickerParent,
35 0 : nsString(aColor));
36 : }
37 0 : return NS_OK;
38 : }
39 :
40 : void
41 0 : ColorPickerParent::ColorPickerShownCallback::Destroy()
42 : {
43 0 : mColorPickerParent = nullptr;
44 0 : }
45 :
46 : bool
47 0 : ColorPickerParent::CreateColorPicker()
48 : {
49 0 : mPicker = do_CreateInstance("@mozilla.org/colorpicker;1");
50 0 : if (!mPicker) {
51 0 : return false;
52 : }
53 :
54 0 : Element* ownerElement = TabParent::GetFrom(Manager())->GetOwnerElement();
55 0 : if (!ownerElement) {
56 0 : return false;
57 : }
58 :
59 0 : nsCOMPtr<nsPIDOMWindowOuter> window = ownerElement->OwnerDoc()->GetWindow();
60 0 : if (!window) {
61 0 : return false;
62 : }
63 :
64 0 : return NS_SUCCEEDED(mPicker->Init(window, mTitle, mInitialColor));
65 : }
66 :
67 : mozilla::ipc::IPCResult
68 0 : ColorPickerParent::RecvOpen()
69 : {
70 0 : if (!CreateColorPicker()) {
71 0 : Unused << Send__delete__(this, mInitialColor);
72 0 : return IPC_OK();
73 : }
74 :
75 0 : mCallback = new ColorPickerShownCallback(this);
76 :
77 0 : mPicker->Open(mCallback);
78 0 : return IPC_OK();
79 : };
80 :
81 : void
82 0 : ColorPickerParent::ActorDestroy(ActorDestroyReason aWhy)
83 : {
84 0 : if (mCallback) {
85 0 : mCallback->Destroy();
86 : }
87 0 : }
|