File: office_web_app.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 5,854 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/office_web_app/office_web_app.h"

#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/web_applications/external_install_options.h"
#include "chrome/browser/web_applications/externally_managed_app_manager.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_icon_generator.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/office_web_app_resources.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"

namespace chromeos {

const char kMicrosoft365WebAppUrl[] =
    "https://www.microsoft365.com/?from=Homescreen";

namespace {
constexpr char kMicrosoft365FallbackName[] = "Microsoft 365";

void OnOfficeWebAppInstalledOffline(
    Profile* profile,
    base::OnceCallback<void(webapps::InstallResultCode)> callback,
    const GURL& install_url,
    web_app::ExternallyManagedAppManager::InstallResult result) {
  UMA_HISTOGRAM_ENUMERATION(
      "FileBrowser.OfficeFiles.Setup.OfficeWebAppOfflineInstallation",
      result.code);
  if (webapps::IsSuccess(result.code)) {
    auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile);
    proxy->SetSupportedLinksPreference(*result.app_id);
  } else {
    LOG(ERROR) << "Office web app offline installation failure: "
               << result.code;
  }

  std::move(callback).Run(result.code);
}

void InstallMicrosoft365Offline(
    Profile* profile,
    base::OnceCallback<void(webapps::InstallResultCode)> callback) {
  auto* provider = web_app::WebAppProvider::GetForWebApps(profile);
  DCHECK(provider);

  // Manually install using the manifest at
  // https://www.microsoft365.com/webmanifest.json.
  web_app::ExternalInstallOptions options(
      GURL(kMicrosoft365WebAppUrl),
      web_app::mojom::UserDisplayMode::kStandalone,
      web_app::ExternalInstallSource::kInternalMicrosoft365Setup);
  options.fallback_app_name = kMicrosoft365FallbackName;
  options.add_to_quick_launch_bar = false;
  options.only_use_app_info_factory = true;
  options.app_info_factory = base::BindRepeating([]() {
    GURL start_url = GURL(kMicrosoft365WebAppUrl);
    webapps::ManifestId manifest_id =
        web_app::GenerateManifestIdFromStartUrlOnly(start_url);
    auto info =
        std::make_unique<web_app::WebAppInstallInfo>(manifest_id, start_url);
    info->title =
        l10n_util::GetStringUTF16(IDS_OFFICE_FILE_HANDLER_APP_MICROSOFT);
    info->scope = GURL("/");
    info->display_mode = web_app::DisplayMode::kStandalone;

    auto image = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
        IDR_OFFICE_WEB_APP_ICONS_OFFICE_192_PNG);
    info->icon_bitmaps.any[192] = image.AsBitmap();
    info->background_color = 0xFFD53A00;
    info->theme_color = 0xFFD53A00;
    return info;
  });

  provider->externally_managed_app_manager().InstallNow(
      std::move(options), base::BindOnce(&OnOfficeWebAppInstalledOffline,
                                         profile, std::move(callback)));
}

void OnOfficeWebAppInstalled(
    Profile* profile,
    base::OnceCallback<void(webapps::InstallResultCode)> callback,
    const GURL& install_url,
    web_app::ExternallyManagedAppManager::InstallResult result) {
  UMA_HISTOGRAM_ENUMERATION(
      "FileBrowser.OfficeFiles.Setup.OfficeWebAppInstallation", result.code);
  if (webapps::IsSuccess(result.code)) {
    auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile);
    proxy->SetSupportedLinksPreference(*result.app_id);
    std::move(callback).Run(result.code);
    return;
  }

  // Fallback to an offline install.
  LOG(ERROR) << "Office web app installation failure: " << result.code
             << " attempting offline installation";
  InstallMicrosoft365Offline(profile, std::move(callback));
}

}  // namespace

void InstallMicrosoft365(
    Profile* profile,
    base::OnceCallback<void(webapps::InstallResultCode)> callback) {
  auto* provider = web_app::WebAppProvider::GetForWebApps(profile);
  DCHECK(provider);

  web_app::ExternalInstallOptions options(
      GURL(kMicrosoft365WebAppUrl),
      web_app::mojom::UserDisplayMode::kStandalone,
      web_app::ExternalInstallSource::kInternalMicrosoft365Setup);
  options.fallback_app_name = kMicrosoft365FallbackName;
  options.add_to_quick_launch_bar = false;
  // Remove the app with the unexpected start URL.
  if (auto* app =
          provider->registrar_unsafe().LookUpAppByInstallSourceInstallUrl(
              web_app::WebAppManagement::Type::kOneDriveIntegration,
              GURL(kMicrosoft365WebAppUrl));
      app && app->start_url() != kMicrosoft365WebAppUrl) {
    LOG(WARNING)
        << "replacing previously installed app with the unexpected URL";
    options.force_reinstall = true;
    options.uninstall_and_replace = {app->app_id()};
  }

  // Attempt an online install first.
  provider->externally_managed_app_manager().InstallNow(
      std::move(options),
      base::BindOnce(&OnOfficeWebAppInstalled, profile, std::move(callback)));
}

}  // namespace chromeos