diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-05 20:32:22 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-05 20:32:22 +0000 |
commit | 0a8b97bce41cba47185d5fbee6a6850f175e9513 (patch) | |
tree | 2e774a8aac4ab7b0b52912216e04d46a2e0688b2 | |
parent | a43ce683b98659b5120125f04b07d8db81979f7b (diff) | |
download | chromium_src-0a8b97bce41cba47185d5fbee6a6850f175e9513.zip chromium_src-0a8b97bce41cba47185d5fbee6a6850f175e9513.tar.gz chromium_src-0a8b97bce41cba47185d5fbee6a6850f175e9513.tar.bz2 |
gtk: refactor copy-pasted code
I wanted to do the same thing in a third place.
TEST=compiles
Review URL: http://codereview.chromium.org/4508004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65246 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/gpu/gpu_thread.cc | 18 | ||||
-rw-r--r-- | chrome/plugin/plugin_thread.cc | 18 | ||||
-rw-r--r-- | gfx/gtk_util.cc | 19 | ||||
-rw-r--r-- | gfx/gtk_util.h | 6 |
4 files changed, 29 insertions, 32 deletions
diff --git a/chrome/gpu/gpu_thread.cc b/chrome/gpu/gpu_thread.cc index 8c1059f..85aea62 100644 --- a/chrome/gpu/gpu_thread.cc +++ b/chrome/gpu/gpu_thread.cc @@ -15,6 +15,7 @@ #include "chrome/common/gpu_info.h" #include "chrome/common/gpu_messages.h" #include "chrome/gpu/gpu_info_collector.h" +#include "gfx/gtk_util.h" #include "ipc/ipc_channel_handle.h" #if defined(OS_WIN) @@ -43,22 +44,7 @@ GpuThread::GpuThread() { // rethink whether initializing Gtk is really necessary or whether we // should just send the display connection down to the GPUProcessor. g_thread_init(NULL); - const std::vector<std::string>& args = - CommandLine::ForCurrentProcess()->argv(); - int argc = args.size(); - scoped_array<char *> argv(new char *[argc + 1]); - for (size_t i = 0; i < args.size(); ++i) { - // TODO(piman@google.com): can gtk_init modify argv? Just being safe - // here. - argv[i] = strdup(args[i].c_str()); - } - argv[argc] = NULL; - char **argv_pointer = argv.get(); - - gtk_init(&argc, &argv_pointer); - for (size_t i = 0; i < args.size(); ++i) { - free(argv[i]); - } + gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess()); x11_util::SetDefaultX11ErrorHandlers(); } #endif diff --git a/chrome/plugin/plugin_thread.cc b/chrome/plugin/plugin_thread.cc index 0e169e4..588c532 100644 --- a/chrome/plugin/plugin_thread.cc +++ b/chrome/plugin/plugin_thread.cc @@ -27,6 +27,7 @@ #include "chrome/plugin/chrome_plugin_host.h" #include "chrome/plugin/npobject_util.h" #include "chrome/renderer/render_thread.h" +#include "gfx/gtk_util.h" #include "ipc/ipc_channel_handle.h" #include "net/base/net_errors.h" #include "webkit/glue/plugins/plugin_lib.h" @@ -58,33 +59,18 @@ PluginThread::PluginThread() // XEmbed plugins assume they are hosted in a Gtk application, so we need // to initialize Gtk in the plugin process. g_thread_init(NULL); - const std::vector<std::string>& args = - CommandLine::ForCurrentProcess()->argv(); - int argc = args.size(); - scoped_array<char *> argv(new char *[argc + 1]); - for (size_t i = 0; i < args.size(); ++i) { - // TODO(piman@google.com): can gtk_init modify argv? Just being safe - // here. - argv[i] = strdup(args[i].c_str()); - } - argv[argc] = NULL; - char **argv_pointer = argv.get(); // Flash has problems receiving clicks with newer GTKs due to the // client-side windows change. To be safe, we just always set the // backwards-compat environment variable. setenv("GDK_NATIVE_WINDOWS", "1", 1); - gtk_init(&argc, &argv_pointer); + gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess()); // GTK after 2.18 resets the environment variable. But if we're using // nspluginwrapper, that means it'll spawn its subprocess without the // environment variable! So set it again. setenv("GDK_NATIVE_WINDOWS", "1", 1); - - for (size_t i = 0; i < args.size(); ++i) { - free(argv[i]); - } } x11_util::SetDefaultX11ErrorHandlers(); diff --git a/gfx/gtk_util.cc b/gfx/gtk_util.cc index c45e133..b30b827 100644 --- a/gfx/gtk_util.cc +++ b/gfx/gtk_util.cc @@ -9,6 +9,7 @@ #include <stdlib.h> #include "base/basictypes.h" +#include "base/command_line.h" #include "base/linux_util.h" #include "gfx/rect.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -52,6 +53,24 @@ std::string ConvertAmperstandsTo(const std::string& label, namespace gfx { +void GtkInitFromCommandLine(const CommandLine& command_line) { + const std::vector<std::string>& args = command_line.argv(); + int argc = args.size(); + scoped_array<char *> argv(new char *[argc + 1]); + for (size_t i = 0; i < args.size(); ++i) { + // TODO(piman@google.com): can gtk_init modify argv? Just being safe + // here. + argv[i] = strdup(args[i].c_str()); + } + argv[argc] = NULL; + char **argv_pointer = argv.get(); + + gtk_init(&argc, &argv_pointer); + for (size_t i = 0; i < args.size(); ++i) { + free(argv[i]); + } +} + GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap* bitmap) { if (bitmap->isNull()) return NULL; diff --git a/gfx/gtk_util.h b/gfx/gtk_util.h index 5c4ad94..8f9618d 100644 --- a/gfx/gtk_util.h +++ b/gfx/gtk_util.h @@ -17,12 +17,18 @@ typedef struct _GdkPixbuf GdkPixbuf; typedef struct _GdkRegion GdkRegion; +class CommandLine; class SkBitmap; namespace gfx { class Rect; +// Call gtk_init() using the argc and argv from command_line. +// gtk_init() wants an argc and argv that it can mutate; we provide those, +// but leave the original CommandLine unaltered. +void GtkInitFromCommandLine(const CommandLine& command_line); + // Convert and copy a SkBitmap to a GdkPixbuf. NOTE: this uses BGRAToRGBA, so // it is an expensive operation. The returned GdkPixbuf will have a refcount of // 1, and the caller is responsible for unrefing it when done. |