1 // Copyright 2013 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.
5 #include "base/command_line.h"
6 #include "base/debug/trace_event.h"
7 #include "base/logging.h"
8 #include "ui/base/cursor/ozone/cursor_factory_ozone.h"
9 #include "ui/events/ozone/event_factory_ozone.h"
10 #include "ui/gfx/ozone/surface_factory_ozone.h"
11 #include "ui/ozone/ime/input_method_context_factory_ozone.h"
12 #include "ui/ozone/ozone_platform.h"
13 #include "ui/ozone/ozone_platform_list.h"
14 #include "ui/ozone/ozone_switches.h"
20 // Helper to construct an OzonePlatform by name using the platform list.
21 OzonePlatform* CreatePlatform(const std::string& platform_name) {
22 // Search for a matching platform in the list.
23 for (int i = 0; i < kOzonePlatformCount; ++i)
24 if (platform_name == kOzonePlatforms[i].name)
25 return kOzonePlatforms[i].constructor();
27 LOG(FATAL) << "Invalid ozone platform: " << platform_name;
28 return NULL; // not reached
31 // Returns the name of the platform to use (value of --ozone-platform flag).
32 std::string GetPlatformName() {
33 // The first platform is the default.
34 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kOzonePlatform) &&
35 kOzonePlatformCount > 0)
36 return kOzonePlatforms[0].name;
37 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
38 switches::kOzonePlatform);
43 OzonePlatform::OzonePlatform() {}
45 OzonePlatform::~OzonePlatform() {
46 gfx::SurfaceFactoryOzone::SetInstance(NULL);
47 ui::EventFactoryOzone::SetInstance(NULL);
48 ui::CursorFactoryOzone::SetInstance(NULL);
52 void OzonePlatform::Initialize() {
56 std::string platform = GetPlatformName();
58 TRACE_EVENT1("ozone", "OzonePlatform::Initialize", "platform", platform);
60 instance_ = CreatePlatform(platform);
62 // Inject ozone interfaces.
63 gfx::SurfaceFactoryOzone::SetInstance(instance_->GetSurfaceFactoryOzone());
64 ui::EventFactoryOzone::SetInstance(instance_->GetEventFactoryOzone());
65 ui::InputMethodContextFactoryOzone::SetInstance(
66 instance_->GetInputMethodContextFactoryOzone());
67 ui::CursorFactoryOzone::SetInstance(instance_->GetCursorFactoryOzone());
71 OzonePlatform* OzonePlatform::GetInstance() {
72 CHECK(instance_) << "OzonePlatform is not initialized";
77 OzonePlatform* OzonePlatform::instance_;