1 // Copyright (c) 2013 Intel Corporation. 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.
5 #ifndef XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_
6 #define XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/values.h"
14 #include "xwalk/runtime/browser/storage_component.h"
17 class ContentBrowserClient;
18 class RenderProcessHost;
21 class XWalkTestSuiteInitializer;
26 class ApplicationComponent;
27 class SysAppsComponent;
29 class XWalkContentBrowserClient;
30 class XWalkAppExtensionBridge;
32 namespace application {
34 class ApplicationSystem;
37 namespace extensions {
38 class XWalkExtensionService;
41 // Main object for the Browser Process execution in Crosswalk. It is created and
42 // owned by XWalkMainDelegate. It's role is to own, setup and teardown all the
43 // subsystems of Crosswalk.
46 // Read the comments below before using this. Relying too much on this
47 // accessor makes the code harder to change and harder to reason about.
48 static XWalkRunner* GetInstance();
50 virtual ~XWalkRunner();
52 // All sub objects should have their dependencies passed during their
53 // initialization, so that these accessors below are not frequently accessed.
54 // Instead of calling these, consider explicitly passing the dependencies
55 // to the objects that need them.
57 // For example, if "Application System" needs to use "Runtime Context", we
58 // should pass the "Runtime Context" to "Application System" instead of
59 // making "Application System" ask XWalkRunner for its dependency.
61 // Scenarios when it is fine to use the accessors:
63 // - Prototyping solutions, in which we want to see the solution working, and
64 // all dependencies are still not clear. It avoids writing down a lot of
65 // code just to test something out.
67 // - In situations where you don't control the creation of a certain
68 // object. Certain APIs doesn't allow us to pass the dependencies, so we
69 // need to reach them some way.
70 RuntimeContext* runtime_context() { return runtime_context_.get(); }
71 application::ApplicationSystem* app_system();
72 extensions::XWalkExtensionService* extension_service() {
73 return extension_service_.get();
76 // Return true if Crosswalk is running in service mode, i.e. taking
77 // requests from native IPC mechanism to launch applications.
78 bool is_running_as_service() const { return is_running_as_service_; }
80 // Stages of main parts. See content/browser_main_parts.h for description.
81 virtual void PreMainMessageLoopRun();
82 virtual void PostMainMessageLoopRun();
84 // Get the latest application locale from system.
85 // locale is a langtag defined in [BCP47]
86 virtual std::string GetLocale() const;
91 // These two hooks should be used to add new port specific
92 // components. Subclasses *must* call the base class implementation.
93 virtual void CreateComponents();
94 virtual void DestroyComponents();
96 // Should be used by CreateComponents() implementations.
97 void AddComponent(scoped_ptr<XWalkComponent> component);
99 // These specific factory functions are used to allow ports to customize
101 virtual scoped_ptr<ApplicationComponent> CreateAppComponent();
102 virtual scoped_ptr<SysAppsComponent> CreateSysAppsComponent();
103 virtual scoped_ptr<StorageComponent> CreateStorageComponent();
106 friend class XWalkMainDelegate;
107 friend class ::XWalkTestSuiteInitializer;
109 // To track OnRenderProcessHostGone.
110 friend class application::Application;
112 // This class acts as an "arm" of XWalkRunner to fulfill Content API needs,
113 // it may call us back in some situations where the a more wider view of the
114 // objects is necessary, e.g. during render process lifecycle callbacks.
115 friend class XWalkContentBrowserClient;
117 // We track the render process lifecycle to register Crosswalk
118 // extensions. Some subsystems are mostly implemented using extensions.
119 void OnRenderProcessWillLaunch(content::RenderProcessHost* host);
120 void OnRenderProcessHostGone(content::RenderProcessHost* host);
122 // Create the XWalkRunner object. We use a factory function so that we can
123 // switch the concrete class on compile time based on the platform, separating
124 // the per-platform behavior and data in the subclasses.
125 static scoped_ptr<XWalkRunner> Create();
127 // Note: this is not public as we want to discourage the rest of Crosswalk to
128 // rely directly on this object.
129 content::ContentBrowserClient* GetContentBrowserClient();
131 scoped_ptr<XWalkContentBrowserClient> content_browser_client_;
132 scoped_ptr<RuntimeContext> runtime_context_;
133 scoped_ptr<extensions::XWalkExtensionService> extension_service_;
134 scoped_ptr<XWalkAppExtensionBridge> app_extension_bridge_;
136 // XWalkRunner uses the XWalkComponent interface to be able to handle
137 // different subsystems and call them in specific situations, e.g. when
138 // extensions need to be created.
139 ScopedVector<XWalkComponent> components_;
141 ApplicationComponent* app_component_;
143 bool is_running_as_service_;
145 // These variables are used to export some values from the browser process
146 // side to the extension side, such as application IDs and whatnot.
147 void InitializeRuntimeVariablesForExtensions(
148 const content::RenderProcessHost* host,
149 base::ValueMap& runtime_variables);
151 DISALLOW_COPY_AND_ASSIGN(XWalkRunner);
156 #endif // XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_