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_, 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 index = url.indexOf('access_token=');
62 var token = url.substring(index + 13);
63 if (index > 0 && token) {
64 this.cachedToken_ = token;
67 return Promise.reject('Token fetch failed.');
73 * Retrieves the url for cast.
75 * @return {Promise} Promise which is resolved with the url. Reject if failed.
77 MediaManager.prototype.getUrl = function() {
79 return Promise.resolve('http://example.com/dummy_url.mp4');
82 return Promise.resolve(this.cachedUrl_);
84 return new Promise(function(fulfill, reject) {
85 // TODO(yoshiki): Creates the method to get a url and use it.
86 chrome.fileManagerPrivate.getDownloadUrl(this.entry_, fulfill);
87 }.bind(this)).then(function(url) {
88 if (chrome.runtime.lastError) {
89 return Promise.reject(
90 'URL fetch failed: ' + chrome.runtime.lastError.message);
93 return Promise.reject('URL fetch failed.');
94 var access_token_index = url.indexOf('access_token=');
95 if (access_token_index) {
96 url = url.substring(0, access_token_index - 1);
98 this.cachedUrl_ = url;
104 * Retrieves the mime of file.
106 * @return {Promise} Promise which is resolved with the mime. Reject if failed.
108 MediaManager.prototype.getMime = function() {
109 if (this.cachedDriveProp_)
110 return Promise.resolve(this.cachedDriveProp_.contentMimeType || '');
112 return new Promise(function(fulfill, reject) {
113 chrome.fileManagerPrivate.getEntryProperties(
114 [this.entry_], ['contentMimeType', 'thumbnailUrl'], fulfill);
115 }.bind(this)).then(function(props) {
116 if (!props || !props[0]) {
117 return Promise.reject('Mime fetch failed.');
118 } else if (!props[0].contentMimeType) {
119 // TODO(yoshiki): Adds a logic to guess the mime.
120 this.cachedDriveProp_ = props[0];
123 this.cachedDriveProp_ = props[0];
124 return props[0].contentMimeType;
130 * Retrieves the thumbnail url of file.
132 * @return {Promise} Promise which is resolved with the url. Reject if failed.
134 MediaManager.prototype.getThumbnail = function() {
135 if (this.cachedDriveProp_)
136 return Promise.resolve(this.cachedDriveProp_.thumbnailUrl || '');
138 return new Promise(function(fulfill, reject) {
139 chrome.fileManagerPrivate.getEntryProperties(
140 [this.entry_], ['contentMimeType', 'thumbnailUrl'], fulfill);
141 }.bind(this)).then(function(props) {
142 if (!props || !props[0]) {
143 return Promise.reject('Thumbnail fetch failed.');
145 this.cachedDriveProp_ = props[0];
146 return props[0].thumbnailUrl || '';