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;
25 class ApplicationComponent;
26 class RemoteDebuggingServer;
27 class SysAppsComponent;
28 class XWalkBrowserContext;
30 class XWalkContentBrowserClient;
31 class XWalkAppExtensionBridge;
33 namespace application {
35 class ApplicationSystem;
38 namespace extensions {
39 class XWalkExtensionService;
42 // Main object for the Browser Process execution in Crosswalk. It is created and
43 // owned by XWalkMainDelegate. It's role is to own, setup and teardown all the
44 // subsystems of Crosswalk.
47 // Read the comments below before using this. Relying too much on this
48 // accessor makes the code harder to change and harder to reason about.
49 static XWalkRunner* GetInstance();
51 virtual ~XWalkRunner();
53 // All sub objects should have their dependencies passed during their
54 // initialization, so that these accessors below are not frequently accessed.
55 // Instead of calling these, consider explicitly passing the dependencies
56 // to the objects that need them.
58 // For example, if "Application System" needs to use "Runtime Context", we
59 // should pass the "Runtime Context" to "Application System" instead of
60 // making "Application System" ask XWalkRunner for its dependency.
62 // Scenarios when it is fine to use the accessors:
64 // - Prototyping solutions, in which we want to see the solution working, and
65 // all dependencies are still not clear. It avoids writing down a lot of
66 // code just to test something out.
68 // - In situations where you don't control the creation of a certain
69 // object. Certain APIs doesn't allow us to pass the dependencies, so we
70 // need to reach them some way.
71 XWalkBrowserContext* browser_context() { return browser_context_.get(); }
72 application::ApplicationSystem* app_system();
73 extensions::XWalkExtensionService* extension_service() {
74 return extension_service_.get();
77 // Stages of main parts. See content/browser_main_parts.h for description.
78 virtual void PreMainMessageLoopRun();
79 virtual void PostMainMessageLoopRun();
81 void EnableRemoteDebugging(int port);
82 void DisableRemoteDebugging();
84 bool shared_process_mode_enabled() const
85 { return shared_process_mode_enabled_; }
90 // These two hooks should be used to add new port specific
91 // components. Subclasses *must* call the base class implementation.
92 virtual void CreateComponents();
93 virtual void DestroyComponents();
95 // Should be used by CreateComponents() implementations.
96 void AddComponent(scoped_ptr<XWalkComponent> component);
98 // These specific factory functions are used to allow ports to customize
100 virtual scoped_ptr<ApplicationComponent> CreateAppComponent();
101 virtual scoped_ptr<SysAppsComponent> CreateSysAppsComponent();
102 virtual scoped_ptr<StorageComponent> CreateStorageComponent();
104 bool shared_process_mode_enabled_;
107 friend class XWalkMainDelegate;
108 friend class ::XWalkTestSuiteInitializer;
110 // To track OnRenderProcessHostGone.
111 friend class application::Application;
113 // This class acts as an "arm" of XWalkRunner to fulfill Content API needs,
114 // it may call us back in some situations where the a more wider view of the
115 // objects is necessary, e.g. during render process lifecycle callbacks.
116 friend class XWalkContentBrowserClient;
118 // We track the render process lifecycle to register Crosswalk
119 // extensions. Some subsystems are mostly implemented using extensions.
120 void OnRenderProcessWillLaunch(content::RenderProcessHost* host);
121 void OnRenderProcessHostGone(content::RenderProcessHost* host);
123 // Create the XWalkRunner object. We use a factory function so that we can
124 // switch the concrete class on compile time based on the platform, separating
125 // the per-platform behavior and data in the subclasses.
126 static scoped_ptr<XWalkRunner> Create();
128 // Note: this is not public as we want to discourage the rest of Crosswalk to
129 // rely directly on this object.
130 content::ContentBrowserClient* GetContentBrowserClient();
132 scoped_ptr<XWalkContentBrowserClient> content_browser_client_;
133 scoped_ptr<XWalkBrowserContext> browser_context_;
134 scoped_ptr<extensions::XWalkExtensionService> extension_service_;
135 scoped_ptr<XWalkAppExtensionBridge> app_extension_bridge_;
137 // XWalkRunner uses the XWalkComponent interface to be able to handle
138 // different subsystems and call them in specific situations, e.g. when
139 // extensions need to be created.
140 ScopedVector<XWalkComponent> components_;
142 ApplicationComponent* app_component_;
144 // Remote debugger server.
145 scoped_ptr<RemoteDebuggingServer> remote_debugging_server_;
147 // These variables are used to export some values from the browser process
148 // side to the extension side, such as application IDs and whatnot.
149 void InitializeRuntimeVariablesForExtensions(
150 const content::RenderProcessHost* host,
151 base::ValueMap* runtime_variables);
153 DISALLOW_COPY_AND_ASSIGN(XWalkRunner);
158 #endif // XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_