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