File: compose_enabling.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (125 lines) | stat: -rw-r--r-- 5,356 bytes parent folder | download | duplicates (3)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_COMPOSE_COMPOSE_ENABLING_H_
#define CHROME_BROWSER_COMPOSE_COMPOSE_ENABLING_H_

#include <memory>

#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/types/expected.h"
#include "chrome/browser/compose/proto/compose_optimization_guide.pb.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/autofill/core/common/aliases.h"
#include "components/compose/core/browser/compose_metrics.h"
#include "components/optimization_guide/core/model_execution/settings_enabled_observer.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/translate/core/browser/translate_manager.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/render_frame_host.h"

namespace compose {

enum class ComposeNudgeDenyReason {
  kSavedStateNotificationDisabled = 0,
  kSavedStateNudgeDisabled = 1,
  // The proactive nudge could have shown but was disabled by preference or
  // config values.
  kProactiveNudgeDisabled = 2,
  // The proactive nudge can not be shown for this user, page, or field.
  kProactiveNudgeBlocked = 3,
};

}  // namespace compose

class ComposeEnabling {
 public:
  using ScopedOverride = std::unique_ptr<base::ScopedClosureRunner>;

  explicit ComposeEnabling(
      Profile* profile,
      signin::IdentityManager* identity_manager,
      OptimizationGuideKeyedService* opt_guide);
  ~ComposeEnabling();

  ComposeEnabling(const ComposeEnabling&) = delete;
  ComposeEnabling& operator=(const ComposeEnabling&) = delete;

  // Static method that verifies that the feature can be enabled on the given
  // profile. Doesn't take advantage of test opt_guide or identity_manager,
  // use member function version if you need to mock them out.
  static bool IsEnabledForProfile(Profile* profile);

  // Static method that verifies that the feature can be visible in settings for
  // the given profile even if the feature is disabled by policy. Doesn't take
  // advantage of test opt_guide or identity_manager, use member function
  // version if you need to mock them out.
  static bool IsSettingVisible(Profile* profile);

  // Instance method that verifies that the feature can be enabled for the
  // profile associated with this instance upon construction.
  base::expected<void, compose::ComposeShowStatus> IsEnabled();

  // The following methods allow overriding is-enabled checks to facilitate
  // testing. The returned instances must be kept in scope while the override
  // is required. When they go out of scope, the override is reverted. If
  // multiple overrides are created, the override is reverted only when all
  // instances are destroyed. These implementations are not multi-thread safe.
  static ScopedOverride ScopedEnableComposeForTesting();
  static ScopedOverride ScopedSkipUserCheckForTesting();
  // VariationsService is not available for unit tests, hence the need for this
  // override method. Browser tests are recommended to use
  // VariationsService::OverrideStoredPermanentCountry because it exercises a
  // code path closer to the runtime one.
  static ScopedOverride OverrideCountryForTesting(std::string country_code);

  base::expected<void, compose::ComposeShowStatus> ShouldTriggerNoStatePopup(
      std::string_view autocomplete_attribute,
      bool writingsuggestions_attribute,
      Profile* profile,
      PrefService* prefs,
      translate::TranslateManager* translate_manager,
      const url::Origin& top_level_frame_origin,
      const url::Origin& element_frame_origin,
      GURL url,
      bool is_msbb_enabled);

  bool ShouldTriggerSavedStatePopup(
      autofill::AutofillSuggestionTriggerSource trigger_source);

  bool ShouldTriggerContextMenu(Profile* profile,
                                translate::TranslateManager* translate_manager,
                                content::RenderFrameHost* rfh,
                                content::ContextMenuParams& params);

  compose::ComposeHintDecision GetOptimizationGuidanceForUrl(const GURL& url,
                                                             Profile* profile);

  // Checks if the page assessed language is supported by Compose.
  bool IsPageLanguageSupported(translate::TranslateManager* translate_manager);

 private:
  base::expected<void, compose::ComposeShowStatus> PageLevelChecks(
      translate::TranslateManager* translate_manager,
      GURL url,
      const url::Origin& top_level_frame_origin,
      const url::Origin& element_frame_origin,
      bool is_newsted_within_fenced_frame);

  static base::expected<void, compose::ComposeShowStatus> CheckEnabling(
      OptimizationGuideKeyedService* opt_guide,
      signin::IdentityManager* identity_manager);

  raw_ptr<Profile> profile_;
  raw_ptr<OptimizationGuideKeyedService> opt_guide_;
  raw_ptr<signin::IdentityManager> identity_manager_;

  static int enabled_for_testing_;
  static int skip_user_check_for_testing_;
};

#endif  // CHROME_BROWSER_COMPOSE_COMPOSE_ENABLING_H_