Upstream version 11.39.264.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_runner.h
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.
4
5 #ifndef XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_
6 #define XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_
7
8 #include <string>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/values.h"
13
14 #include "xwalk/runtime/browser/storage_component.h"
15
16 namespace content {
17 class ContentBrowserClient;
18 class RenderProcessHost;
19 }
20
21 class XWalkTestSuiteInitializer;
22
23 namespace xwalk {
24
25 class ApplicationComponent;
26 class RemoteDebuggingServer;
27 class SysAppsComponent;
28 class XWalkBrowserContext;
29 class XWalkComponent;
30 class XWalkContentBrowserClient;
31 class XWalkAppExtensionBridge;
32
33 namespace application {
34 class Application;
35 class ApplicationSystem;
36 }
37
38 namespace extensions {
39 class XWalkExtensionService;
40 };
41
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.
45 class XWalkRunner {
46  public:
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();
50
51   virtual ~XWalkRunner();
52
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.
57   //
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.
61   //
62   // Scenarios when it is fine to use the accessors:
63   //
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.
67   //
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();
75   }
76
77   // Stages of main parts. See content/browser_main_parts.h for description.
78   virtual void PreMainMessageLoopRun();
79   virtual void PostMainMessageLoopRun();
80
81   void EnableRemoteDebugging(int port);
82   void DisableRemoteDebugging();
83
84   bool shared_process_mode_enabled() const
85       { return shared_process_mode_enabled_; }
86
87  protected:
88   XWalkRunner();
89
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();
94
95   // Should be used by CreateComponents() implementations.
96   void AddComponent(scoped_ptr<XWalkComponent> component);
97
98   // These specific factory functions are used to allow ports to customize
99   // components.
100   virtual scoped_ptr<ApplicationComponent> CreateAppComponent();
101   virtual scoped_ptr<SysAppsComponent> CreateSysAppsComponent();
102   virtual scoped_ptr<StorageComponent> CreateStorageComponent();
103
104   bool shared_process_mode_enabled_;
105
106  private:
107   friend class XWalkMainDelegate;
108   friend class ::XWalkTestSuiteInitializer;
109
110   // To track OnRenderProcessHostGone.
111   friend class application::Application;
112
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;
117
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);
122
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();
127
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();
131
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_;
136
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_;
141
142   ApplicationComponent* app_component_;
143
144   // Remote debugger server.
145   scoped_ptr<RemoteDebuggingServer> remote_debugging_server_;
146
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);
152
153   DISALLOW_COPY_AND_ASSIGN(XWalkRunner);
154 };
155
156 }  // namespace xwalk
157
158 #endif  // XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_