ColorPickerParent.cpp 2.0 KB

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