OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "shell/out_of_process_native_runner.h" | 5 #include "shell/out_of_process_native_runner.h" |
6 | 6 |
7 #include <elf.h> | 7 #include <elf.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "shell/child_controller.mojom.h" | 15 #include "shell/child_controller.mojom.h" |
16 #include "shell/child_process_host.h" | 16 #include "shell/child_process_host.h" |
17 #include "shell/in_process_native_runner.h" | 17 #include "shell/in_process_native_runner.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Determines if content handler must be run as 32 bit application | 21 // Determines if content handler must be run as 32 bit application based on the |
22 // based on elf header. Returns false on error. | 22 // ELF header. Defaults to false on error. |
23 bool Require32Bit(const base::FilePath& app_path) { | 23 bool Require32Bit(const base::FilePath& app_path) { |
24 if (sizeof(void*) == 4) { | 24 if (sizeof(void*) == 4) { |
25 // CPU arch is already 32 bits | 25 // CPU arch is already 32 bits. |
26 return true; | 26 return true; |
27 } else { | |
28 char data[EI_NIDENT]; | |
29 // Read e_ident from the elf file | |
30 if (sizeof(data) != ReadFile(app_path, data, sizeof(data))) { | |
31 DCHECK(false); | |
32 return false; | |
33 } | |
34 // Check the magic elf number | |
35 if (memcmp(data, ELFMAG, SELFMAG)) { | |
36 DCHECK(false); | |
37 return false; | |
38 } | |
39 // Identify the architecture required | |
40 return data[EI_CLASS] == ELFCLASS32; | |
41 } | 27 } |
| 28 |
| 29 char data[EI_NIDENT]; |
| 30 // Read e_ident from the ELF file. |
| 31 if (ReadFile(app_path, data, sizeof(data)) != sizeof(data)) { |
| 32 DCHECK(false) << "Failed to read ELF header"; |
| 33 return false; |
| 34 } |
| 35 // Check the magic ELF number. |
| 36 if (memcmp(data, ELFMAG, SELFMAG)) { |
| 37 DCHECK(false) << "Not an ELF file?"; |
| 38 return false; |
| 39 } |
| 40 // Identify the architecture required. |
| 41 return data[EI_CLASS] == ELFCLASS32; |
42 } | 42 } |
43 | 43 |
44 } // namespace | 44 } // namespace |
45 | 45 |
46 namespace shell { | 46 namespace shell { |
47 | 47 |
48 OutOfProcessNativeRunner::OutOfProcessNativeRunner(Context* context) | 48 OutOfProcessNativeRunner::OutOfProcessNativeRunner( |
49 : context_(context) { | 49 Context* context, |
50 } | 50 const NativeApplicationOptions& options) |
| 51 : context_(context), options_(options) {} |
51 | 52 |
52 OutOfProcessNativeRunner::~OutOfProcessNativeRunner() { | 53 OutOfProcessNativeRunner::~OutOfProcessNativeRunner() { |
53 if (child_process_host_) { | 54 if (child_process_host_) { |
54 // TODO(vtl): Race condition: If |ChildProcessHost::DidStart()| hasn't been | 55 // TODO(vtl): Race condition: If |ChildProcessHost::DidStart()| hasn't been |
55 // called yet, we shouldn't call |Join()| here. (Until |DidStart()|, we may | 56 // called yet, we shouldn't call |Join()| here. (Until |DidStart()|, we may |
56 // not have a child process to wait on.) Probably we should fix |Join()|. | 57 // not have a child process to wait on.) Probably we should fix |Join()|. |
57 child_process_host_->Join(); | 58 child_process_host_->Join(); |
58 } | 59 } |
59 } | 60 } |
60 | 61 |
61 void OutOfProcessNativeRunner::Start( | 62 void OutOfProcessNativeRunner::Start( |
62 const base::FilePath& app_path, | 63 const base::FilePath& app_path, |
63 mojo::InterfaceRequest<mojo::Application> application_request, | 64 mojo::InterfaceRequest<mojo::Application> application_request, |
64 const base::Closure& app_completed_callback) { | 65 const base::Closure& app_completed_callback) { |
65 app_path_ = app_path; | 66 app_path_ = app_path; |
66 | 67 |
67 DCHECK(app_completed_callback_.is_null()); | 68 DCHECK(app_completed_callback_.is_null()); |
68 app_completed_callback_ = app_completed_callback; | 69 app_completed_callback_ = app_completed_callback; |
69 | 70 |
70 child_process_host_.reset(new ChildProcessHost(context_)); | 71 child_process_host_.reset(new ChildProcessHost(context_)); |
71 child_process_host_->Start(Require32Bit(app_path)); | 72 |
| 73 NativeApplicationOptions options = options_; |
| 74 if (Require32Bit(app_path)) |
| 75 options.require_32_bit = true; |
| 76 child_process_host_->Start(options); |
72 | 77 |
73 // TODO(vtl): |app_path.AsUTF8Unsafe()| is unsafe. | 78 // TODO(vtl): |app_path.AsUTF8Unsafe()| is unsafe. |
74 child_process_host_->StartApp( | 79 child_process_host_->StartApp( |
75 app_path.AsUTF8Unsafe(), application_request.Pass(), | 80 app_path.AsUTF8Unsafe(), application_request.Pass(), |
76 base::Bind(&OutOfProcessNativeRunner::AppCompleted, | 81 base::Bind(&OutOfProcessNativeRunner::AppCompleted, |
77 base::Unretained(this))); | 82 base::Unretained(this))); |
78 } | 83 } |
79 | 84 |
80 void OutOfProcessNativeRunner::AppCompleted(int32_t result) { | 85 void OutOfProcessNativeRunner::AppCompleted(int32_t result) { |
81 DVLOG(2) << "OutOfProcessNativeRunner::AppCompleted(" << result << ")"; | 86 DVLOG(2) << "OutOfProcessNativeRunner::AppCompleted(" << result << ")"; |
82 | 87 |
83 if (child_process_host_) { | 88 if (child_process_host_) { |
84 child_process_host_->Join(); | 89 child_process_host_->Join(); |
85 child_process_host_.reset(); | 90 child_process_host_.reset(); |
86 } | 91 } |
87 // This object may be deleted by this callback. | 92 // This object may be deleted by this callback. |
88 base::Closure app_completed_callback = app_completed_callback_; | 93 base::Closure app_completed_callback = app_completed_callback_; |
89 app_completed_callback_.Reset(); | 94 app_completed_callback_.Reset(); |
90 app_completed_callback.Run(); | 95 app_completed_callback.Run(); |
91 } | 96 } |
92 | 97 |
93 scoped_ptr<NativeRunner> OutOfProcessNativeRunnerFactory::Create( | 98 scoped_ptr<NativeRunner> OutOfProcessNativeRunnerFactory::Create( |
94 const Options& options) { | 99 const NativeApplicationOptions& options) { |
95 if (options.force_in_process) | 100 if (options.force_in_process) |
96 return make_scoped_ptr(new InProcessNativeRunner(context_)); | 101 return make_scoped_ptr(new InProcessNativeRunner(context_)); |
97 | 102 |
98 return make_scoped_ptr(new OutOfProcessNativeRunner(context_)); | 103 return make_scoped_ptr(new OutOfProcessNativeRunner(context_, options)); |
99 } | 104 } |
100 | 105 |
101 } // namespace shell | 106 } // namespace shell |
OLD | NEW |