1/*
2 * Copyright (C) 2015 Apple 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if ENABLE(RESOURCE_USAGE)
29
30#include "ResourceUsageOverlay.h"
31
32#include "Frame.h"
33#include "FrameView.h"
34#include "Page.h"
35#include "PageOverlayController.h"
36#include "PlatformMouseEvent.h"
37
38namespace WebCore {
39
40ResourceUsageOverlay::ResourceUsageOverlay(Page& page)
41 : m_page(page)
42 , m_overlay(PageOverlay::create(*this, PageOverlay::OverlayType::View))
43{
44 // Let the event loop cycle before continuing with initialization.
45 // This way we'll have access to the FrameView's dimensions.
46 callOnMainThread([this] {
47 initialize();
48 });
49}
50
51ResourceUsageOverlay::~ResourceUsageOverlay()
52{
53 platformDestroy();
54
55 // FIXME: This is a hack so we don't try to uninstall the PageOverlay during Page destruction.
56 if (m_page.mainFrame().page())
57 m_page.pageOverlayController().uninstallPageOverlay(*m_overlay, PageOverlay::FadeMode::DoNotFade);
58}
59
60void ResourceUsageOverlay::initialize()
61{
62 if (!m_page.mainFrame().view())
63 return;
64
65 FrameView& frameView = *m_page.mainFrame().view();
66
67 IntRect initialRect(frameView.width() / 2 - normalWidth / 2, frameView.height() - normalHeight - 20, normalWidth, normalHeight);
68
69#if PLATFORM(IOS_FAMILY)
70 // FIXME: The overlay should be stuck to the viewport instead of moving along with the page.
71 initialRect.setY(20);
72#endif
73
74 m_overlay->setFrame(initialRect);
75 m_page.pageOverlayController().installPageOverlay(*m_overlay, PageOverlay::FadeMode::DoNotFade);
76 platformInitialize();
77}
78
79bool ResourceUsageOverlay::mouseEvent(PageOverlay&, const PlatformMouseEvent& event)
80{
81 if (event.button() != LeftButton)
82 return false;
83
84 switch (event.type()) {
85 case PlatformEvent::MousePressed: {
86 m_overlay->setShouldIgnoreMouseEventsOutsideBounds(false);
87 m_dragging = true;
88 IntPoint location = m_overlay->frame().location();
89 m_dragPoint = event.position() + IntPoint(-location.x(), -location.y());
90 return true;
91 }
92 case PlatformEvent::MouseReleased:
93 if (m_dragging) {
94 m_overlay->setShouldIgnoreMouseEventsOutsideBounds(true);
95 m_dragging = false;
96 return true;
97 }
98 break;
99 case PlatformEvent::MouseMoved:
100 if (m_dragging) {
101 IntRect newFrame = m_overlay->frame();
102
103 // Move the new frame relative to the point where the drag was initiated.
104 newFrame.setLocation(event.position());
105 newFrame.moveBy(IntPoint(-m_dragPoint.x(), -m_dragPoint.y()));
106
107 // Force the frame to stay inside the viewport entirely.
108 if (newFrame.x() < 0)
109 newFrame.setX(0);
110 if (newFrame.y() < m_page.topContentInset())
111 newFrame.setY(m_page.topContentInset());
112 FrameView& frameView = *m_page.mainFrame().view();
113 if (newFrame.maxX() > frameView.width())
114 newFrame.setX(frameView.width() - newFrame.width());
115 if (newFrame.maxY() > frameView.height())
116 newFrame.setY(frameView.height() - newFrame.height());
117
118 m_overlay->setFrame(newFrame);
119 m_overlay->setNeedsDisplay();
120 return true;
121 }
122 break;
123 default:
124 break;
125 }
126 return false;
127}
128
129}
130
131#endif
132