Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Modern Media Controls] Add a MediaControlsHost API to retrieve image…
…s as base64

https://bugs.webkit.org/show_bug.cgi?id=163502
<rdar://problem/28792017>

Patch by Antoine Quint <graouts@apple.com> on 2016-10-17
Reviewed by Darin Adler.

Add a new `base64StringForIconAndPlatform()` method to MediaControlsHost such that we
may load images from the modern-media-controls directory in the WebCore.framework bundle.
We use this new method in the iconService singleton when a `mediaControlsHost` is set,
which will be set in a later patch.

* Modules/mediacontrols/MediaControlsHost.cpp:
(WebCore::MediaControlsHost::base64StringForIconAndPlatform):
* Modules/mediacontrols/MediaControlsHost.h:
* Modules/mediacontrols/MediaControlsHost.idl:
* Modules/modern-media-controls/controls/icon-service.js:
(const.iconService.new.IconService.prototype.urlForIconNameAndLayoutTraits):
(const.iconService.new.IconService):
* rendering/RenderTheme.h:
(WebCore::RenderTheme::mediaControlsBase64StringForIconAndPlatform):
(WebCore::RenderTheme::mediaControlsScript): Deleted.
* rendering/RenderThemeMac.h:
* rendering/RenderThemeMac.mm:
(WebCore::RenderThemeMac::mediaControlsBase64StringForIconAndPlatform):

Canonical link: https://commits.webkit.org/181341@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@207423 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
graouts authored and webkit-commit-queue committed Oct 17, 2016
1 parent 398906f commit 92ebfc2
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 12 deletions.
Expand Up @@ -4,9 +4,9 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE


Checking path to images is computed according to traits
PASS iconService.urlForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS).includes("macOS/pause") is true
PASS iconService.urlForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.iOS).includes("iOS/pause") is true
PASS iconService.urlForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS | LayoutTraits.Fullscreen).includes("macOS/pause-fullscreen") is true
PASS iconService.imageForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS).src.includes("macOS/pause@") is true
PASS iconService.imageForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.iOS).src.includes("iOS/pause@") is true
PASS iconService.imageForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS | LayoutTraits.Fullscreen).src.includes("macOS/pause-fullscreen@") is true

Checking requested images are cached
PASS iconService.imageForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS) === iconService.imageForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS) is true
Expand Down
Expand Up @@ -10,9 +10,9 @@
iconService.directoryPath = "../../../../Source/WebCore/Modules/modern-media-controls/images";

debug("Checking path to images is computed according to traits");
shouldBeTrue('iconService.urlForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS).includes("macOS/pause")');
shouldBeTrue('iconService.urlForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.iOS).includes("iOS/pause")');
shouldBeTrue('iconService.urlForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS | LayoutTraits.Fullscreen).includes("macOS/pause-fullscreen")');
shouldBeTrue('iconService.imageForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS).src.includes("macOS/pause@")');
shouldBeTrue('iconService.imageForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.iOS).src.includes("iOS/pause@")');
shouldBeTrue('iconService.imageForIconNameAndLayoutTraits(Icons.Pause, LayoutTraits.macOS | LayoutTraits.Fullscreen).src.includes("macOS/pause-fullscreen@")');

debug("");
debug("Checking requested images are cached");
Expand Down
27 changes: 27 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,30 @@
2016-10-17 Antoine Quint <graouts@apple.com>

[Modern Media Controls] Add a MediaControlsHost API to retrieve images as base64
https://bugs.webkit.org/show_bug.cgi?id=163502
<rdar://problem/28792017>

Reviewed by Darin Adler.

Add a new `base64StringForIconAndPlatform()` method to MediaControlsHost such that we
may load images from the modern-media-controls directory in the WebCore.framework bundle.
We use this new method in the iconService singleton when a `mediaControlsHost` is set,
which will be set in a later patch.

* Modules/mediacontrols/MediaControlsHost.cpp:
(WebCore::MediaControlsHost::base64StringForIconAndPlatform):
* Modules/mediacontrols/MediaControlsHost.h:
* Modules/mediacontrols/MediaControlsHost.idl:
* Modules/modern-media-controls/controls/icon-service.js:
(const.iconService.new.IconService.prototype.urlForIconNameAndLayoutTraits):
(const.iconService.new.IconService):
* rendering/RenderTheme.h:
(WebCore::RenderTheme::mediaControlsBase64StringForIconAndPlatform):
(WebCore::RenderTheme::mediaControlsScript): Deleted.
* rendering/RenderThemeMac.h:
* rendering/RenderThemeMac.mm:
(WebCore::RenderThemeMac::mediaControlsBase64StringForIconAndPlatform):

2016-10-17 Antoine Quint <graouts@apple.com>

[Modern Media Controls] Add a MediaControlsHost API to retrieve the shadow root CSS
Expand Down
8 changes: 8 additions & 0 deletions Source/WebCore/Modules/mediacontrols/MediaControlsHost.cpp
Expand Up @@ -292,6 +292,14 @@ String MediaControlsHost::shadowRootCSSText() const
return RenderTheme::themeForPage(page)->mediaControlsStyleSheet();
}

String MediaControlsHost::base64StringForIconAndPlatform(const String& iconName, const String& platform) const
{
Page* page = m_mediaElement->document().page();
if (!page)
return emptyString();
return RenderTheme::themeForPage(page)->mediaControlsBase64StringForIconAndPlatform(iconName, platform);
}

}

#endif
1 change: 1 addition & 0 deletions Source/WebCore/Modules/mediacontrols/MediaControlsHost.h
Expand Up @@ -83,6 +83,7 @@ class MediaControlsHost : public RefCounted<MediaControlsHost> {
String generateUUID() const;

String shadowRootCSSText() const;
String base64StringForIconAndPlatform(const String& iconName, const String& platform) const;

private:
MediaControlsHost(HTMLMediaElement*);
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/Modules/mediacontrols/MediaControlsHost.idl
Expand Up @@ -61,4 +61,5 @@ enum DeviceType {
DOMString generateUUID();

[EnabledAtRuntime=ModernMediaControls] readonly attribute DOMString shadowRootCSSText;
[EnabledAtRuntime=ModernMediaControls] DOMString base64StringForIconAndPlatform(DOMString iconName, DOMString platform);
};
Expand Up @@ -52,20 +52,30 @@ const iconService = new class IconService {
this.images = {};
}

// Public

imageForIconNameAndLayoutTraits(iconName, layoutTraits)
{
const path = this.urlForIconNameAndLayoutTraits(iconName, layoutTraits);

const [fileName, platform] = this._fileNameAndPlatformForIconNameAndLayoutTraits(iconName, layoutTraits);
const path = `${platform}/${fileName}.png`;

let image = this.images[path];
if (image)
return image;

image = this.images[path] = new Image;
image.src = path;

if (this.mediaControlsHost)
image.src = "data:image/png;base64," + this.mediaControlsHost.base64StringForIconAndPlatform(fileName, platform);
else
image.src = `${this.directoryPath}/${path}`;

return image;
}

urlForIconNameAndLayoutTraits(iconName, layoutTraits)
// Private

_fileNameAndPlatformForIconNameAndLayoutTraits(iconName, layoutTraits)
{
let platform;
if (layoutTraits & LayoutTraits.macOS)
Expand All @@ -78,7 +88,9 @@ const iconService = new class IconService {
if (layoutTraits & LayoutTraits.Fullscreen && IconsWithFullScreenVariants.includes(iconName))
iconName += "-fullscreen";

return `${this.directoryPath}/${platform}/${iconName}@${window.devicePixelRatio}x.png`;
const fileName = `${iconName}@${window.devicePixelRatio}x`;

return [fileName, platform];
}

};
Expand Up @@ -36,7 +36,7 @@ class StartButton extends Button
background.className = "background";

const image = this.element.appendChild(new Image);
image.src = iconService.urlForIconNameAndLayoutTraits(Icons.Start, this.layoutTraits);
image.src = iconService.imageForIconNameAndLayoutTraits(Icons.Start, this.layoutTraits).src;
}

}
1 change: 1 addition & 0 deletions Source/WebCore/rendering/RenderTheme.h
Expand Up @@ -101,6 +101,7 @@ class RenderTheme : public RefCounted<RenderTheme> {
virtual String mediaControlsStyleSheet() { return String(); }
virtual String extraMediaControlsStyleSheet() { return String(); }
virtual String mediaControlsScript() { return String(); }
virtual String mediaControlsBase64StringForIconAndPlatform(const String&, const String&) { return String(); }
#endif
#if ENABLE(FULLSCREEN_API)
virtual String extraFullScreenStyleSheet() { return String(); }
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/rendering/RenderThemeMac.h
Expand Up @@ -108,6 +108,7 @@ class RenderThemeMac final : public RenderThemeCocoa {
// Media controls
String mediaControlsStyleSheet() override;
String mediaControlsScript() override;
String mediaControlsBase64StringForIconAndPlatform(const String&, const String&) override;
#endif

#if ENABLE(SERVICE_CONTROLS)
Expand Down
14 changes: 14 additions & 0 deletions Source/WebCore/rendering/RenderThemeMac.mm
Expand Up @@ -272,6 +272,20 @@ @interface NSSearchFieldCell()
#endif
}

String RenderThemeMac::mediaControlsBase64StringForIconAndPlatform(const String& iconName, const String& platform)
{
#if ENABLE(MEDIA_CONTROLS_SCRIPT)
if (!RuntimeEnabledFeatures::sharedFeatures().modernMediaControlsEnabled())
return emptyString();

String directory = "modern-media-controls/images/" + platform;
NSBundle *bundle = [NSBundle bundleForClass:[WebCoreRenderThemeBundle class]];
return [[NSData dataWithContentsOfFile:[bundle pathForResource:iconName ofType:@"png" inDirectory:directory]] base64EncodedStringWithOptions:0];
#else
return emptyString();
#endif
}

#endif // ENABLE(VIDEO)


Expand Down

0 comments on commit 92ebfc2

Please sign in to comment.