Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / extensions / extension_view_host_factory.cc
blobd98b2b704424aa50a7a6b2fb980cba39117893ac
1 // Copyright 2013 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 "chrome/browser/extensions/extension_view_host_factory.h"
7 #include "chrome/browser/extensions/extension_util.h"
8 #include "chrome/browser/extensions/extension_view_host.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/common/url_constants.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/process_manager.h"
14 #include "extensions/common/manifest_handlers/incognito_info.h"
15 #include "extensions/common/view_type.h"
17 namespace extensions {
19 namespace {
21 // Creates a new ExtensionHost with its associated view, grouping it in the
22 // appropriate SiteInstance (and therefore process) based on the URL and
23 // profile.
24 ExtensionViewHost* CreateViewHostForExtension(const Extension* extension,
25 const GURL& url,
26 Profile* profile,
27 Browser* browser,
28 ViewType view_type) {
29 DCHECK(profile);
30 // A NULL browser may only be given for dialogs.
31 DCHECK(browser || view_type == VIEW_TYPE_EXTENSION_DIALOG);
32 scoped_refptr<content::SiteInstance> site_instance =
33 ProcessManager::Get(profile)->GetSiteInstanceForURL(url);
34 ExtensionViewHost* host =
35 new ExtensionViewHost(extension, site_instance.get(), url, view_type);
36 host->CreateView(browser);
37 return host;
40 // Creates a view host for an extension in an incognito window. Returns NULL
41 // if the extension is not allowed to run in incognito.
42 ExtensionViewHost* CreateViewHostForIncognito(const Extension* extension,
43 const GURL& url,
44 Profile* profile,
45 Browser* browser,
46 ViewType view_type) {
47 DCHECK(extension);
48 DCHECK(profile->IsOffTheRecord());
50 if (!IncognitoInfo::IsSplitMode(extension)) {
51 // If it's not split-mode the host is associated with the original profile.
52 Profile* original_profile = profile->GetOriginalProfile();
53 return CreateViewHostForExtension(
54 extension, url, original_profile, browser, view_type);
57 // Create the host if the extension can run in incognito.
58 if (util::IsIncognitoEnabled(extension->id(), profile)) {
59 return CreateViewHostForExtension(
60 extension, url, profile, browser, view_type);
62 NOTREACHED() <<
63 "We shouldn't be trying to create an incognito extension view unless "
64 "it has been enabled for incognito.";
65 return NULL;
68 // Returns the extension associated with |url| in |profile|. Returns NULL if
69 // the extension does not exist.
70 const Extension* GetExtensionForUrl(Profile* profile, const GURL& url) {
71 ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
72 if (!registry)
73 return NULL;
74 std::string extension_id = url.host();
75 return registry->enabled_extensions().GetByID(extension_id);
78 // Creates and initializes an ExtensionViewHost for the extension with |url|.
79 ExtensionViewHost* CreateViewHost(const GURL& url,
80 Profile* profile,
81 Browser* browser,
82 extensions::ViewType view_type) {
83 DCHECK(profile);
84 // A NULL browser may only be given for dialogs.
85 DCHECK(browser || view_type == VIEW_TYPE_EXTENSION_DIALOG);
87 const Extension* extension = GetExtensionForUrl(profile, url);
88 if (!extension)
89 return NULL;
90 if (profile->IsOffTheRecord()) {
91 return CreateViewHostForIncognito(
92 extension, url, profile, browser, view_type);
94 return CreateViewHostForExtension(
95 extension, url, profile, browser, view_type);
98 } // namespace
100 // static
101 ExtensionViewHost* ExtensionViewHostFactory::CreatePopupHost(const GURL& url,
102 Browser* browser) {
103 DCHECK(browser);
104 return CreateViewHost(
105 url, browser->profile(), browser, VIEW_TYPE_EXTENSION_POPUP);
108 // static
109 ExtensionViewHost* ExtensionViewHostFactory::CreateDialogHost(
110 const GURL& url,
111 Profile* profile) {
112 DCHECK(profile);
113 return CreateViewHost(url, profile, NULL, VIEW_TYPE_EXTENSION_DIALOG);
116 } // namespace extensions