summaryrefslogtreecommitdiffstats
path: root/chrome/test/chromedriver/chromedriver_shared_library.cc
blob: ba158dbc32202bfce7780f5031dd2cad6bf59fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cstring>
#include <string>

#include "base/at_exit.h"
#include "base/memory/scoped_ptr.h"
#include "base/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/test/chromedriver/chromedriver.h"
#include "chrome/test/chromedriver/command_executor.h"
#include "chrome/test/chromedriver/command_executor_impl.h"

#if defined(OS_WIN)
#include <windows.h>
#endif

#if defined(OS_WIN)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __attribute__((visibility("default")))
#endif

namespace {

base::AtExitManager* g_at_exit = NULL;

}  // namespace

extern "C" {

// Synchronously executes the given command. Thread safe.
// Every call dynamically allocates a |response| string, which must be
// deallocated by calling |Free|. This |response| string is not guaranteed
// to be null terminated, and may contain null characters within. You
// should use the given response size to construct the string.
void EXPORT ExecuteCommand(
    const char* command,
    unsigned int command_size,
    char** response,
    unsigned int* response_size) {
  std::string command_str(command, command_size);
  std::string json;
  ExecuteCommand(command_str, &json);
  *response = new char[json.length()];
  std::memcpy(*response, json.c_str(), json.length());
  *response_size = json.length();
}

void EXPORT Free(char* p) {
  delete [] p;
}

}  // extern "C"

#if defined(OS_WIN)
BOOL APIENTRY DllMain(HMODULE module, DWORD reason, void* reserved) {
  if (reason == DLL_PROCESS_ATTACH) {
    g_at_exit = new base::AtExitManager();
    Init(scoped_ptr<CommandExecutor>(new CommandExecutorImpl()));
  }
  if (reason == DLL_PROCESS_DETACH) {
    // If |reserved| is not null, the process is terminating and
    // ChromeDriver's threads have already been signaled to exit.
    // Just leak and let the OS clean it up.
    if (reserved)
      return TRUE;
    Shutdown();
    delete g_at_exit;
  }
  return TRUE;
}
#else
void __attribute__((constructor)) OnLoad(void) {
  g_at_exit = new base::AtExitManager();
  Init(scoped_ptr<CommandExecutor>(new CommandExecutorImpl()));
}
void __attribute__((destructor)) OnUnload(void) {
  Shutdown();
  delete g_at_exit;
}
#endif