• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 "multiprocess_func_list.h"
6 
7 #include <map>
8 
9 // Helper functions to maintain mapping of "test name"->test func.
10 // The information is accessed via a global map.
11 namespace multi_process_function_list {
12 
13 namespace {
14 
15 struct ProcessFunctions {
ProcessFunctionsmulti_process_function_list::__anonaf0a758d0111::ProcessFunctions16   ProcessFunctions() : main(NULL), setup(NULL) {}
ProcessFunctionsmulti_process_function_list::__anonaf0a758d0111::ProcessFunctions17   ProcessFunctions(TestMainFunctionPtr main, SetupFunctionPtr setup)
18       : main(main),
19         setup(setup) {
20   }
21   TestMainFunctionPtr main;
22   SetupFunctionPtr setup;
23 };
24 
25 typedef std::map<std::string, ProcessFunctions> MultiProcessTestMap;
26 
27 // Retrieve a reference to the global 'func name' -> func ptr map.
GetMultiprocessFuncMap()28 MultiProcessTestMap& GetMultiprocessFuncMap() {
29   static MultiProcessTestMap test_name_to_func_ptr_map;
30   return test_name_to_func_ptr_map;
31 }
32 
33 }  // namespace
34 
AppendMultiProcessTest(std::string test_name,TestMainFunctionPtr main_func_ptr,SetupFunctionPtr setup_func_ptr)35 AppendMultiProcessTest::AppendMultiProcessTest(
36     std::string test_name,
37     TestMainFunctionPtr main_func_ptr,
38     SetupFunctionPtr setup_func_ptr) {
39   GetMultiprocessFuncMap()[test_name] =
40       ProcessFunctions(main_func_ptr, setup_func_ptr);
41 }
42 
InvokeChildProcessTest(const std::string & test_name)43 int InvokeChildProcessTest(const std::string& test_name) {
44   MultiProcessTestMap& func_lookup_table = GetMultiprocessFuncMap();
45   MultiProcessTestMap::iterator it = func_lookup_table.find(test_name);
46   if (it != func_lookup_table.end()) {
47     const ProcessFunctions& process_functions = it->second;
48     if (process_functions.setup)
49       (*process_functions.setup)();
50     if (process_functions.main)
51       return (*process_functions.main)();
52   }
53 
54   return -1;
55 }
56 
57 }  // namespace multi_process_function_list
58