1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/components/javascript_dialog_extensions_client/javascript_dialog_extension_client_impl.h"
7 #include "components/app_modal/javascript_dialog_extensions_client.h"
8 #include "components/app_modal/javascript_dialog_manager.h"
9 #include "content/public/browser/web_contents.h"
10 #include "extensions/browser/process_manager.h"
11 #include "extensions/common/extension.h"
12 #include "ui/gfx/native_widget_types.h"
14 namespace javascript_dialog_extensions_client
{
17 using extensions::Extension
;
19 // Returns the ProcessManager for the browser context from |web_contents|.
20 extensions::ProcessManager
* GetProcessManager(
21 content::WebContents
* web_contents
) {
22 return extensions::ProcessManager::Get(web_contents
->GetBrowserContext());
25 // Returns the extension associated with |web_contents| or NULL if there is no
26 // associated extension (or extensions are not supported).
27 const Extension
* GetExtensionForWebContents(
28 content::WebContents
* web_contents
) {
29 return GetProcessManager(web_contents
)->GetExtensionForWebContents(
33 class JavaScriptDialogExtensionsClientImpl
34 : public app_modal::JavaScriptDialogExtensionsClient
{
36 JavaScriptDialogExtensionsClientImpl() {}
37 ~JavaScriptDialogExtensionsClientImpl() override
{}
39 // JavaScriptDialogExtensionsClient:
40 void OnDialogOpened(content::WebContents
* web_contents
) override
{
41 const Extension
* extension
= GetExtensionForWebContents(web_contents
);
42 if (extension
== nullptr)
46 extensions::ProcessManager
* pm
= GetProcessManager(web_contents
);
48 pm
->IncrementLazyKeepaliveCount(extension
);
50 void OnDialogClosed(content::WebContents
* web_contents
) override
{
51 const Extension
* extension
= GetExtensionForWebContents(web_contents
);
52 if (extension
== nullptr)
56 extensions::ProcessManager
* pm
= GetProcessManager(web_contents
);
58 pm
->DecrementLazyKeepaliveCount(extension
);
60 bool GetExtensionName(content::WebContents
* web_contents
,
61 const GURL
& origin_url
,
62 std::string
* name_out
) override
{
63 const Extension
* extension
= GetExtensionForWebContents(web_contents
);
65 web_contents
->GetLastCommittedURL().GetOrigin() == origin_url
) {
66 *name_out
= extension
->name();
73 DISALLOW_COPY_AND_ASSIGN(JavaScriptDialogExtensionsClientImpl
);
78 void InstallClient() {
79 app_modal::JavaScriptDialogManager::GetInstance()->
81 make_scoped_ptr(new JavaScriptDialogExtensionsClientImpl
));
84 } // namespace javascript_dialog_extensions_client