Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / app / android / chrome_main_delegate_android.cc
blobdec88e9c93a287479542220bfd21c52f7fb30074
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.
5 #include "chrome/app/android/chrome_main_delegate_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/base_paths_android.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/trace_event/trace_event.h"
14 #include "chrome/browser/android/chrome_startup_flags.h"
15 #include "chrome/browser/android/metrics/uma_utils.h"
16 #include "chrome/browser/android/metrics/uma_utils.h"
17 #include "chrome/browser/media/android/remote/remote_media_player_manager.h"
18 #include "components/policy/core/browser/android/android_combined_policy_provider.h"
19 #include "components/startup_metric_utils/startup_metric_utils.h"
20 #include "content/browser/media/android/browser_media_player_manager.h"
21 #include "content/public/browser/browser_main_runner.h"
23 #if defined(SAFE_BROWSING_DB_REMOTE)
24 #include "chrome/browser/safe_browsing/safe_browsing_api_handler.h"
25 #endif
27 namespace {
29 content::BrowserMediaPlayerManager* CreateRemoteMediaPlayerManager(
30 content::RenderFrameHost* render_frame_host) {
31 return new remote_media::RemoteMediaPlayerManager(render_frame_host);
34 } // namespace
36 // ChromeMainDelegateAndroid is created when the library is loaded. It is always
37 // done in the process's main Java thread. But for non browser process, e.g.
38 // renderer process, it is not the native Chrome's main thread.
39 ChromeMainDelegateAndroid::ChromeMainDelegateAndroid() {
42 ChromeMainDelegateAndroid::~ChromeMainDelegateAndroid() {
45 bool ChromeMainDelegateAndroid::BasicStartupComplete(int* exit_code) {
46 #if defined(SAFE_BROWSING_DB_REMOTE)
47 safe_browsing_api_handler_.reset(CreateSafeBrowsingApiHandler());
48 SafeBrowsingApiHandler::SetInstance(safe_browsing_api_handler_.get());
49 #endif
51 policy::android::AndroidCombinedPolicyProvider::SetShouldWaitForPolicy(true);
52 SetChromeSpecificCommandLineFlags();
53 content::BrowserMediaPlayerManager::RegisterFactory(
54 &CreateRemoteMediaPlayerManager);
56 return ChromeMainDelegate::BasicStartupComplete(exit_code);
59 void ChromeMainDelegateAndroid::SandboxInitialized(
60 const std::string& process_type) {
61 ChromeMainDelegate::SandboxInitialized(process_type);
64 int ChromeMainDelegateAndroid::RunProcess(
65 const std::string& process_type,
66 const content::MainFunctionParams& main_function_params) {
67 TRACE_EVENT0("startup", "ChromeMainDelegateAndroid::RunProcess")
68 if (process_type.empty()) {
69 // By default, Android creates the directory accessible by others.
70 // We'd like to tighten security and make it accessible only by
71 // the browser process.
72 base::FilePath data_path;
73 bool ok = PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path);
74 if (ok) {
75 int permissions;
76 ok = base::GetPosixFilePermissions(data_path, &permissions);
77 if (ok) {
78 permissions &= base::FILE_PERMISSION_USER_MASK;
79 } else {
80 permissions = base::FILE_PERMISSION_READ_BY_USER |
81 base::FILE_PERMISSION_WRITE_BY_USER |
82 base::FILE_PERMISSION_EXECUTE_BY_USER;
85 ok = base::SetPosixFilePermissions(data_path, permissions);
87 if (!ok)
88 LOG(ERROR) << "Failed to set permission of " << data_path.value();
90 // Because the browser process can be started asynchronously as a series of
91 // UI thread tasks a second request to start it can come in while the
92 // first request is still being processed. Chrome must keep the same
93 // browser runner for the second request.
94 // Also only record the start time the first time round, since this is the
95 // start time of the application, and will be same for all requests.
96 if (!browser_runner_.get()) {
97 base::Time time = chrome::android::GetMainEntryPointTime();
98 startup_metric_utils::RecordMainEntryPointTime(time);
99 browser_runner_.reset(content::BrowserMainRunner::Create());
101 return browser_runner_->Initialize(main_function_params);
104 return ChromeMainDelegate::RunProcess(process_type, main_function_params);
107 void ChromeMainDelegateAndroid::ProcessExiting(
108 const std::string& process_type) {
109 #if defined(SAFE_BROWSING_DB_REMOTE)
110 SafeBrowsingApiHandler::SetInstance(nullptr);
111 #endif
114 #if defined(SAFE_BROWSING_DB_REMOTE)
115 SafeBrowsingApiHandler*
116 ChromeMainDelegateAndroid::CreateSafeBrowsingApiHandler() {
117 return nullptr;
119 #endif