1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "libcef/common/command_line_impl.h"
6
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10
CefCommandLineImpl(base::CommandLine * value,bool will_delete,bool read_only)11 CefCommandLineImpl::CefCommandLineImpl(base::CommandLine* value,
12 bool will_delete,
13 bool read_only)
14 : CefValueBase<CefCommandLine, base::CommandLine>(
15 value,
16 nullptr,
17 will_delete ? kOwnerWillDelete : kOwnerNoDelete,
18 read_only,
19 nullptr) {}
20
IsValid()21 bool CefCommandLineImpl::IsValid() {
22 return !detached();
23 }
24
IsReadOnly()25 bool CefCommandLineImpl::IsReadOnly() {
26 return read_only();
27 }
28
Copy()29 CefRefPtr<CefCommandLine> CefCommandLineImpl::Copy() {
30 CEF_VALUE_VERIFY_RETURN(false, nullptr);
31 return new CefCommandLineImpl(new base::CommandLine(const_value().argv()),
32 true, false);
33 }
34
InitFromArgv(int argc,const char * const * argv)35 void CefCommandLineImpl::InitFromArgv(int argc, const char* const* argv) {
36 #if !BUILDFLAG(IS_WIN)
37 CEF_VALUE_VERIFY_RETURN_VOID(true);
38 mutable_value()->InitFromArgv(argc, argv);
39 #else
40 NOTREACHED() << "method not supported on this platform";
41 #endif
42 }
43
InitFromString(const CefString & command_line)44 void CefCommandLineImpl::InitFromString(const CefString& command_line) {
45 #if BUILDFLAG(IS_WIN)
46 CEF_VALUE_VERIFY_RETURN_VOID(true);
47 const std::wstring& str16 = command_line;
48 mutable_value()->ParseFromString(str16);
49 #else
50 NOTREACHED() << "method not supported on this platform";
51 #endif
52 }
53
Reset()54 void CefCommandLineImpl::Reset() {
55 CEF_VALUE_VERIFY_RETURN_VOID(true);
56 base::CommandLine::StringVector argv;
57 argv.push_back(mutable_value()->GetProgram().value());
58 mutable_value()->InitFromArgv(argv);
59
60 const base::CommandLine::SwitchMap& map = mutable_value()->GetSwitches();
61 const_cast<base::CommandLine::SwitchMap*>(&map)->clear();
62 }
63
GetArgv(std::vector<CefString> & argv)64 void CefCommandLineImpl::GetArgv(std::vector<CefString>& argv) {
65 CEF_VALUE_VERIFY_RETURN_VOID(false);
66 const base::CommandLine::StringVector& cmd_argv = const_value().argv();
67 base::CommandLine::StringVector::const_iterator it = cmd_argv.begin();
68 for (; it != cmd_argv.end(); ++it)
69 argv.push_back(*it);
70 }
71
GetCommandLineString()72 CefString CefCommandLineImpl::GetCommandLineString() {
73 CEF_VALUE_VERIFY_RETURN(false, CefString());
74 return const_value().GetCommandLineString();
75 }
76
GetProgram()77 CefString CefCommandLineImpl::GetProgram() {
78 CEF_VALUE_VERIFY_RETURN(false, CefString());
79 return const_value().GetProgram().value();
80 }
81
SetProgram(const CefString & program)82 void CefCommandLineImpl::SetProgram(const CefString& program) {
83 CEF_VALUE_VERIFY_RETURN_VOID(true);
84 mutable_value()->SetProgram(base::FilePath(program));
85 }
86
HasSwitches()87 bool CefCommandLineImpl::HasSwitches() {
88 CEF_VALUE_VERIFY_RETURN(false, false);
89 return (const_value().GetSwitches().size() > 0);
90 }
91
HasSwitch(const CefString & name)92 bool CefCommandLineImpl::HasSwitch(const CefString& name) {
93 CEF_VALUE_VERIFY_RETURN(false, false);
94 return const_value().HasSwitch(base::ToLowerASCII(name.ToString()));
95 }
96
GetSwitchValue(const CefString & name)97 CefString CefCommandLineImpl::GetSwitchValue(const CefString& name) {
98 CEF_VALUE_VERIFY_RETURN(false, CefString());
99 return const_value().GetSwitchValueNative(
100 base::ToLowerASCII(name.ToString()));
101 }
102
GetSwitches(SwitchMap & switches)103 void CefCommandLineImpl::GetSwitches(SwitchMap& switches) {
104 CEF_VALUE_VERIFY_RETURN_VOID(false);
105 const base::CommandLine::SwitchMap& map = const_value().GetSwitches();
106 base::CommandLine::SwitchMap::const_iterator it = map.begin();
107 for (; it != map.end(); ++it)
108 switches.insert(std::make_pair(it->first, it->second));
109 }
110
AppendSwitch(const CefString & name)111 void CefCommandLineImpl::AppendSwitch(const CefString& name) {
112 CEF_VALUE_VERIFY_RETURN_VOID(true);
113 mutable_value()->AppendSwitch(name.ToString());
114 }
115
AppendSwitchWithValue(const CefString & name,const CefString & value)116 void CefCommandLineImpl::AppendSwitchWithValue(const CefString& name,
117 const CefString& value) {
118 CEF_VALUE_VERIFY_RETURN_VOID(true);
119 #if BUILDFLAG(IS_WIN)
120 mutable_value()->AppendSwitchNative(name.ToString(), value.ToWString());
121 #else
122 mutable_value()->AppendSwitchNative(name.ToString(), value.ToString());
123 #endif
124 }
125
HasArguments()126 bool CefCommandLineImpl::HasArguments() {
127 CEF_VALUE_VERIFY_RETURN(false, false);
128 return (const_value().GetArgs().size() > 0);
129 }
130
GetArguments(ArgumentList & arguments)131 void CefCommandLineImpl::GetArguments(ArgumentList& arguments) {
132 CEF_VALUE_VERIFY_RETURN_VOID(false);
133 const base::CommandLine::StringVector& vec = const_value().GetArgs();
134 base::CommandLine::StringVector::const_iterator it = vec.begin();
135 for (; it != vec.end(); ++it)
136 arguments.push_back(*it);
137 }
138
AppendArgument(const CefString & argument)139 void CefCommandLineImpl::AppendArgument(const CefString& argument) {
140 CEF_VALUE_VERIFY_RETURN_VOID(true);
141 #if BUILDFLAG(IS_WIN)
142 mutable_value()->AppendArgNative(argument.ToWString());
143 #else
144 mutable_value()->AppendArgNative(argument.ToString());
145 #endif
146 }
147
PrependWrapper(const CefString & wrapper)148 void CefCommandLineImpl::PrependWrapper(const CefString& wrapper) {
149 CEF_VALUE_VERIFY_RETURN_VOID(true);
150 #if BUILDFLAG(IS_WIN)
151 mutable_value()->PrependWrapper(wrapper.ToWString());
152 #else
153 mutable_value()->PrependWrapper(wrapper.ToString());
154 #endif
155 }
156
157 // CefCommandLine implementation.
158
159 // static
CreateCommandLine()160 CefRefPtr<CefCommandLine> CefCommandLine::CreateCommandLine() {
161 return new CefCommandLineImpl(
162 new base::CommandLine(base::CommandLine::NO_PROGRAM), true, false);
163 }
164
165 // static
GetGlobalCommandLine()166 CefRefPtr<CefCommandLine> CefCommandLine::GetGlobalCommandLine() {
167 // Uses a singleton reference object.
168 static CefRefPtr<CefCommandLineImpl> commandLinePtr;
169 if (!commandLinePtr.get()) {
170 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
171 if (command_line)
172 commandLinePtr = new CefCommandLineImpl(command_line, false, true);
173 }
174 return commandLinePtr.get();
175 }
176