Home | History | Annotate | Download | only in WebPage
      1 /*
      2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
      3  * Copyright (C) 2010 Apple Inc. All rights reserved.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  *
     20  */
     21 
     22 #include "config.h"
     23 #include "WebContextMenu.h"
     24 
     25 #include "ContextMenuState.h"
     26 #include "InjectedBundleHitTestResult.h"
     27 #include "InjectedBundleUserMessageCoders.h"
     28 #include "WebCoreArgumentCoders.h"
     29 #include "WebPage.h"
     30 #include "WebPageProxyMessages.h"
     31 #include "WebProcess.h"
     32 #include <WebCore/ContextMenu.h>
     33 #include <WebCore/ContextMenuController.h>
     34 #include <WebCore/FrameView.h>
     35 #include <WebCore/Page.h>
     36 
     37 using namespace WebCore;
     38 
     39 namespace WebKit {
     40 
     41 WebContextMenu::WebContextMenu(WebPage* page)
     42     : m_page(page)
     43 {
     44 }
     45 
     46 WebContextMenu::~WebContextMenu()
     47 {
     48 }
     49 
     50 void WebContextMenu::show()
     51 {
     52     ContextMenuController* controller = m_page->corePage()->contextMenuController();
     53     if (!controller)
     54         return;
     55     ContextMenu* menu = controller->contextMenu();
     56     if (!menu)
     57         return;
     58     Node* node = controller->hitTestResult().innerNonSharedNode();
     59     if (!node)
     60         return;
     61     Frame* frame = node->document()->frame();
     62     if (!frame)
     63         return;
     64     FrameView* view = frame->view();
     65     if (!view)
     66         return;
     67 
     68     // Give the bundle client a chance to process the menu.
     69 #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
     70     const Vector<ContextMenuItem>& coreItems = menu->items();
     71 #else
     72     Vector<ContextMenuItem> coreItems = contextMenuItemVector(menu->platformDescription());
     73 #endif
     74     Vector<WebContextMenuItemData> proposedMenu = kitItems(coreItems, menu);
     75     Vector<WebContextMenuItemData> newMenu;
     76     RefPtr<APIObject> userData;
     77     RefPtr<InjectedBundleHitTestResult> hitTestResult = InjectedBundleHitTestResult::create(controller->hitTestResult());
     78     if (m_page->injectedBundleContextMenuClient().getCustomMenuFromDefaultItems(m_page, hitTestResult.get(), proposedMenu, newMenu, userData))
     79         proposedMenu = newMenu;
     80 
     81     ContextMenuState contextMenuState;
     82     contextMenuState.absoluteImageURLString = controller->hitTestResult().absoluteImageURL().string();
     83     contextMenuState.absoluteLinkURLString = controller->hitTestResult().absoluteLinkURL().string();
     84 
     85     // Mark the WebPage has having a shown context menu then notify the UIProcess.
     86     m_page->contextMenuShowing();
     87     m_page->send(Messages::WebPageProxy::ShowContextMenu(view->contentsToWindow(controller->hitTestResult().point()), contextMenuState, proposedMenu, InjectedBundleUserMessageEncoder(userData.get())));
     88 }
     89 
     90 void WebContextMenu::itemSelected(const WebContextMenuItemData& item)
     91 {
     92     ContextMenuItem coreItem(ActionType, static_cast<ContextMenuAction>(item.action()), item.title());
     93     m_page->corePage()->contextMenuController()->contextMenuItemSelected(&coreItem);
     94 }
     95 
     96 } // namespace WebKit
     97