File: drive_service.h

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 (102 lines) | stat: -rw-r--r-- 4,186 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
// Copyright 2021 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_NEW_TAB_PAGE_MODULES_FILE_SUGGESTION_DRIVE_SERVICE_H_
#define CHROME_BROWSER_NEW_TAB_PAGE_MODULES_FILE_SUGGESTION_DRIVE_SERVICE_H_

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "chrome/browser/new_tab_page/modules/file_suggestion/drive_suggestion.mojom.h"
#include "chrome/browser/new_tab_page/modules/file_suggestion/file_suggestion.mojom.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/segmentation_platform/public/result.h"
#include "components/segmentation_platform/public/segmentation_platform_service.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"

class PrefRegistrySimple;
class PrefService;

namespace signin {
class IdentityManager;
class PrimaryAccountAccessTokenFetcher;
}  // namespace signin

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class ItemSuggestRequestResult {
  kSuccess = 0,
  kNetworkError = 1,
  kJsonParseError = 2,
  kContentError = 3,
  kMaxValue = kContentError,
};

// Handles requests for user Google Drive data.
class DriveService : public KeyedService {
 public:
  static const char kLastDismissedTimePrefName[];
  static const base::TimeDelta kDismissDuration;

  DriveService(const DriveService&) = delete;
  DriveService(
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
      signin::IdentityManager* identity_manager,
      segmentation_platform::SegmentationPlatformService*
          segmentation_platform_service,
      const std::string& application_locale,
      PrefService* pref_service);
  ~DriveService() override;

  static void RegisterProfilePrefs(PrefRegistrySimple* registry);

  using GetFilesCallback =
      file_suggestion::mojom::DriveSuggestionHandler::GetFilesCallback;
  // Retrieves Google Drive document suggestions from ItemSuggest API.
  void GetDriveFiles(GetFilesCallback get_files_callback);
  // Retrieves classification result from segmentation platform before
  // retrieving document suggestions.
  // TODO(crbug.com/40925895): Use the classification result to decide when to
  // show the Drive module, instead of ignoring it.
  bool GetDriveModuleSegmentationData();
  void GetDriveFilesInternal();
  // Makes the service not return data for a specified amount of time.
  void DismissModule();
  // Makes the service return data again even if dimiss time is not yet over.
  void RestoreModule();

 private:
  void OnTokenReceived(GoogleServiceAuthError error,
                       signin::AccessTokenInfo token_info);
  void OnJsonReceived(const std::string& token,
                      std::unique_ptr<std::string> json_response);
  void OnJsonParsed(data_decoder::DataDecoder::ValueOrError result);

  // Used for fetching OAuth2 access tokens. Only non-null when a token
  // is made available, or a token is being fetched.
  std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher> token_fetcher_;
  std::unique_ptr<network::SimpleURLLoader> url_loader_;
  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  std::vector<GetFilesCallback> callbacks_;
  raw_ptr<signin::IdentityManager> identity_manager_;
  raw_ptr<segmentation_platform::SegmentationPlatformService>
      segmentation_platform_service_;
  std::string application_locale_;
  raw_ptr<PrefService> pref_service_;
  std::unique_ptr<std::string> cached_json_;
  base::Time cached_json_time_;
  std::string cached_json_token_;
  SEQUENCE_CHECKER(sequence_checker_);
  base::WeakPtrFactory<DriveService> weak_factory_{this};
};

#endif  // CHROME_BROWSER_NEW_TAB_PAGE_MODULES_FILE_SUGGESTION_DRIVE_SERVICE_H_