11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef MOJO_APPS_JS_JS_APP_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define MOJO_APPS_JS_JS_APP_H_
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/memory/ref_counted.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/single_thread_task_runner.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/threading/thread.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "gin/public/isolate_holder.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "gin/shell_runner.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/apps/js/mojo_runner_delegate.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/services/public/interfaces/content_handler/content_handler.mojom.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace mojo {
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace apps {
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass JSApp;
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass ApplicationDelegateImpl;
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Each JavaScript app started by content handler runs on its own thread and
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// in its own V8 isolate. This class represents one running JS app.
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// The lifetime of this class is defined by the content handler's internal
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// "app_vector" list.
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass JSApp {
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci public:
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  JSApp(ApplicationDelegateImpl* content_handler_app,
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        const std::string& url,
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        URLResponsePtr content);
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ~JSApp();
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool Start();
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void Quit();
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Handle ConnectToService(const std::string& application_url,
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                          const std::string& interface_name);
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci private:
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void Run();
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void Terminate();
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Used to CHECK that we're running on the correct thread.
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool on_content_handler_thread() const;
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool on_js_app_thread() const;
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ApplicationDelegateImpl* content_handler_app_;
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string url_;
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  URLResponsePtr content_;
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::Thread thread_;
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_refptr<base::SingleThreadTaskRunner> content_handler_task_runner_;
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_refptr<base::SingleThreadTaskRunner> js_app_task_runner_;
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  MojoRunnerDelegate runner_delegate_;
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<gin::IsolateHolder> isolate_holder_;
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<gin::ShellRunner> shell_runner_;
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DISALLOW_COPY_AND_ASSIGN(JSApp);
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace apps
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace mojo
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // MOJO_APPS_JS_JS_APP_H_
65