OLD | NEW |
1 # Running Mojo Applications | 1 # Running Mojo Applications |
2 | 2 |
3 A Mojo application written in JavaScript is launched with mojo_shell like this: | 3 A Mojo application written in JavaScript is launched with mojo_shell like this: |
4 | 4 |
5 ``` | 5 ``` |
6 mojo_shell <js-application-url> | 6 mojo_shell <js-application-url> |
7 ``` | 7 ``` |
8 | 8 |
9 Where js-application-url is a URL understood by the shell. For example | 9 Where js-application-url is a URL understood by the shell. For example |
10 a file or an http URL that names a JS source file. The JS file itself | 10 a file or an http URL that names a JS source file. The JS file itself |
11 must begin with a Mojo "shebang" that specifies the Mojo URL of the JS | 11 must begin with a Mojo "shebang" that specifies the Mojo URL of the JS |
12 content handler. In other words, the first line of the JS source file | 12 content handler. In other words, the first line of the JS source file |
13 must be: | 13 must be: |
14 | 14 |
15 ```javascript | 15 ```javascript |
16 #!mojo:js_content_handler | 16 #!mojo mojo:js_content_handler |
17 ``` | 17 ``` |
18 | 18 |
19 Following the shebang should be a single AMD module called "main" whose value | 19 Following the shebang should be a single AMD module called "main" whose value |
20 is an Application subclass. The JS content handler will create an instance of | 20 is an Application subclass. The JS content handler will create an instance of |
21 the Application and make it the client of the Mojo shell. The JS content handler | 21 the Application and make it the client of the Mojo shell. The JS content handler |
22 is itself a Mojo application and it's responsible for creating an instance of V8 | 22 is itself a Mojo application and it's responsible for creating an instance of V8 |
23 and loading the "main" JS module and all of the modules the main module | 23 and loading the "main" JS module and all of the modules the main module |
24 depends on. | 24 depends on. |
25 | 25 |
26 ## JavaScript Classes | 26 ## JavaScript Classes |
27 | 27 |
28 The JS content handler depends on the ECMAScript6 ("Harmony") classes feature. | 28 The JS content handler depends on the ECMAScript6 ("Harmony") classes feature. |
29 | 29 |
30 As of January 2015 Chrome enables Harmony classes by default. | 30 As of January 2015 Chrome enables Harmony classes by default. |
31 | 31 |
32 | 32 |
33 # The JS Application Class | 33 # The JS Application Class |
34 | 34 |
35 Mojo JS applications are defined with the Application class. The | 35 Mojo JS applications are defined with the Application class. The |
36 Application class handles incoming requests for services and provides | 36 Application class handles incoming requests for services and provides |
37 services of its own. | 37 services of its own. |
38 | 38 |
39 This is the overall structure of a JS Mojo application: | 39 This is the overall structure of a JS Mojo application: |
40 | 40 |
41 ```javascript | 41 ```javascript |
42 #!mojo:js_content_handler | 42 #!mojo mojo:js_content_handler |
43 | 43 |
44 define("main", ["mojo/services/public/js/application", | 44 define("main", ["mojo/services/public/js/application", |
45 <list of other modules that this application depends on> | 45 <list of other modules that this application depends on> |
46 ], | 46 ], |
47 function(application, <one parameter per dependent module>) { | 47 function(application, <one parameter per dependent module>) { |
48 class MyApplication extends application.Application { | 48 class MyApplication extends application.Application { |
49 constructor(appShell, url) { | 49 constructor(appShell, url) { |
50 super(appShell, url); // Initializes this.shell, this.url. | 50 super(appShell, url); // Initializes this.shell, this.url. |
51 // MyApplication initializations here. | 51 // MyApplication initializations here. |
52 } | 52 } |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 ### Requesting a Service Using the Application's Shell | 423 ### Requesting a Service Using the Application's Shell |
424 | 424 |
425 The Shell's `connectToService()` method returns a proxy to a Mojo | 425 The Shell's `connectToService()` method returns a proxy to a Mojo |
426 service provided by another application. The proxy can be used immediately. | 426 service provided by another application. The proxy can be used immediately. |
427 | 427 |
428 Given the URL of a Mojo application that implements the EchoService we | 428 Given the URL of a Mojo application that implements the EchoService we |
429 can use the application's shell to get an EchoService proxy. Here's a | 429 can use the application's shell to get an EchoService proxy. Here's a |
430 complete application: | 430 complete application: |
431 | 431 |
432 ```javascript | 432 ```javascript |
433 #!mojo:js_content_handler | 433 #!mojo mojo:js_content_handler |
434 | 434 |
435 define("main", [ | 435 define("main", [ |
436 "console", | 436 "console", |
437 "mojo/services/public/js/application", | 437 "mojo/services/public/js/application", |
438 "services/js/test/echo_service.mojom" | 438 "services/js/test/echo_service.mojom" |
439 ], function(console, appModule, echoModule) { | 439 ], function(console, appModule, echoModule) { |
440 | 440 |
441 class EchoShellRequest extends appModule.Application { | 441 class EchoShellRequest extends appModule.Application { |
442 initialize(args) { | 442 initialize(args) { |
443 var url = "file:/foo/bar/echo.js"; | 443 var url = "file:/foo/bar/echo.js"; |
(...skipping 16 matching lines...) Expand all Loading... |
460 | 460 |
461 The echo_share.js and echo_share_target.js applications demonstrate this. | 461 The echo_share.js and echo_share_target.js applications demonstrate this. |
462 | 462 |
463 | 463 |
464 ### Providing a Service with an Application's ServiceProvider | 464 ### Providing a Service with an Application's ServiceProvider |
465 | 465 |
466 A complete application that unconditionally provides the EchoService | 466 A complete application that unconditionally provides the EchoService |
467 looks like this: | 467 looks like this: |
468 | 468 |
469 ```javascript | 469 ```javascript |
470 #!mojo:js_content_handler | 470 #!mojo mojo:js_content_handler |
471 | 471 |
472 define("main", [ | 472 define("main", [ |
473 "mojo/services/public/js/application", | 473 "mojo/services/public/js/application", |
474 "services/js/test/echo_service.mojom" | 474 "services/js/test/echo_service.mojom" |
475 ], function(appModule, echoModule) { | 475 ], function(appModule, echoModule) { |
476 | 476 |
477 class EchoService extends appModule.Application { | 477 class EchoService extends appModule.Application { |
478 acceptConnection(initiatorURL, serviceProvider) { | 478 acceptConnection(initiatorURL, serviceProvider) { |
479 function EchoServiceImpl(client) { | 479 function EchoServiceImpl(client) { |
480 this.echoString = function(s) { | 480 this.echoString = function(s) { |
(...skipping 20 matching lines...) Expand all Loading... |
501 | 501 |
502 The echo_share.js and echo_share_target.js applications demonstrate this. | 502 The echo_share.js and echo_share_target.js applications demonstrate this. |
503 | 503 |
504 ## Final note | 504 ## Final note |
505 | 505 |
506 An initiator's serviceProvider object can be retained and used to | 506 An initiator's serviceProvider object can be retained and used to |
507 request or provide services at any time, not just from within | 507 request or provide services at any time, not just from within |
508 application's acceptConnection() method. | 508 application's acceptConnection() method. |
509 | 509 |
510 | 510 |
OLD | NEW |