Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / ozone_platform.cc
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.
4
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"
15
16 namespace ui {
17
18 namespace {
19
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();
26
27   LOG(FATAL) << "Invalid ozone platform: " << platform_name;
28   return NULL;  // not reached
29 }
30
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);
39 }
40
41 }  // namespace
42
43 OzonePlatform::OzonePlatform() {}
44
45 OzonePlatform::~OzonePlatform() {
46   gfx::SurfaceFactoryOzone::SetInstance(NULL);
47   ui::EventFactoryOzone::SetInstance(NULL);
48   ui::CursorFactoryOzone::SetInstance(NULL);
49 }
50
51 // static
52 void OzonePlatform::Initialize() {
53   if (instance_)
54     return;
55
56   std::string platform = GetPlatformName();
57
58   TRACE_EVENT1("ozone", "OzonePlatform::Initialize", "platform", platform);
59
60   instance_ = CreatePlatform(platform);
61
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());
68 }
69
70 // static
71 OzonePlatform* OzonePlatform::GetInstance() {
72   CHECK(instance_) << "OzonePlatform is not initialized";
73   return instance_;
74 }
75
76 // static
77 OzonePlatform* OzonePlatform::instance_;
78
79 }  // namespace ui