1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * This class supports the information for the media file.
8 * @param {FileEntry} entry Entry of media file. This must be a external entry.
11 function MediaManager(entry) {
14 this.cachedDriveProp_ = null;
15 this.cachedUrl_ = null;
16 this.cachedToken_ = null;
21 MediaManager.prototype = {};
24 * Checks if the file is available for cast or not.
26 * @return {Promise} Promise which is resolved with boolean. If true, the file
27 * is available for cast. False, otherwise.
29 MediaManager.prototype.isAvailableForCast = function() {
30 return this.getUrl().then(function(url) {
38 * Retrieves the token for cast.
40 * @param {boolean} refresh If true, force to refresh a token. If false, use the
41 * cached token if available.
42 * @return {Promise} Promise which is resolved with the token. Reject if failed.
44 MediaManager.prototype.getToken = function(refresh) {
46 return Promise.resolve('DUMMY_ACCESS_TOKEN');
48 if (this.cachedToken_ && !refresh)
49 return Promise.resolve(this.cachedToken_);
51 return new Promise(function(fulfill, reject) {
52 // TODO(yoshiki): Creates the method to get a token and use it.
53 chrome.fileManagerPrivate.getDownloadUrl(this.entry_.toURL(), fulfill);
54 }.bind(this)).then(function(url) {
55 if (chrome.runtime.lastError) {
56 return Promise.reject(
57 'Token fetch failed: ' + chrome.runtime.lastError.message);
60 return Promise.reject('Token fetch failed.');
61 var token = url.substring(url.indexOf('access_token=') + 13);
63 this.cachedToken_ = token;
66 return Promise.reject('Token fetch failed.');
72 * Retrieves the url for cast.
74 * @return {Promise} Promise which is resolved with the url. Reject if failed.
76 MediaManager.prototype.getUrl = function() {
78 return Promise.resolve('http://example.com/dummy_url.mp4');
81 return Promise.resolve(this.cachedUrl_);
83 return new Promise(function(fulfill, reject) {
84 // TODO(yoshiki): Creates the method to get a url and use it.
85 chrome.fileManagerPrivate.getDownloadUrl(this.entry_.toURL(), fulfill);
86 }.bind(this)).then(function(url) {
87 if (chrome.runtime.lastError) {
88 return Promise.reject(
89 'URL fetch failed: ' + chrome.runtime.lastError.message);
92 return Promise.reject('URL fetch failed.');
93 var access_token_index = url.indexOf('access_token=');
94 if (access_token_index) {
95 url = url.substring(0, access_token_index - 1);
97 this.cachedUrl_ = url;
103 * Retrieves the mime of file.
105 * @return {Promise} Promise which is resolved with the mime. Reject if failed.
107 MediaManager.prototype.getMime = function() {
108 if (this.cachedDriveProp_)
109 return Promise.resolve(this.cachedDriveProp_.contentMimeType || '');
111 return new Promise(function(fulfill, reject) {
112 chrome.fileManagerPrivate.getEntryProperties(
113 [this.entry_.toURL()], fulfill);
114 }.bind(this)).then(function(props) {
115 if (!props || !props[0]) {
116 return Promise.reject('Mime fetch failed.');
117 } else if (!props[0].contentMimeType) {
118 // TODO(yoshiki): Adds a logic to guess the mime.
119 this.cachedDriveProp_ = props[0];
122 this.cachedDriveProp_ = props[0];
123 return props[0].contentMimeType;
129 * Retrieves the thumbnail url of file.
131 * @return {Promise} Promise which is resolved with the url. Reject if failed.
133 MediaManager.prototype.getThumbnail = function() {
134 if (this.cachedDriveProp_)
135 return Promise.resolve(this.cachedDriveProp_.thumbnailUrl || '');
137 return new Promise(function(fulfill, reject) {
138 chrome.fileManagerPrivate.getEntryProperties(
139 [this.entry_.toURL()], fulfill);
140 }.bind(this)).then(function(props) {
141 if (!props || !props[0]) {
142 return Promise.reject('Thumbnail fetch failed.');
144 this.cachedDriveProp_ = props[0];
145 return props[0].thumbnailUrl || '';