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 // Stages of main parts. See content/browser_main_parts.h for description.
77 virtual void PreMainMessageLoopRun();
78 virtual void PostMainMessageLoopRun();
83 // These two hooks should be used to add new port specific
84 // components. Subclasses *must* call the base class implementation.
85 virtual void CreateComponents();
86 virtual void DestroyComponents();
88 // Should be used by CreateComponents() implementations.
89 void AddComponent(scoped_ptr<XWalkComponent> component);
91 // These specific factory functions are used to allow ports to customize
93 virtual scoped_ptr<ApplicationComponent> CreateAppComponent();
94 virtual scoped_ptr<SysAppsComponent> CreateSysAppsComponent();
95 virtual scoped_ptr<StorageComponent> CreateStorageComponent();
98 friend class XWalkMainDelegate;
99 friend class ::XWalkTestSuiteInitializer;
101 // To track OnRenderProcessHostGone.
102 friend class application::Application;
104 // This class acts as an "arm" of XWalkRunner to fulfill Content API needs,
105 // it may call us back in some situations where the a more wider view of the
106 // objects is necessary, e.g. during render process lifecycle callbacks.
107 friend class XWalkContentBrowserClient;
109 // We track the render process lifecycle to register Crosswalk
110 // extensions. Some subsystems are mostly implemented using extensions.
111 void OnRenderProcessWillLaunch(content::RenderProcessHost* host);
112 void OnRenderProcessHostGone(content::RenderProcessHost* host);
114 // Create the XWalkRunner object. We use a factory function so that we can
115 // switch the concrete class on compile time based on the platform, separating
116 // the per-platform behavior and data in the subclasses.
117 static scoped_ptr<XWalkRunner> Create();
119 // Note: this is not public as we want to discourage the rest of Crosswalk to
120 // rely directly on this object.
121 content::ContentBrowserClient* GetContentBrowserClient();
123 scoped_ptr<XWalkContentBrowserClient> content_browser_client_;
124 scoped_ptr<RuntimeContext> runtime_context_;
125 scoped_ptr<extensions::XWalkExtensionService> extension_service_;
126 scoped_ptr<XWalkAppExtensionBridge> app_extension_bridge_;
128 // XWalkRunner uses the XWalkComponent interface to be able to handle
129 // different subsystems and call them in specific situations, e.g. when
130 // extensions need to be created.
131 ScopedVector<XWalkComponent> components_;
133 ApplicationComponent* app_component_;
135 // These variables are used to export some values from the browser process
136 // side to the extension side, such as application IDs and whatnot.
137 void InitializeRuntimeVariablesForExtensions(
138 const content::RenderProcessHost* host,
139 base::ValueMap* runtime_variables);
141 DISALLOW_COPY_AND_ASSIGN(XWalkRunner);
146 #endif // XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_