1// Copyright (c) 2011 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#import <AppKit/AppKit.h>
6
7#include "base/environment.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/strings/string_util.h"
10#include "content/common/plugin_carbon_interpose_constants_mac.h"
11#include "content/plugin/plugin_interpose_util_mac.h"
12#include "content/public/common/content_client.h"
13
14namespace content {
15
16#if !defined(__LP64__)
17void TrimInterposeEnvironment() {
18  scoped_ptr<base::Environment> env(base::Environment::Create());
19
20  std::string interpose_list;
21  if (!env->GetVar(kDYLDInsertLibrariesKey, &interpose_list)) {
22    VLOG(0) << "No Carbon Interpose library found.";
23    return;
24  }
25
26  // The list is a :-separated list of paths. Because we append our interpose
27  // library just before forking in plugin_process_host.cc, the only cases we
28  // need to handle are:
29  // 1) The whole string is "<kInterposeLibraryPath>", so just clear it, or
30  // 2) ":<kInterposeLibraryPath>" is the end of the string, so trim and re-set.
31  std::string interpose_library_path =
32      GetContentClient()->GetCarbonInterposePath();
33  DCHECK_GE(interpose_list.size(), interpose_library_path.size());
34  size_t suffix_offset = interpose_list.size() - interpose_library_path.size();
35  if (suffix_offset == 0 &&
36      interpose_list == interpose_library_path) {
37    env->UnSetVar(kDYLDInsertLibrariesKey);
38  } else if (suffix_offset > 0 && interpose_list[suffix_offset - 1] == ':' &&
39             interpose_list.substr(suffix_offset) == interpose_library_path) {
40    std::string trimmed_list = interpose_list.substr(0, suffix_offset - 1);
41    env->SetVar(kDYLDInsertLibrariesKey, trimmed_list.c_str());
42  } else {
43    NOTREACHED() << "Missing Carbon interposing library";
44  }
45}
46#endif
47
48void InitializeChromeApplication() {
49  [NSApplication sharedApplication];
50  mac_plugin_interposing::SetUpCocoaInterposing();
51}
52
53}  // namespace content
54