Index: apps/url_redirector.cc |
diff --git a/apps/url_redirector.cc b/apps/url_redirector.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a79be8285e3e2ab62bd31a6ba5db533e53420b64 |
--- /dev/null |
+++ b/apps/url_redirector.cc |
@@ -0,0 +1,93 @@ |
+// Copyright 2013 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 "apps/url_redirector.h" |
+ |
+#include "apps/launcher.h" |
+#include "base/command_line.h" |
+#include "base/logging.h" |
+#include "chrome/browser/extensions/extension_service.h" |
+#include "chrome/browser/extensions/extension_system.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/common/extensions/api/url_handlers/url_handlers_parser.h" |
+#include "chrome/common/extensions/extension_messages.h" |
+#include "chrome/common/extensions/extension_set.h" |
jam
2013/09/03 15:25:16
I'll defer to the apps folks, but it's not obvious
sergeygs
2013/09/03 16:43:04
This class launches v2 apps. chrome/, and even ext
not at google - send to devlin
2013/09/03 17:39:52
Honestly I thought it was the other way around, th
sergeygs
2013/09/04 03:29:17
As discussed offline, you are right. I'm going to
|
+#include "extensions/common/switches.h" |
+#include "ipc/ipc_message.h" |
+#include "ipc/ipc_message_macros.h" |
+ |
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(apps::UrlRedirector); |
+ |
+namespace apps { |
+ |
+UrlRedirector::UrlRedirector(content::WebContents* web_contents) |
+ : content::WebContentsObserver(web_contents), |
+ profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) { |
+ // We don't want to ever send visited URLs to apps in incognito. Technically, |
+ // apps are not supported in incognito, but explicitly check for that. |
+ // See b/240879, which tracks incognito support for v2 apps. |
+ CHECK(!profile_->IsOffTheRecord()); |
+} |
+ |
+UrlRedirector::~UrlRedirector() { |
+} |
+ |
+// static |
+bool UrlRedirector::MaybeLaunchAppWithUrl( |
+ Profile* profile, |
+ const GURL& url, |
+ const GURL& referrer_url) { |
+ // This should never happen unless the switch is specified; if it's not, |
+ // originators of the invocation chain should not call us |
+ // (ChromeContentRenderClient::HandleNavigation, chrome::Navigate). |
+ // TODO(sergeygs): Remove this and update includes once url_handlers are |
+ // moved out of experimental. |
+ CHECK(CommandLine::ForCurrentProcess()->HasSwitch( |
+ extensions::switches::kEnableExperimentalExtensionApis)); |
+ |
+ // Even with the about CHECK in the constructor, this method still can be |
+ // invoked in icognito, since it is static. Handle that here just in case. |
+ if (profile->IsOffTheRecord()) |
+ return false; |
+ |
+ ExtensionService* service = |
+ extensions::ExtensionSystem::Get(profile)->extension_service(); |
+ if (!service) |
+ return false; |
+ |
+ for (ExtensionSet::const_iterator iter = service->extensions()->begin(); |
+ iter != service->extensions()->end(); |
+ ++iter) { |
+ const extensions::UrlHandlerInfo* handler = |
+ extensions::UrlHandlers::FindMatchingUrlHandler(*iter, url); |
+ if (handler) { |
+ LaunchPlatformAppWithUrl(profile, *iter, handler->id, url, referrer_url); |
+ return true; |
+ } |
+ } |
+ |
+ return false; |
+} |
+ |
+bool UrlRedirector::OnMessageReceived(const IPC::Message& message) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(UrlRedirector, message) |
+ IPC_MESSAGE_HANDLER(ExtensionHostMsg_RedirectUrl, |
+ LaunchAppWithUrl) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ return handled; |
+} |
+ |
+void UrlRedirector::LaunchAppWithUrl(const GURL& url, |
+ const GURL& referrer_url) const { |
+ // This is a ExtensionHostMsg_RedirectUrl handler. The sender is expected |
+ // to check that an appropriate app to handle |url| indeed exists, so |
+ // the launch here should always succeed. |
+ // See ChromeContentRendererClient::HandleNavigation for an example. |
+ const bool launched = MaybeLaunchAppWithUrl(profile_, url, referrer_url); |
+ DCHECK(launched); |
+} |
+ |
+} // namespace apps |