1 // Copyright 2019 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 #ifndef CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_WEB_APP_FILE_HANDLER_REGISTRATION_H_
6 #define CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_WEB_APP_FILE_HANDLER_REGISTRATION_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "build/build_config.h"
13 #include "chrome/browser/web_applications/components/web_app_id.h"
14 #include "components/services/app_service/public/cpp/file_handler.h"
15 
16 class Profile;
17 
18 namespace web_app {
19 
20 // True if file handlers are managed externally by the operating system, and
21 // Chrome supports file handling on this operating system.
22 // In practice, this is false on Chrome OS (as Chrome OS uses Chrome's installed
23 // apps to find file handlers), and on operating systems where Chrome doesn't
24 // know how to register file handlers.
25 bool ShouldRegisterFileHandlersWithOs();
26 
27 // Do OS-specific registration to handle opening files with the specified
28 // |file_extensions| and |mime_types| with the PWA with the specified |app_id|.
29 // This may also involve creating a shim app to launch Chrome from.
30 // Note: Some operating systems (such as Chrome OS) may not need to do any work
31 // here.
32 void RegisterFileHandlersWithOs(const AppId& app_id,
33                                 const std::string& app_name,
34                                 Profile* profile,
35                                 const apps::FileHandlers& file_handlers);
36 
37 // Undo the file extensions registration for the PWA with specified |app_id|.
38 // If a shim app was required, also removes the shim app.
39 void UnregisterFileHandlersWithOs(const AppId& app_id, Profile* profile);
40 
41 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
42 using RegisterMimeTypesOnLinuxCallback =
43     base::OnceCallback<bool(base::FilePath profile_path,
44                             std::string file_contents)>;
45 
46 // Exposed for testing purposes. Register the set of
47 // MIME-type-to-file-extensions mappings corresponding to |file_handlers|. File
48 // I/O and a a callout to the Linux shell are performed asynchronously in a
49 // |callback|, which is set automatically on the usual install code path.
50 void RegisterMimeTypesOnLinux(const AppId& app_id,
51                               Profile* profile,
52                               const apps::FileHandlers& file_handlers,
53                               RegisterMimeTypesOnLinuxCallback callback);
54 
55 // Override the default |callback| passed to RegisterMimeTypesOnLinux. Used in
56 // automated browser tests.
57 void SetRegisterMimeTypesOnLinuxCallbackForTesting(
58     RegisterMimeTypesOnLinuxCallback callback);
59 #endif
60 
61 }  // namespace web_app
62 
63 #endif  // CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_WEB_APP_FILE_HANDLER_REGISTRATION_H_
64