Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / apps / shell / browser / shell_content_browser_client.cc
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.
4
5 #include "apps/shell/browser/shell_content_browser_client.h"
6
7 #include "apps/shell/browser/shell_browser_context.h"
8 #include "apps/shell/browser/shell_browser_main_parts.h"
9 #include "apps/shell/browser/shell_extension_system.h"
10 #include "base/command_line.h"
11 #include "chrome/browser/extensions/extension_protocols.h"
12 #include "chrome/browser/extensions/extension_resource_protocols.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/site_instance.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/url_constants.h"
18 #include "content/shell/browser/shell_browser_context.h"
19 #include "extensions/browser/extension_message_filter.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/browser/info_map.h"
22 #include "extensions/browser/process_map.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/extension.h"
25 #include "extensions/common/switches.h"
26 #include "url/gurl.h"
27
28 using content::BrowserThread;
29 using extensions::ExtensionRegistry;
30
31 namespace apps {
32
33 ShellContentBrowserClient::ShellContentBrowserClient()
34     : browser_main_parts_(NULL) {
35 }
36
37 ShellContentBrowserClient::~ShellContentBrowserClient() {
38 }
39
40 content::BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts(
41     const content::MainFunctionParams& parameters) {
42   browser_main_parts_ = new ShellBrowserMainParts(parameters);
43   return browser_main_parts_;
44 }
45
46 void ShellContentBrowserClient::RenderProcessWillLaunch(
47     content::RenderProcessHost* host) {
48   int render_process_id = host->GetID();
49   host->AddFilter(new extensions::ExtensionMessageFilter(
50       render_process_id, browser_main_parts_->browser_context()));
51 }
52
53 net::URLRequestContextGetter*
54 ShellContentBrowserClient::CreateRequestContext(
55     content::BrowserContext* content_browser_context,
56     content::ProtocolHandlerMap* protocol_handlers) {
57   // Handle chrome-extension: and chrome-extension-resource: requests.
58   extensions::InfoMap* extension_info_map =
59       browser_main_parts_->extension_system()->info_map();
60   (*protocol_handlers)[extensions::kExtensionScheme] =
61       linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
62           CreateExtensionProtocolHandler(false /*is_incognito*/,
63                                          extension_info_map));
64   (*protocol_handlers)[extensions::kExtensionResourceScheme] =
65       linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
66           CreateExtensionResourceProtocolHandler());
67   // Let content::ShellBrowserContext handle the rest of the setup.
68   return browser_main_parts_->browser_context()->CreateRequestContext(
69       protocol_handlers);
70 }
71
72 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) {
73   if (!url.is_valid())
74     return false;
75   // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
76   // content::ShellURLRequestContextGetter::GetURLRequestContext().
77   static const char* const kProtocolList[] = {
78       chrome::kBlobScheme,
79       content::kChromeDevToolsScheme,
80       content::kChromeUIScheme,
81       content::kDataScheme,
82       content::kFileScheme,
83       content::kFileSystemScheme,
84       extensions::kExtensionScheme,
85       extensions::kExtensionResourceScheme,
86   };
87   for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
88     if (url.scheme() == kProtocolList[i])
89       return true;
90   }
91   return false;
92 }
93
94 void ShellContentBrowserClient::SiteInstanceGotProcess(
95     content::SiteInstance* site_instance) {
96   // If this isn't an extension renderer there's nothing to do.
97   const extensions::Extension* extension = GetExtension(site_instance);
98   if (!extension)
99     return;
100
101   extensions::ProcessMap::Get(browser_main_parts_->browser_context())
102       ->Insert(extension->id(),
103                site_instance->GetProcess()->GetID(),
104                site_instance->GetId());
105
106   BrowserThread::PostTask(
107       BrowserThread::IO,
108       FROM_HERE,
109       base::Bind(&extensions::InfoMap::RegisterExtensionProcess,
110                  browser_main_parts_->extension_system()->info_map(),
111                  extension->id(),
112                  site_instance->GetProcess()->GetID(),
113                  site_instance->GetId()));
114 }
115
116 void ShellContentBrowserClient::SiteInstanceDeleting(
117     content::SiteInstance* site_instance) {
118   // If this isn't an extension renderer there's nothing to do.
119   const extensions::Extension* extension = GetExtension(site_instance);
120   if (!extension)
121     return;
122
123   extensions::ProcessMap::Get(browser_main_parts_->browser_context())
124       ->Remove(extension->id(),
125                site_instance->GetProcess()->GetID(),
126                site_instance->GetId());
127
128   BrowserThread::PostTask(
129       BrowserThread::IO,
130       FROM_HERE,
131       base::Bind(&extensions::InfoMap::UnregisterExtensionProcess,
132                  browser_main_parts_->extension_system()->info_map(),
133                  extension->id(),
134                  site_instance->GetProcess()->GetID(),
135                  site_instance->GetId()));
136 }
137
138 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
139     CommandLine* command_line, int child_process_id) {
140   std::string process_type =
141       command_line->GetSwitchValueASCII(switches::kProcessType);
142   if (process_type == switches::kRendererProcess) {
143     // TODO(jamescook): Should we check here if the process is in the extension
144     // service process map, or can we assume all renderers are extension
145     // renderers?
146     command_line->AppendSwitch(extensions::switches::kExtensionProcess);
147   }
148 }
149
150 const extensions::Extension* ShellContentBrowserClient::GetExtension(
151     content::SiteInstance* site_instance) {
152   ExtensionRegistry* registry =
153       ExtensionRegistry::Get(site_instance->GetBrowserContext());
154   return registry->enabled_extensions().GetExtensionOrAppByURL(
155       site_instance->GetSiteURL());
156 }
157
158 }  // namespace apps