Skip to content

Commit

Permalink
Merge pull request #18394 from brave/fix_copy_text_image_crash
Browse files Browse the repository at this point in the history
Fixed crash due to refer widget very early
  • Loading branch information
simonhong authored May 8, 2023
2 parents f8d204c + 41980d0 commit 42a7b55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
21 changes: 15 additions & 6 deletions browser/ui/views/text_recognition_dialog_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,13 @@ TextRecognitionDialogView::TextRecognitionDialogView(const SkBitmap& image)
header_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
header_label_->SetProperty(views::kMarginsKey,
gfx::Insets::TLBR(0, 0, 10, 0));
}

TextRecognitionDialogView::~TextRecognitionDialogView() = default;

void TextRecognitionDialogView::AddedToWidget() {
#if BUILDFLAG(IS_WIN)
// Start text extracting after getting language list to make code simpler.
com_task_runner_ =
base::ThreadPool::CreateCOMSTATaskRunner({base::MayBlock()});
com_task_runner_->PostTaskAndReplyWithResult(
Expand All @@ -137,13 +142,11 @@ TextRecognitionDialogView::TextRecognitionDialogView(const SkBitmap& image)
base::BindOnce(
&TextRecognitionDialogView::OnGetAvailableRecognizerLanguages,
weak_factory_.GetWeakPtr()));
#endif

#else
StartExtractingText();
#endif
}

TextRecognitionDialogView::~TextRecognitionDialogView() = default;

void TextRecognitionDialogView::StartExtractingText(
const std::string& language_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Expand All @@ -165,6 +168,7 @@ void TextRecognitionDialogView::StartExtractingText(

header_label_->SetText(brave_l10n::GetLocalizedResourceUTF16String(
IDS_TEXT_RECOGNITION_DIALOG_HEADER_IN_PROGRESS));
AdjustWidgetSize();

#if BUILDFLAG(IS_MAC)
base::ThreadPool::PostTaskAndReplyWithResult(
Expand Down Expand Up @@ -250,6 +254,7 @@ void TextRecognitionDialogView::UpdateContents(
}

void TextRecognitionDialogView::AdjustWidgetSize() {
DCHECK(GetWidget());
GetWidget()->SetSize(GetDialogClientView()->GetPreferredSize());
}

Expand Down Expand Up @@ -283,8 +288,12 @@ void TextRecognitionDialogView::OnGetAvailableRecognizerLanguages(
base::BindRepeating(&TextRecognitionDialogView::OnLanguageOptionchanged,
base::Unretained(this)));
combobox_->SetProperty(views::kMarginsKey, gfx::Insets::TLBR(0, 0, 10, 0));
combobox_->SetEnabled(result_ ? true : false);
AdjustWidgetSize();

// Enabled state is updated during the extracting.
// While extracting, it's disabled and then enabled when extracting is done.
combobox_->SetEnabled(false);

StartExtractingText();
}

bool TextRecognitionDialogView::OnLanguageOptionchanged(size_t index) {
Expand Down
3 changes: 3 additions & 0 deletions browser/ui/views/text_recognition_dialog_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class TextRecognitionDialogView : public views::DialogDelegateView {
private:
FRIEND_TEST_ALL_PREFIXES(TextRecognitionBrowserTest, TextRecognitionTest);

// views::DialogDelegateView overrides:
void AddedToWidget() override;

void OnGetTextFromImage(const std::vector<std::string>& text);

#if BUILDFLAG(IS_WIN)
Expand Down

0 comments on commit 42a7b55

Please sign in to comment.