forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DevTools: move browser context management to devtools_manager_delegate
This patch: - moves browser context management to devtools_manager_delegate - implements browser context related protocol methods in content/ R=dgozman Bug: 631464 Change-Id: Ia43343d5c2208556214b41225cb05f744f2d9ccd Reviewed-on: https://chromium-review.googlesource.com/1183594 Commit-Queue: Andrey Lushnikov <lushnikov@chromium.org> Reviewed-by: Dmitry Gozman <dgozman@chromium.org> Cr-Commit-Position: refs/heads/master@{#585102}
- Loading branch information
1 parent
8706c40
commit 3a29ade
Showing
18 changed files
with
329 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
chrome/browser/devtools/devtools_browser_context_manager.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/devtools/devtools_browser_context_manager.h" | ||
|
||
#include "chrome/browser/profiles/profile_manager.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/browser/ui/browser_list.h" | ||
|
||
DevToolsBrowserContextManager::DevToolsBrowserContextManager() | ||
: weak_factory_(this) {} | ||
|
||
DevToolsBrowserContextManager::~DevToolsBrowserContextManager() = default; | ||
|
||
// static | ||
DevToolsBrowserContextManager& DevToolsBrowserContextManager::GetInstance() { | ||
static base::NoDestructor<DevToolsBrowserContextManager> instance; | ||
return *instance; | ||
} | ||
|
||
Profile* DevToolsBrowserContextManager::GetProfileById( | ||
const std::string& context_id) { | ||
auto it = registrations_.find(context_id); | ||
if (it == registrations_.end()) | ||
return nullptr; | ||
return it->second->profile(); | ||
} | ||
|
||
content::BrowserContext* DevToolsBrowserContextManager::CreateBrowserContext() { | ||
Profile* original_profile = | ||
ProfileManager::GetActiveUserProfile()->GetOriginalProfile(); | ||
|
||
auto registration = | ||
IndependentOTRProfileManager::GetInstance()->CreateFromOriginalProfile( | ||
original_profile, | ||
base::BindOnce( | ||
&DevToolsBrowserContextManager::OnOriginalProfileDestroyed, | ||
weak_factory_.GetWeakPtr())); | ||
content::BrowserContext* context = registration->profile(); | ||
const std::string& context_id = context->UniqueId(); | ||
registrations_[context_id] = std::move(registration); | ||
return context; | ||
} | ||
|
||
std::vector<content::BrowserContext*> | ||
DevToolsBrowserContextManager::GetBrowserContexts() { | ||
std::vector<content::BrowserContext*> result; | ||
for (const auto& registration_pair : registrations_) | ||
result.push_back(registration_pair.second->profile()); | ||
return result; | ||
} | ||
|
||
void DevToolsBrowserContextManager::DisposeBrowserContext( | ||
content::BrowserContext* context, | ||
content::DevToolsManagerDelegate::DisposeCallback callback) { | ||
std::string context_id = context->UniqueId(); | ||
if (pending_context_disposals_.find(context_id) != | ||
pending_context_disposals_.end()) { | ||
std::move(callback).Run(false, "Disposal of browser context " + context_id + | ||
" is already pending"); | ||
return; | ||
} | ||
auto it = registrations_.find(context_id); | ||
if (it == registrations_.end()) { | ||
std::move(callback).Run( | ||
false, "Failed to find browser context with id " + context_id); | ||
return; | ||
} | ||
|
||
Profile* profile = it->second->profile(); | ||
bool has_opened_browser = false; | ||
for (auto* opened_browser : *BrowserList::GetInstance()) { | ||
if (opened_browser->profile() == profile) { | ||
has_opened_browser = true; | ||
break; | ||
} | ||
} | ||
|
||
// If no browsers are opened - dispose right away. | ||
if (!has_opened_browser) { | ||
registrations_.erase(it); | ||
std::move(callback).Run(true, ""); | ||
return; | ||
} | ||
|
||
if (pending_context_disposals_.empty()) | ||
BrowserList::AddObserver(this); | ||
|
||
pending_context_disposals_[context_id] = std::move(callback); | ||
BrowserList::CloseAllBrowsersWithIncognitoProfile( | ||
profile, base::DoNothing(), base::DoNothing(), | ||
true /* skip_beforeunload */); | ||
} | ||
|
||
void DevToolsBrowserContextManager::OnOriginalProfileDestroyed( | ||
Profile* profile) { | ||
base::EraseIf(registrations_, [&profile](const auto& it) { | ||
return it.second->profile()->GetOriginalProfile() == profile; | ||
}); | ||
} | ||
|
||
void DevToolsBrowserContextManager::OnBrowserRemoved(Browser* browser) { | ||
std::string context_id = browser->profile()->UniqueId(); | ||
auto pending_disposal = pending_context_disposals_.find(context_id); | ||
if (pending_disposal == pending_context_disposals_.end()) | ||
return; | ||
for (auto* opened_browser : *BrowserList::GetInstance()) { | ||
if (opened_browser->profile() == browser->profile()) | ||
return; | ||
} | ||
auto it = registrations_.find(context_id); | ||
// We cannot delete immediately here: the profile might still be referenced | ||
// during the browser tier-down process. | ||
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, | ||
it->second.release()); | ||
registrations_.erase(it); | ||
std::move(pending_disposal->second).Run(true, ""); | ||
pending_context_disposals_.erase(pending_disposal); | ||
if (pending_context_disposals_.empty()) | ||
BrowserList::RemoveObserver(this); | ||
} |
43 changes: 43 additions & 0 deletions
43
chrome/browser/devtools/devtools_browser_context_manager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_CONTEXT_MANAGER_H_ | ||
#define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_CONTEXT_MANAGER_H_ | ||
|
||
#include "base/memory/weak_ptr.h" | ||
#include "base/no_destructor.h" | ||
#include "chrome/browser/media/router/presentation/independent_otr_profile_manager.h" | ||
#include "content/public/browser/devtools_manager_delegate.h" | ||
|
||
class DevToolsBrowserContextManager : public BrowserListObserver { | ||
public: | ||
static DevToolsBrowserContextManager& GetInstance(); | ||
|
||
Profile* GetProfileById(const std::string& browser_context_id); | ||
std::vector<content::BrowserContext*> GetBrowserContexts(); | ||
content::BrowserContext* CreateBrowserContext(); | ||
void DisposeBrowserContext( | ||
content::BrowserContext* context, | ||
content::DevToolsManagerDelegate::DisposeCallback callback); | ||
|
||
private: | ||
friend class base::NoDestructor<DevToolsBrowserContextManager>; | ||
DevToolsBrowserContextManager(); | ||
~DevToolsBrowserContextManager() override; | ||
void OnOriginalProfileDestroyed(Profile* profile); | ||
|
||
void OnBrowserRemoved(Browser* browser) override; | ||
|
||
base::flat_map< | ||
std::string, | ||
std::unique_ptr<IndependentOTRProfileManager::OTRProfileRegistration>> | ||
registrations_; | ||
base::flat_map<std::string, content::DevToolsManagerDelegate::DisposeCallback> | ||
pending_context_disposals_; | ||
|
||
base::WeakPtrFactory<DevToolsBrowserContextManager> weak_factory_; | ||
DISALLOW_COPY_AND_ASSIGN(DevToolsBrowserContextManager); | ||
}; | ||
|
||
#endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_CONTEXT_MANAGER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.