Commit d0552800 authored by Becca Hughes's avatar Becca Hughes Committed by Commit Bot

[Media Session] [4/4] Move Metadata to service

This CL deletes the old content::MediaMetadata types.

BUG=875004

Change-Id: Ia6c5fa76090d2e848261207d6a0dc28e782a4db3
Reviewed-on: https://chromium-review.googlesource.com/c/1241554
Commit-Queue: Becca Hughes <beccahughes@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612750}
parent 88c43d22
......@@ -42,8 +42,6 @@ source_set("common") {
"android/gin_java_bridge_value.h",
"android/hash_set.cc",
"android/hash_set.h",
"android/media_metadata_android.cc",
"android/media_metadata_android.h",
"android/sync_compositor_statics.cc",
"android/sync_compositor_statics.h",
"android/use_zoom_for_dsf_policy_android.cc",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/android/media_metadata_android.h"
#include <string>
#include <vector>
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "content/public/common/media_metadata.h"
#include "jni/MediaMetadata_jni.h"
using base::android::ScopedJavaLocalRef;
namespace content {
namespace {
std::vector<int> GetFlattenedSizeArray(const std::vector<gfx::Size>& sizes) {
std::vector<int> flattened_array;
flattened_array.reserve(2 * sizes.size());
for (const auto& size : sizes) {
flattened_array.push_back(size.width());
flattened_array.push_back(size.height());
}
return flattened_array;
}
} // anonymous namespace
// static
base::android::ScopedJavaLocalRef<jobject>
MediaMetadataAndroid::CreateJavaObject(
JNIEnv* env, const MediaMetadata& metadata) {
ScopedJavaLocalRef<jstring> j_title(
base::android::ConvertUTF16ToJavaString(env, metadata.title));
ScopedJavaLocalRef<jstring> j_artist(
base::android::ConvertUTF16ToJavaString(env, metadata.artist));
ScopedJavaLocalRef<jstring> j_album(
base::android::ConvertUTF16ToJavaString(env, metadata.album));
ScopedJavaLocalRef<jobject> j_metadata =
Java_MediaMetadata_create(env, j_title, j_artist, j_album);
for (const auto& image : metadata.artwork) {
std::string src = image.src.spec();
ScopedJavaLocalRef<jstring> j_src(
base::android::ConvertUTF8ToJavaString(env, src));
ScopedJavaLocalRef<jstring> j_type(
base::android::ConvertUTF16ToJavaString(env, image.type));
ScopedJavaLocalRef<jintArray> j_sizes(
base::android::ToJavaIntArray(env, GetFlattenedSizeArray(image.sizes)));
Java_MediaMetadata_createAndAddMediaImage(env, j_metadata, j_src, j_type,
j_sizes);
}
return j_metadata;
}
} // namespace content
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_COMMON_ANDROID_MEDIA_METADATA_ANDROID_H_
#define CONTENT_COMMON_ANDROID_MEDIA_METADATA_ANDROID_H_
#include <jni.h>
#include "base/android/scoped_java_ref.h"
namespace content {
struct MediaMetadata;
class MediaMetadataAndroid {
public:
static base::android::ScopedJavaLocalRef<jobject> CreateJavaObject(
JNIEnv* env, const MediaMetadata& metadata);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(MediaMetadataAndroid);
};
} // namespace content
#endif // CONTENT_COMMON_ANDROID_MEDIA_METADATA_ANDROID_H_
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
mojom = "//third_party/blink/public/platform/modules/mediasession/media_session.mojom"
public_headers = [ "//content/public/common/media_metadata.h" ]
traits_headers = [ "//content/common/media/media_session_struct_traits.h" ]
type_mappings = [
"blink.mojom.MediaImage=content::MediaMetadata::MediaImage",
"blink.mojom.MediaMetadata=content::MediaMetadata",
]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_COMMON_MEDIA_MEDIA_SESSION_STRUCT_TRAITS_H_
#define CONTENT_COMMON_MEDIA_MEDIA_SESSION_STRUCT_TRAITS_H_
#include "third_party/blink/public/platform/modules/mediasession/media_session.mojom.h"
namespace mojo {
template <>
struct StructTraits<blink::mojom::MediaImageDataView,
content::MediaMetadata::MediaImage> {
static const GURL& src(const content::MediaMetadata::MediaImage& image) {
return image.src;
}
static const base::string16& type(
const content::MediaMetadata::MediaImage& image) {
return image.type;
}
static const std::vector<gfx::Size>& sizes(
const content::MediaMetadata::MediaImage& image) {
return image.sizes;
}
static bool Read(blink::mojom::MediaImageDataView data,
content::MediaMetadata::MediaImage* out) {
if (!data.ReadSrc(&out->src))
return false;
if (!data.ReadType(&out->type))
return false;
if (!data.ReadSizes(&out->sizes))
return false;
return true;
}
};
template <>
struct StructTraits<blink::mojom::MediaMetadataDataView,
content::MediaMetadata> {
static const base::string16& title(const content::MediaMetadata& metadata) {
return metadata.title;
}
static const base::string16& artist(const content::MediaMetadata& metadata) {
return metadata.artist;
}
static const base::string16& album(const content::MediaMetadata& metadata) {
return metadata.album;
}
static const std::vector<content::MediaMetadata::MediaImage>& artwork(
const content::MediaMetadata& metadata) {
return metadata.artwork;
}
static bool Read(blink::mojom::MediaMetadataDataView data,
content::MediaMetadata* out) {
if (!data.ReadTitle(&out->title))
return false;
if (!data.ReadArtist(&out->artist))
return false;
if (!data.ReadAlbum(&out->album))
return false;
if (!data.ReadArtwork(&out->artwork))
return false;
return true;
}
};
} // namespace mojo
#endif // CONTENT_COMMON_MEDIA_MEDIA_SESSION_STRUCT_TRAITS_H_
......@@ -16,5 +16,4 @@ typemaps = [
"//content/common/render_frame_metadata.typemap",
"//content/common/url_loader_factory_bundle.typemap",
"//content/common/web_preferences.typemap",
"//content/common/media/media_session.typemap",
]
......@@ -277,7 +277,6 @@ android_library("content_java") {
"java/src/org/chromium/content_public/common/ContentProcessInfo.java",
"java/src/org/chromium/content_public/common/ContentSwitches.java",
"java/src/org/chromium/content_public/common/ContentUrlConstants.java",
"java/src/org/chromium/content_public/common/MediaMetadata.java",
"java/src/org/chromium/content_public/common/Referrer.java",
"java/src/org/chromium/content_public/common/ResourceRequestBody.java",
"java/src/org/chromium/content_public/common/ScreenOrientationConstants.java",
......@@ -402,7 +401,6 @@ generate_jni("content_jni_headers") {
"java/src/org/chromium/content/browser/webcontents/WebContentsObserverProxy.java",
"java/src/org/chromium/content/common/ServiceManagerConnectionImpl.java",
"java/src/org/chromium/content_public/browser/LoadUrlParams.java",
"java/src/org/chromium/content_public/common/MediaMetadata.java",
"java/src/org/chromium/content_public/common/ResourceRequestBody.java",
"java/src/org/chromium/content_public/common/UseZoomForDSFPolicy.java",
]
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content_public.common;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import java.util.ArrayList;
import java.util.List;
/**
* The MediaMetadata class carries information related to a media session. It is
* the Java counterpart of content::MediaMetadata.
*/
@JNINamespace("content")
public final class MediaMetadata {
/**
* The MediaImage class carries the artwork information in MediaMetadata. It is the Java
* counterpart of content::MediaMetadata::MediaImage.
*/
public static final class MediaImage {
@NonNull
private String mSrc;
private String mType;
@NonNull
private List<Rect> mSizes = new ArrayList<Rect>();
/**
* Creates a new MediaImage.
*/
public MediaImage(@NonNull String src, @NonNull String type, @NonNull List<Rect> sizes) {
mSrc = src;
mType = type;
mSizes = sizes;
}
/**
* @return The URL of this MediaImage.
*/
@NonNull
public String getSrc() {
return mSrc;
}
/**
* @return The MIME type of this MediaImage.
*/
public String getType() {
return mType;
}
/**
* @return The hinted sizes of this MediaImage.
*/
public List<Rect> getSizes() {
return mSizes;
}
/**
* Sets the URL of this MediaImage.
*/
public void setSrc(@NonNull String src) {
mSrc = src;
}
/**
* Sets the MIME type of this MediaImage.
*/
public void setType(@NonNull String type) {
mType = type;
}
/**
* Sets the sizes of this MediaImage.
*/
public void setSizes(@NonNull List<Rect> sizes) {
mSizes = sizes;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof MediaImage)) return false;
MediaImage other = (MediaImage) obj;
return TextUtils.equals(mSrc, other.mSrc)
&& TextUtils.equals(mType, other.mType)
&& mSizes.equals(other.mSizes);
}
/**
* @return The hash code of this {@link MediaImage}. The method uses the same algorithm in
* {@link java.util.List} for combinine hash values.
*/
@Override
public int hashCode() {
int result = mSrc.hashCode();
result = 31 * result + mType.hashCode();
result = 31 * result + mSizes.hashCode();
return result;
}
}
@NonNull
private String mTitle;
@NonNull
private String mArtist;
@NonNull
private String mAlbum;
@NonNull
private List<MediaImage> mArtwork = new ArrayList<MediaImage>();
/**
* Returns the title associated with the media session.
*/
public String getTitle() {
return mTitle;
}
/**
* Returns the artist name associated with the media session.
*/
public String getArtist() {
return mArtist;
}
/**
* Returns the album name associated with the media session.
*/
public String getAlbum() {
return mAlbum;
}
public List<MediaImage> getArtwork() {
return mArtwork;
}
/**
* Sets the title associated with the media session.
* @param title The title to use for the media session.
*/
public void setTitle(@NonNull String title) {
mTitle = title;
}
/**
* Sets the arstist name associated with the media session.
* @param arstist The artist name to use for the media session.
*/
public void setArtist(@NonNull String artist) {
mArtist = artist;
}
/**
* Sets the album name associated with the media session.
* @param album The album name to use for the media session.
*/
public void setAlbum(@NonNull String album) {
mAlbum = album;
}
/**
* Create a new {@link MediaImage} from the C++ code, and add it to the Metadata.
* @param src The URL of the image.
* @param type The MIME type of the image.
* @param flattenedSizes The flattened array of image sizes. In native code, it is of type
* `std::vector<gfx::Size>` before flattening.
*/
@CalledByNative
private void createAndAddMediaImage(String src, String type, int[] flattenedSizes) {
assert (flattenedSizes.length % 2) == 0;
List<Rect> sizes = new ArrayList<Rect>();
for (int i = 0; (i + 1) < flattenedSizes.length; i += 2) {
sizes.add(new Rect(0, 0, flattenedSizes[i], flattenedSizes[i + 1]));
}
mArtwork.add(new MediaImage(src, type, sizes));
}
/**
* Creates a new MediaMetadata from the C++ code. This is exactly like the
* constructor below apart that it can be called by native code.
*/
@CalledByNative
private static MediaMetadata create(String title, String artist, String album) {
return new MediaMetadata(title == null ? "" : title, artist == null ? "" : artist,
album == null ? "" : album);
}
/**
* Creates a new MediaMetadata.
*/
public MediaMetadata(@NonNull String title, @NonNull String artist, @NonNull String album) {
mTitle = title;
mArtist = artist;
mAlbum = album;
}
/**
* Comparing MediaMetadata is expensive and should be used sparingly
*/
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof MediaMetadata)) return false;
MediaMetadata other = (MediaMetadata) obj;
return TextUtils.equals(mTitle, other.mTitle)
&& TextUtils.equals(mArtist, other.mArtist)
&& TextUtils.equals(mAlbum, other.mAlbum)
&& mArtwork.equals(other.mArtwork);
}
/**
* @return The hash code of this {@link MediaMetadata}. The method uses the same algorithm in
* {@link java.util.List} for combinine hash values.
*/
@Override
public int hashCode() {
int result = mTitle.hashCode();
result = 31 * result + mArtist.hashCode();
result = 31 * result + mAlbum.hashCode();
result = 31 * result + mArtwork.hashCode();
return result;
}
}
......@@ -157,8 +157,6 @@ jumbo_source_set("common_sources") {
"main_function_params.h",
"manifest_util.cc",
"manifest_util.h",
"media_metadata.cc",
"media_metadata.h",
"media_stream_request.cc",
"media_stream_request.h",
"menu_item.cc",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/common/media_metadata.h"
#include <algorithm>
#include <iterator>
namespace content {
MediaMetadata::MediaImage::MediaImage() = default;
MediaMetadata::MediaImage::MediaImage(const MediaImage& other) = default;
MediaMetadata::MediaImage::~MediaImage() = default;
bool MediaMetadata::MediaImage::operator==(
const MediaMetadata::MediaImage& other) const {
return src == other.src && type == other.type && sizes == other.sizes;
}
MediaMetadata::MediaMetadata() = default;
MediaMetadata::~MediaMetadata() = default;
MediaMetadata::MediaMetadata(const MediaMetadata& other) = default;
bool MediaMetadata::operator==(const MediaMetadata& other) const {
return title == other.title && artist == other.artist &&
album == other.album && artwork == other.artwork;
}
bool MediaMetadata::operator!=(const MediaMetadata& other) const {
return !this->operator==(other);
}
} // namespace content
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_PUBLIC_COMMON_MEDIA_METADATA_H_
#define CONTENT_PUBLIC_COMMON_MEDIA_METADATA_H_
#include <vector>
#include "base/strings/string16.h"
#include "content/common/content_export.h"
#include "ui/gfx/geometry/size.h"
#include "url/gurl.h"
namespace content {
// The MediaMetadata is a structure carrying information associated to a
// content::MediaSession.
struct CONTENT_EXPORT MediaMetadata {
// Structure representing an MediaImage as per the MediaSession API, see:
// https://wicg.github.io/mediasession/#dictdef-mediaimage
struct CONTENT_EXPORT MediaImage {
MediaImage();
MediaImage(const MediaImage& other);
~MediaImage();
bool operator==(const MediaImage& other) const;
// MUST be a valid url. If an icon doesn't have a valid URL, it will not be
// successfully parsed, thus will not be represented in the Manifest.
GURL src;
// Empty if the parsing failed or the field was not present. The type can be
// any string and doesn't have to be a valid image MIME type at this point.
// It is up to the consumer of the object to check if the type matches a
// supported type.
base::string16 type;
// Empty if the parsing failed, the field was not present or empty.
// The special value "any" is represented by gfx::Size(0, 0).
std::vector<gfx::Size> sizes;
};
MediaMetadata();
~MediaMetadata();
MediaMetadata(const MediaMetadata& other);
bool operator==(const MediaMetadata& other) const;
bool operator!=(const MediaMetadata& other) const;
// Title associated to the MediaSession.
base::string16 title;
// Artist associated to the MediaSession.
base::string16 artist;
// Album associated to the MediaSession.
base::string16 album;
// Artwork associated to the MediaSession.
std::vector<MediaImage> artwork;
};
} // namespace content
#endif // CONTENT_PUBLIC_COMMON_MEDIA_METADATA_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment