dom/ipc/ColorPickerParent.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/ipc/ColorPickerParent.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set sw=4 ts=8 et tw=80 :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "ColorPickerParent.h"
    1.11 +#include "nsComponentManagerUtils.h"
    1.12 +#include "nsIDocument.h"
    1.13 +#include "nsIDOMWindow.h"
    1.14 +#include "mozilla/unused.h"
    1.15 +#include "mozilla/dom/Element.h"
    1.16 +#include "mozilla/dom/TabParent.h"
    1.17 +
    1.18 +using mozilla::unused;
    1.19 +using namespace mozilla::dom;
    1.20 +
    1.21 +NS_IMPL_ISUPPORTS(ColorPickerParent::ColorPickerShownCallback,
    1.22 +                  nsIColorPickerShownCallback);
    1.23 +
    1.24 +NS_IMETHODIMP
    1.25 +ColorPickerParent::ColorPickerShownCallback::Update(const nsAString& aColor)
    1.26 +{
    1.27 +  if (mColorPickerParent) {
    1.28 +    unused << mColorPickerParent->SendUpdate(nsString(aColor));
    1.29 +  }
    1.30 +  return NS_OK;
    1.31 +}
    1.32 +
    1.33 +NS_IMETHODIMP
    1.34 +ColorPickerParent::ColorPickerShownCallback::Done(const nsAString& aColor)
    1.35 +{
    1.36 +  if (mColorPickerParent) {
    1.37 +    unused << mColorPickerParent->Send__delete__(mColorPickerParent,
    1.38 +                                                 nsString(aColor));
    1.39 +  }
    1.40 +  return NS_OK;
    1.41 +}
    1.42 +
    1.43 +void
    1.44 +ColorPickerParent::ColorPickerShownCallback::Destroy()
    1.45 +{
    1.46 +  mColorPickerParent = nullptr;
    1.47 +}
    1.48 +
    1.49 +bool
    1.50 +ColorPickerParent::CreateColorPicker()
    1.51 +{
    1.52 +  mPicker = do_CreateInstance("@mozilla.org/colorpicker;1");
    1.53 +  if (!mPicker) {
    1.54 +    return false;
    1.55 +  }
    1.56 +
    1.57 +  Element* ownerElement = static_cast<TabParent*>(Manager())->GetOwnerElement();
    1.58 +  if (!ownerElement) {
    1.59 +    return false;
    1.60 +  }
    1.61 +
    1.62 +  nsCOMPtr<nsIDOMWindow> window = do_QueryInterface(ownerElement->OwnerDoc()->GetWindow());
    1.63 +  if (!window) {
    1.64 +    return false;
    1.65 +  }
    1.66 +
    1.67 +  return NS_SUCCEEDED(mPicker->Init(window, mTitle, mInitialColor));
    1.68 +}
    1.69 +
    1.70 +bool
    1.71 +ColorPickerParent::RecvOpen()
    1.72 +{
    1.73 +  if (!CreateColorPicker()) {
    1.74 +    unused << Send__delete__(this, mInitialColor);
    1.75 +    return true;
    1.76 +  }
    1.77 +
    1.78 +  mCallback = new ColorPickerShownCallback(this);
    1.79 +
    1.80 +  mPicker->Open(mCallback);
    1.81 +  return true;
    1.82 +};
    1.83 +
    1.84 +void
    1.85 +ColorPickerParent::ActorDestroy(ActorDestroyReason aWhy)
    1.86 +{
    1.87 +  if (mCallback) {
    1.88 +    mCallback->Destroy();
    1.89 +  }
    1.90 +}

mercurial