content_client.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
1// Copyright (c) 2012 The Chromium Authors. 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 CONTENT_PUBLIC_COMMON_CONTENT_CLIENT_H_
6#define CONTENT_PUBLIC_COMMON_CONTENT_CLIENT_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/strings/string16.h"
13#include "base/strings/string_piece.h"
14#include "build/build_config.h"
15#include "content/common/content_export.h"
16#include "ui/base/layout.h"
17
18class CommandLine;
19class GURL;
20
21namespace base {
22class RefCountedStaticMemory;
23}
24
25namespace IPC {
26class Message;
27}
28
29namespace gfx {
30class Image;
31}
32
33namespace gpu {
34struct GPUInfo;
35}
36
37namespace sandbox {
38class TargetPolicy;
39}
40
41namespace webkit {
42namespace npapi {
43class PluginList;
44}
45
46namespace ppapi {
47class HostGlobals;
48}
49}
50
51namespace content {
52
53class ContentBrowserClient;
54class ContentClient;
55class ContentPluginClient;
56class ContentRendererClient;
57class ContentUtilityClient;
58struct PepperPluginInfo;
59
60// Setter and getter for the client.  The client should be set early, before any
61// content code is called.
62CONTENT_EXPORT void SetContentClient(ContentClient* client);
63
64#if defined(CONTENT_IMPLEMENTATION)
65// Content's embedder API should only be used by content.
66ContentClient* GetContentClient();
67#endif
68
69// Used for tests to override the relevant embedder interfaces. Each method
70// returns the old value.
71CONTENT_EXPORT ContentBrowserClient* SetBrowserClientForTesting(
72    ContentBrowserClient* b);
73CONTENT_EXPORT ContentRendererClient* SetRendererClientForTesting(
74    ContentRendererClient* r);
75
76// Returns the user agent string being used by the browser. SetContentClient()
77// must be called prior to calling this, and this routine must be used
78// instead of webkit_glue::GetUserAgent() in order to ensure that we use
79// the same user agent string everywhere.
80// TODO(dpranke): This is caused by webkit_glue being a library that can
81// get linked into multiple linkable objects, causing us to have multiple
82// static values of the user agent. This will be fixed when we clean up
83// webkit_glue.
84CONTENT_EXPORT const std::string& GetUserAgent(const GURL& url);
85
86// Returns the PPAPI global singleton. See webkit/plugins/ppapi/host_globals.h
87// TODO(dpranke): Also needed since webkit_glue is a library.
88CONTENT_EXPORT webkit::ppapi::HostGlobals* GetHostGlobals();
89
90// Interface that the embedder implements.
91class CONTENT_EXPORT ContentClient {
92 public:
93  ContentClient();
94  virtual ~ContentClient();
95
96  ContentBrowserClient* browser() { return browser_; }
97  ContentPluginClient* plugin() { return plugin_; }
98  ContentRendererClient* renderer() { return renderer_; }
99  ContentUtilityClient* utility() { return utility_; }
100
101  // Sets the currently active URL.  Use GURL() to clear the URL.
102  virtual void SetActiveURL(const GURL& url) {}
103
104  // Sets the data on the current gpu.
105  virtual void SetGpuInfo(const gpu::GPUInfo& gpu_info) {}
106
107  // Gives the embedder a chance to register its own pepper plugins.
108  virtual void AddPepperPlugins(
109      std::vector<content::PepperPluginInfo>* plugins) {}
110
111  // Gives the embedder a chance to register its own internal NPAPI plugins.
112  virtual void AddNPAPIPlugins(
113      webkit::npapi::PluginList* plugin_list) {}
114
115  // Gives the embedder a chance to register its own standard and saveable
116  // url schemes early on in the startup sequence.
117  virtual void AddAdditionalSchemes(
118      std::vector<std::string>* standard_schemes,
119      std::vector<std::string>* savable_schemes) {}
120
121  // Returns whether the given message should be sent in a swapped out renderer.
122  virtual bool CanSendWhileSwappedOut(const IPC::Message* message);
123
124  // Returns whether the given message should be processed in the browser on
125  // behalf of a swapped out renderer.
126  virtual bool CanHandleWhileSwappedOut(const IPC::Message& message);
127
128  // Returns a string describing the embedder version.  Used as part of the
129  // user agent string.
130  virtual std::string GetProduct() const;
131
132  // Returns the user agent.
133  virtual std::string GetUserAgent() const;
134
135  // Returns a string resource given its id.
136  virtual string16 GetLocalizedString(int message_id) const;
137
138  // Return the contents of a resource in a StringPiece given the resource id.
139  virtual base::StringPiece GetDataResource(
140      int resource_id,
141      ui::ScaleFactor scale_factor) const;
142
143  // Returns the raw bytes of a scale independent data resource.
144  virtual base::RefCountedStaticMemory* GetDataResourceBytes(
145      int resource_id) const;
146
147  // Returns a native image given its id.
148  virtual gfx::Image& GetNativeImageNamed(int resource_id) const;
149
150  // Called by content::GetProcessTypeNameInEnglish for process types that it
151  // doesn't know about because they're from the embedder.
152  virtual std::string GetProcessTypeNameInEnglish(int type);
153
154#if defined(OS_MACOSX) && !defined(OS_IOS)
155  // Allows the embedder to define a new |sandbox_type| by mapping it to the
156  // resource ID corresponding to the sandbox profile to use. The legal values
157  // for |sandbox_type| are defined by the embedder and should start with
158  // SandboxType::SANDBOX_TYPE_AFTER_LAST_TYPE. Returns false if no sandbox
159  // profile for the given |sandbox_type| exists. Otherwise,
160  // |sandbox_profile_resource_id| is set to the resource ID corresponding to
161  // the sandbox profile to use and true is returned.
162  virtual bool GetSandboxProfileForSandboxType(
163      int sandbox_type,
164      int* sandbox_profile_resource_id) const;
165
166  // Gets the Carbon interposing path to give to DYLD. Returns an empty string
167  // if the embedder doesn't bundle it.
168  virtual std::string GetCarbonInterposePath() const;
169#endif
170
171 private:
172  friend class ContentClientInitializer;  // To set these pointers.
173  friend class InternalTestInitializer;
174
175  // The embedder API for participating in browser logic.
176  ContentBrowserClient* browser_;
177  // The embedder API for participating in plugin logic.
178  ContentPluginClient* plugin_;
179  // The embedder API for participating in renderer logic.
180  ContentRendererClient* renderer_;
181  // The embedder API for participating in utility logic.
182  ContentUtilityClient* utility_;
183};
184
185}  // namespace content
186
187#endif  // CONTENT_PUBLIC_COMMON_CONTENT_CLIENT_H_
188