1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- import { Injectable , NgZone } from '@angular/core' ;
15+ import { Injectable } from '@angular/core' ;
1616import { TranslateService } from '@ngx-translate/core' ;
1717import { CoreAppProvider } from '@providers/app' ;
1818import { CoreFileProvider } from '@providers/file' ;
@@ -24,6 +24,8 @@ import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper';
2424import { CoreSite } from '@classes/site' ;
2525import { CoreWSExternalWarning , CoreWSExternalFile } from '@providers/ws' ;
2626
27+ import { makeSingleton } from '@singletons/core.singletons' ;
28+
2729/**
2830 * Service that provides some features for LTI.
2931 */
@@ -41,8 +43,7 @@ export class AddonModLtiProvider {
4143 private utils : CoreUtilsProvider ,
4244 private translate : TranslateService ,
4345 private appProvider : CoreAppProvider ,
44- private logHelper : CoreCourseLogHelperProvider ,
45- protected zone : NgZone ) { }
46+ private logHelper : CoreCourseLogHelperProvider ) { }
4647
4748 /**
4849 * Delete launcher.
@@ -65,10 +66,23 @@ export class AddonModLtiProvider {
6566 return url ;
6667 }
6768
68- // Generate an empty page with the JS code.
69- const text = '<script type="text/javascript"> \n' +
69+ // Generate a form with the params.
70+ let text = '<form action="' + url + '" name="ltiLaunchForm" ' +
71+ 'method="post" encType="application/x-www-form-urlencoded">\n' ;
72+ params . forEach ( ( p ) => {
73+ if ( p . name == 'ext_submit' ) {
74+ text += ' <input type="submit"' ;
75+ } else {
76+ text += ' <input type="hidden" name="' + this . textUtils . escapeHTML ( p . name ) + '"' ;
77+ }
78+ text += ' value="' + this . textUtils . escapeHTML ( p . value ) + '"/>\n' ;
79+ } ) ;
80+ text += '</form>\n' ;
81+
82+ // Add an in-line script to automatically submit the form.
83+ text += '<script type="text/javascript"> \n' +
7084 ' window.onload = function() { \n' +
71- this . getLaunchJSCode ( url , params ) +
85+ ' document.ltiLaunchForm.submit(); \n' +
7286 ' }; \n' +
7387 '</script> \n' ;
7488
@@ -81,42 +95,6 @@ export class AddonModLtiProvider {
8195 }
8296 }
8397
84- /**
85- * Get the Javascript code to launch the LTI tool.
86- *
87- * @param url Launch URL.
88- * @param params Launch params.
89- * @return Javascript code.
90- */
91- getLaunchJSCode ( url : string , params : AddonModLtiParam [ ] ) : string {
92- // Create the form.
93- let jsCode = 'var form = document.createElement("form");\n' +
94- 'form.method = "post";\n' +
95- 'form.setAttribute("encType", "application/x-www-form-urlencoded");\n' +
96- `form.setAttribute("action", "${ url } ");\n` ;
97-
98- // Create the inputs based on the params.
99- params . forEach ( ( p ) => {
100- jsCode += 'var input = document.createElement("input");\n' ;
101-
102- if ( p . name == 'ext_submit' ) {
103- jsCode += 'input.type = "submit";\n' ;
104- } else {
105- jsCode += 'input.type = "hidden";\n' +
106- 'input.name = "' + this . textUtils . escapeHTML ( p . name ) + '";\n' ;
107- }
108-
109- jsCode += 'input.value = "' + this . textUtils . escapeHTML ( p . value ) + '";\n' +
110- 'form.appendChild(input);\n' ;
111- } ) ;
112-
113- // Add the form to the document and submit it.
114- jsCode += 'document.body.appendChild(form);\n' +
115- 'form.submit();\n' ;
116-
117- return jsCode ;
118- }
119-
12098 /**
12199 * Get a LTI.
122100 *
@@ -217,6 +195,30 @@ export class AddonModLtiProvider {
217195 return this . sitesProvider . getCurrentSite ( ) . invalidateWsCacheForKey ( this . getLtiLaunchDataCacheKey ( id ) ) ;
218196 }
219197
198+ /**
199+ * Check if open in InAppBrowser is disabled.
200+ *
201+ * @param siteId Site ID. If not defined, current site.
202+ * @return Promise resolved with boolean: whether it's disabled.
203+ */
204+ async isOpenInAppBrowserDisabled ( siteId ?: string ) : Promise < boolean > {
205+ const site = await this . sitesProvider . getSite ( siteId ) ;
206+
207+ return this . isOpenInAppBrowserDisabledInSite ( site ) ;
208+ }
209+
210+ /**
211+ * Check if open in InAppBrowser is disabled.
212+ *
213+ * @param site Site. If not defined, current site.
214+ * @return Whether it's disabled.
215+ */
216+ isOpenInAppBrowserDisabledInSite ( site ?: CoreSite ) : boolean {
217+ site = site || this . sitesProvider . getCurrentSite ( ) ;
218+
219+ return site . isFeatureDisabled ( 'CoreCourseModuleDelegate_AddonModLti:openInAppBrowser' ) ;
220+ }
221+
220222 /**
221223 * Launch LTI.
222224 *
@@ -229,40 +231,13 @@ export class AddonModLtiProvider {
229231 throw this . translate . instant ( 'addon.mod_lti.errorinvalidlaunchurl' ) ;
230232 }
231233
232- if ( this . appProvider . isMobile ( ) ) {
233- // Open it in InAppBrowser. Use JS code because IAB has a bug in iOS when opening local files.
234- const jsCode = this . getLaunchJSCode ( url , params ) ;
235-
236- const iabInstance = this . utils . openInApp ( 'about:blank' ) ;
237-
238- // Execute the JS code when the page is loaded.
239- let codeExecuted = false ;
240- const executeCode = ( ) : void => {
241- if ( codeExecuted ) {
242- return ;
243- }
244-
245- codeExecuted = true ;
246- loadStopSubscription && loadStopSubscription . unsubscribe ( ) ;
234+ // Generate launcher and open it.
235+ const launcherUrl = await this . generateLauncher ( url , params ) ;
247236
248- // Execute the callback in the Angular zone, so change detection doesn't stop working.
249- this . zone . run ( ( ) => {
250- iabInstance . executeScript ( { code : jsCode } ) ;
251- } ) ;
252- } ;
253-
254- const loadStopSubscription = iabInstance . on ( 'loadstop' ) . subscribe ( ( event ) => {
255- executeCode ( ) ;
256- } ) ;
257-
258- // If loadstop hasn't triggered after 1 second, execute the code anyway.
259- setTimeout ( ( ) => {
260- executeCode ( ) ;
261- } , 1000 ) ;
237+ if ( this . appProvider . isMobile ( ) ) {
238+ this . utils . openInApp ( launcherUrl ) ;
262239 } else {
263- // Generate launched and open it in system browser, we found some cases where inapp caused JS issues.
264- const launcherUrl = await this . generateLauncher ( url , params ) ;
265-
240+ // In desktop open in browser, we found some cases where inapp caused JS issues.
266241 this . utils . openInBrowser ( launcherUrl ) ;
267242 }
268243 }
@@ -284,6 +259,8 @@ export class AddonModLtiProvider {
284259 }
285260}
286261
262+ export class AddonModLti extends makeSingleton ( AddonModLtiProvider ) { }
263+
287264/**
288265 * LTI returned by mod_lti_get_ltis_by_courses.
289266 */
0 commit comments