Elim cr-checkbox
[chromium-blink-merge.git] / extensions / components / javascript_dialog_extensions_client / javascript_dialog_extension_client_impl.cc
blob6874a3a1a1ba2bf116bfadcbe9923d248fe562f9
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 {
15 namespace {
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(
30 web_contents);
33 class JavaScriptDialogExtensionsClientImpl
34 : public app_modal::JavaScriptDialogExtensionsClient {
35 public:
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)
43 return;
45 DCHECK(web_contents);
46 extensions::ProcessManager* pm = GetProcessManager(web_contents);
47 if (pm)
48 pm->IncrementLazyKeepaliveCount(extension);
50 void OnDialogClosed(content::WebContents* web_contents) override {
51 const Extension* extension = GetExtensionForWebContents(web_contents);
52 if (extension == nullptr)
53 return;
55 DCHECK(web_contents);
56 extensions::ProcessManager* pm = GetProcessManager(web_contents);
57 if (pm)
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);
64 if (extension &&
65 web_contents->GetLastCommittedURL().GetOrigin() == origin_url) {
66 *name_out = extension->name();
67 return true;
69 return false;
72 private:
73 DISALLOW_COPY_AND_ASSIGN(JavaScriptDialogExtensionsClientImpl);
76 } // namespace
78 void InstallClient() {
79 app_modal::JavaScriptDialogManager::GetInstance()->
80 SetExtensionsClient(
81 make_scoped_ptr(new JavaScriptDialogExtensionsClientImpl));
84 } // namespace javascript_dialog_extensions_client