Skip to content

Commit fe68180

Browse files
committed
Fix WolvicPermissionManager's ownership
The permission manager ownership was not properly handled. On the one hand we were treating it as a singleton (because we're storing an instance in the class) but at the same time the constructor was not private so multiple instances could be created. Additionally the code calling the constructor was storing it in an unique_ptr. That didn't make much sense because the unique_ptr takes care of the ownership while at the same time we're keeping a pointer in the class. That sort of a mess was causing crashes when requesting permission like in Google Meet for example. Fixes #97
1 parent 07d9122 commit fe68180

File tree

4 files changed

+18
-37
lines changed

4 files changed

+18
-37
lines changed

wolvic/wolvic_browser_context.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,7 @@ SSLHostStateDelegate* WolvicBrowserContext::GetSSLHostStateDelegate() {
243243

244244
PermissionControllerDelegate*
245245
WolvicBrowserContext::GetPermissionControllerDelegate() {
246-
if (!permission_manager_) {
247-
permission_manager_ =
248-
std::make_unique<wolvic::WolvicPermissionManager>(this);
249-
}
250-
251-
return permission_manager_.get();
246+
return wolvic::WolvicPermissionManager::GetInstance(off_the_record_);
252247
}
253248

254249
ClientHintsControllerDelegate*

wolvic/wolvic_browser_context.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ class WolvicBrowserContext : public BrowserContext,
102102
}
103103

104104
protected:
105-
std::unique_ptr<PermissionControllerDelegate> permission_manager_;
106105
std::unique_ptr<BackgroundSyncController> background_sync_controller_;
107106
std::unique_ptr<ContentIndexProvider> content_index_provider_;
108107
std::unique_ptr<ReduceAcceptLanguageControllerDelegate>

wolvic/wolvic_permission_manager.cc

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "base/android/scoped_java_ref.h"
1717
#include "base/notreached.h"
1818
#include "components/permissions/permission_util.h"
19-
#include "content/public/browser/browser_context.h"
2019
#include "content/public/browser/browser_thread.h"
2120
#include "content/public/browser/media_capture_devices.h"
2221
#include "content/public/browser/media_stream_request.h"
@@ -267,16 +266,6 @@ const blink::MediaStreamDevice* GetDeviceByIdOrFirstAvailable(
267266
wolvic::WolvicPermissionManager* g_instance = nullptr;
268267
wolvic::WolvicPermissionManager* g_off_the_record_instance = nullptr;
269268

270-
WolvicPermissionManager* GetPermissionManager() {
271-
DCHECK(g_instance);
272-
return g_instance;
273-
}
274-
275-
WolvicPermissionManager* GetOffTheRecordPermissionManager() {
276-
DCHECK(g_off_the_record_instance);
277-
return g_off_the_record_instance;
278-
}
279-
280269
std::vector<content::PermissionStatus> CombineStatuses(
281270
const std::vector<content::PermissionStatus>& content_statuses,
282271
const std::vector<content::PermissionStatus>& android_statuses) {
@@ -310,23 +299,21 @@ InProgressRequest::InProgressRequest(
310299
InProgressRequest::~InProgressRequest() = default;
311300

312301
WolvicPermissionManager::WolvicPermissionManager(
313-
content::BrowserContext* browser_context)
314-
: browser_context_(browser_context) {
315-
if (browser_context_->IsOffTheRecord()) {
316-
DCHECK(!g_off_the_record_instance);
317-
g_off_the_record_instance = this;
318-
} else {
319-
DCHECK(!g_instance);
320-
g_instance = this;
321-
}
322-
}
302+
bool off_the_record)
303+
: off_the_record_(off_the_record) {}
323304

324305
WolvicPermissionManager::~WolvicPermissionManager() = default;
325306

326307
WolvicPermissionManager* WolvicPermissionManager::GetInstance(
327-
bool is_off_the_record) {
328-
return is_off_the_record ? GetOffTheRecordPermissionManager()
329-
: GetPermissionManager();
308+
bool off_the_record) {
309+
if (off_the_record) {
310+
if (!g_off_the_record_instance)
311+
g_off_the_record_instance = new WolvicPermissionManager(true);
312+
return g_off_the_record_instance;
313+
}
314+
if (!g_instance)
315+
g_instance = new WolvicPermissionManager(false);
316+
return g_instance;
330317
}
331318

332319
void WolvicPermissionManager::RequestPermissions(
@@ -512,7 +499,7 @@ void WolvicPermissionManager::OnMediaContentPermissionResult(
512499
env, in_progress_request->media_request.value().security_origin.spec());
513500
Java_PermissionManagerBridge_onMediaPermissionRequest(
514501
env, video_sources, audio_sources, url,
515-
browser_context_->IsOffTheRecord(),
502+
off_the_record_,
516503
reinterpret_cast<jlong>(in_progress_request));
517504
}
518505

@@ -605,7 +592,7 @@ void WolvicPermissionManager::RequestContentPermissions(
605592
env, std::span(permissions.begin(), permissions.end())));
606593
Java_PermissionManagerBridge_onContentPermissionRequest(
607594
env, permissions_java_array, url_java_string,
608-
browser_context_->IsOffTheRecord(),
595+
off_the_record_,
609596
reinterpret_cast<jlong>(in_progress_request));
610597
}
611598

@@ -621,7 +608,7 @@ void WolvicPermissionManager::RequestAndroidPermissions(
621608
android_permissions.end())));
622609

623610
Java_PermissionManagerBridge_onAndroidPermissionRequest(
624-
env, java_android_permissions, browser_context_->IsOffTheRecord(),
611+
env, java_android_permissions, off_the_record_,
625612
reinterpret_cast<jlong>(in_progress_request));
626613
}
627614

wolvic/wolvic_permission_manager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ struct InProgressRequest {
3939

4040
class WolvicPermissionManager : public content::PermissionControllerDelegate {
4141
public:
42-
explicit WolvicPermissionManager(content::BrowserContext* browser_context);
4342
~WolvicPermissionManager() override;
4443

45-
static WolvicPermissionManager* GetInstance(bool is_off_the_record);
44+
static WolvicPermissionManager* GetInstance(bool off_the_record);
4645

4746
// PermissionControllerDelegate overrides:
4847
void RequestPermissions(
@@ -112,13 +111,14 @@ class WolvicPermissionManager : public content::PermissionControllerDelegate {
112111
const absl::optional<std::string>& audio_id);
113112

114113
private:
114+
explicit WolvicPermissionManager(bool off_the_record);
115115
void CompleteRequest(InProgressRequest* in_progress_request);
116116
void RequestContentPermissions(JNIEnv* env,
117117
InProgressRequest* in_progress_request);
118118
void RequestAndroidPermissions(JNIEnv* env,
119119
InProgressRequest* in_progress_request);
120120

121-
raw_ptr<content::BrowserContext> browser_context_;
121+
bool off_the_record_;
122122

123123
InProgressRequest* FindInProgressRequest(
124124
const content::PermissionRequestDescription& description);

0 commit comments

Comments
 (0)