Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "web/ColorChooserPopupUIController.h"
     28 
     29 #include "ColorSuggestionPicker.h"
     30 #include "PickerCommon.h"
     31 #include "core/frame/FrameView.h"
     32 #include "platform/ColorChooserClient.h"
     33 #include "platform/geometry/IntRect.h"
     34 #include "public/web/WebColorChooser.h"
     35 #include "web/ChromeClientImpl.h"
     36 #include "web/WebViewImpl.h"
     37 
     38 using namespace WebCore;
     39 
     40 namespace blink {
     41 
     42 // Keep in sync with Actions in colorSuggestionPicker.js.
     43 enum ColorPickerPopupAction {
     44     ColorPickerPopupActionChooseOtherColor = -2,
     45     ColorPickerPopupActionCancel = -1,
     46     ColorPickerPopupActionSetValue = 0
     47 };
     48 
     49 ColorChooserPopupUIController::ColorChooserPopupUIController(WebCore::LocalFrame* frame, ChromeClientImpl* chromeClient, ColorChooserClient* client)
     50     : ColorChooserUIController(frame, client)
     51     , m_chromeClient(chromeClient)
     52     , m_client(client)
     53     , m_popup(0)
     54     , m_locale(Locale::defaultLocale())
     55 {
     56 }
     57 
     58 ColorChooserPopupUIController::~ColorChooserPopupUIController()
     59 {
     60 }
     61 
     62 void ColorChooserPopupUIController::openUI()
     63 {
     64     if (m_client->shouldShowSuggestions())
     65         openPopup();
     66     else
     67         openColorChooser();
     68 }
     69 
     70 void ColorChooserPopupUIController::endChooser()
     71 {
     72     if (m_chooser)
     73         m_chooser->endChooser();
     74     if (m_popup)
     75         closePopup();
     76 }
     77 
     78 IntSize ColorChooserPopupUIController::contentSize()
     79 {
     80     return IntSize(0, 0);
     81 }
     82 
     83 void ColorChooserPopupUIController::writeDocument(SharedBuffer* data)
     84 {
     85     Vector<ColorSuggestion> suggestions = m_client->suggestions();
     86     Vector<String> suggestionValues;
     87     for (unsigned i = 0; i < suggestions.size(); i++)
     88         suggestionValues.append(suggestions[i].color.serialized());
     89     IntRect anchorRectInScreen = m_chromeClient->rootViewToScreen(m_client->elementRectRelativeToRootView());
     90 
     91     PagePopupClient::addString("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", data);
     92     data->append(pickerCommonCss, sizeof(pickerCommonCss));
     93     data->append(colorSuggestionPickerCss, sizeof(colorSuggestionPickerCss));
     94     PagePopupClient::addString("</style></head><body><div id=main>Loading...</div><script>\n"
     95         "window.dialogArguments = {\n", data);
     96     PagePopupClient::addProperty("values", suggestionValues, data);
     97     PagePopupClient::addProperty("otherColorLabel", locale().queryString(WebLocalizedString::OtherColorLabel), data);
     98     addProperty("anchorRectInScreen", anchorRectInScreen, data);
     99     PagePopupClient::addString("};\n", data);
    100     data->append(pickerCommonJs, sizeof(pickerCommonJs));
    101     data->append(colorSuggestionPickerJs, sizeof(colorSuggestionPickerJs));
    102     PagePopupClient::addString("</script></body>\n", data);
    103 }
    104 
    105 Locale& ColorChooserPopupUIController::locale()
    106 {
    107     return m_locale;
    108 }
    109 
    110 void ColorChooserPopupUIController::setValueAndClosePopup(int numValue, const String& stringValue)
    111 {
    112     ASSERT(m_popup);
    113     ASSERT(m_client);
    114     if (numValue == ColorPickerPopupActionSetValue)
    115         setValue(stringValue);
    116     if (numValue == ColorPickerPopupActionChooseOtherColor)
    117         openColorChooser();
    118     closePopup();
    119 }
    120 
    121 void ColorChooserPopupUIController::setValue(const String& value)
    122 {
    123     ASSERT(m_client);
    124     Color color;
    125     bool success = color.setFromString(value);
    126     ASSERT_UNUSED(success, success);
    127     m_client->didChooseColor(color);
    128 }
    129 
    130 void ColorChooserPopupUIController::didClosePopup()
    131 {
    132     m_popup = 0;
    133 
    134     if (!m_chooser)
    135         didEndChooser();
    136 }
    137 
    138 
    139 void ColorChooserPopupUIController::openPopup()
    140 {
    141     ASSERT(!m_popup);
    142     m_popup = m_chromeClient->openPagePopup(this, m_client->elementRectRelativeToRootView());
    143 }
    144 
    145 void ColorChooserPopupUIController::closePopup()
    146 {
    147     if (!m_popup)
    148         return;
    149     m_chromeClient->closePagePopup(m_popup);
    150 }
    151 
    152 }
    153