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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_SCANNER_SCANNER_SESSION_H_
#define ASH_SCANNER_SCANNER_SESSION_H_
#include <memory>
#include <string>
#include <vector>
#include "ash/ash_export.h"
#include "ash/scanner/scanner_action_view_model.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "components/manta/manta_status.h"
#include "components/manta/proto/scanner.pb.h"
namespace ash {
class ScannerProfileScopedDelegate;
// A ScannerSession represents a single "use" of the Scanner feature. A session
// will be created when the feature is first triggered, until the feature is
// either dismissed, or commits its final result. The initialization of a
// session will be triggered on the creation of a new SunfishSession, however
// a ScannerSession's lifetime is not strictly bound to the lifetime of a
// SunfishSession.
class ASH_EXPORT ScannerSession {
public:
// Contains data about an error that may have occurred while fetching actions
// during a Scanner session.
struct FetchError {
// The error message to show.
std::u16string error_message;
// Whether the user can try the same query again after encountering this
// error.
bool can_try_again = false;
};
using FetchActionsResponse =
base::expected<std::vector<ScannerActionViewModel>, FetchError>;
// Callback used to receive the actions returned from a FetchActions call.
using FetchActionsCallback =
base::OnceCallback<void(FetchActionsResponse response)>;
using PopulateActionCallback =
base::OnceCallback<void(manta::proto::ScannerAction action)>;
explicit ScannerSession(ScannerProfileScopedDelegate* delegate);
ScannerSession(const ScannerSession&) = delete;
ScannerSession& operator=(const ScannerSession&) = delete;
~ScannerSession();
// Fetches Scanner actions that are available based on the contents of
// `jpeg_bytes`. The actions are returned via `callback`.
void FetchActionsForImage(scoped_refptr<base::RefCountedMemory> jpeg_bytes,
FetchActionsCallback callback);
// Populates the selected action based on the contents of
// `downscaled_jpeg_bytes`.
void PopulateAction(
scoped_refptr<base::RefCountedMemory> downscaled_jpeg_bytes,
manta::proto::ScannerAction unpopulated_action,
PopulateActionCallback callback);
void SetMockScannerOutput(
std::unique_ptr<manta::proto::ScannerOutput> mock_output);
private:
void OnActionsReturned(
scoped_refptr<base::RefCountedMemory> downscaled_jpeg_bytes,
base::TimeTicks request_start_time,
FetchActionsCallback callback,
std::unique_ptr<manta::proto::ScannerOutput> output,
manta::MantaStatus status);
const raw_ptr<ScannerProfileScopedDelegate> delegate_;
std::unique_ptr<manta::proto::ScannerOutput> mock_scanner_output_;
base::WeakPtrFactory<ScannerSession> weak_ptr_factory_{this};
};
} // namespace ash
#endif // ASH_SCANNER_SCANNER_SESSION_H_
|