[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 1 | // Copyright (c) 2011 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 "content/browser/intents/intent_injector.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/string16.h" |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 10 | #include "content/browser/renderer_host/render_view_host.h" |
| 11 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | 6766b17 | 2011-11-21 18:29:36 | [diff] [blame] | 12 | #include "content/common/intents_messages.h" |
[email protected] | 0d9989d | 2011-12-21 20:26:00 | [diff] [blame] | 13 | #include "content/public/browser/web_intents_dispatcher.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 14 | #include "content/public/common/content_switches.h" |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 15 | #include "webkit/glue/web_intent_data.h" |
[email protected] | 72d8883 | 2011-10-05 17:38:07 | [diff] [blame] | 16 | #include "webkit/glue/web_intent_reply_data.h" |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 17 | |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 18 | using content::WebContents; |
| 19 | |
[email protected] | cb4282a | 2012-01-05 06:08:54 | [diff] [blame] | 20 | IntentInjector::IntentInjector(WebContents* web_contents) |
| 21 | : content::WebContentsObserver(web_contents), |
[email protected] | 0d9989d | 2011-12-21 20:26:00 | [diff] [blame] | 22 | intents_dispatcher_(NULL) { |
[email protected] | cb4282a | 2012-01-05 06:08:54 | [diff] [blame] | 23 | DCHECK(web_contents); |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | IntentInjector::~IntentInjector() { |
| 27 | } |
| 28 | |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 29 | void IntentInjector::WebContentsDestroyed(content::WebContents* tab) { |
[email protected] | 0d9989d | 2011-12-21 20:26:00 | [diff] [blame] | 30 | if (intents_dispatcher_) { |
| 31 | intents_dispatcher_->SendReplyMessage( |
| 32 | webkit_glue::WEB_INTENT_SERVICE_TAB_CLOSED, string16()); |
[email protected] | 4f2c9c3 | 2011-10-21 03:02:51 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 35 | delete this; |
| 36 | } |
| 37 | |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 38 | void IntentInjector::SourceWebContentsDestroyed(WebContents* tab) { |
[email protected] | 0d9989d | 2011-12-21 20:26:00 | [diff] [blame] | 39 | intents_dispatcher_ = NULL; |
[email protected] | 29a6c973 | 2011-10-21 19:16:42 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | 0d9989d | 2011-12-21 20:26:00 | [diff] [blame] | 42 | void IntentInjector::SetIntent( |
| 43 | content::WebIntentsDispatcher* intents_dispatcher, |
| 44 | const webkit_glue::WebIntentData& intent) { |
| 45 | intents_dispatcher_ = intents_dispatcher; |
[email protected] | 3ac1eef7 | 2011-09-28 20:46:07 | [diff] [blame] | 46 | source_intent_.reset(new webkit_glue::WebIntentData(intent)); |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 47 | |
| 48 | SendIntent(); |
| 49 | } |
| 50 | |
| 51 | void IntentInjector::RenderViewCreated(RenderViewHost* host) { |
| 52 | SendIntent(); |
| 53 | } |
| 54 | |
[email protected] | a6e16aec | 2011-11-11 18:53:04 | [diff] [blame] | 55 | void IntentInjector::DidNavigateMainFrame( |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 56 | const content::LoadCommittedDetails& details, |
[email protected] | 6766b17 | 2011-11-21 18:29:36 | [diff] [blame] | 57 | const content::FrameNavigateParams& params) { |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 58 | SendIntent(); |
| 59 | } |
| 60 | |
| 61 | // TODO(gbillock): The "correct" thing here is for this to be a |
| 62 | // RenderViewHostObserver, and do this on RenderViewHostInitialized. There's no |
| 63 | // good hooks for attaching the intent to such an object, though. All RVHOs get |
| 64 | // made deep inside tab contents initialization. Idea: propagate out |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 65 | // RenderViewHostInitialized to a WebContentsObserver latch? That still looks |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 66 | // like it might be racy, though. |
| 67 | void IntentInjector::SendIntent() { |
| 68 | if (source_intent_.get() == NULL || |
[email protected] | 325d034 | 2012-01-26 22:56:39 | [diff] [blame] | 69 | !CommandLine::ForCurrentProcess()->HasSwitch( |
| 70 | switches::kEnableWebIntents) || |
[email protected] | 768c547 | 2011-12-26 19:06:17 | [diff] [blame] | 71 | web_contents()->GetRenderViewHost() == NULL) { |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Send intent data through to renderer. |
[email protected] | 768c547 | 2011-12-26 19:06:17 | [diff] [blame] | 76 | web_contents()->GetRenderViewHost()->Send(new IntentsMsg_SetWebIntentData( |
| 77 | web_contents()->GetRenderViewHost()->routing_id(), |
[email protected] | 67810501 | 2011-12-09 04:01:21 | [diff] [blame] | 78 | *(source_intent_.get()))); |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 79 | source_intent_.reset(NULL); |
| 80 | } |
| 81 | |
| 82 | bool IntentInjector::OnMessageReceived(const IPC::Message& message) { |
| 83 | bool handled = true; |
| 84 | IPC_BEGIN_MESSAGE_MAP(IntentInjector, message) |
[email protected] | 67810501 | 2011-12-09 04:01:21 | [diff] [blame] | 85 | IPC_MESSAGE_HANDLER(IntentsHostMsg_WebIntentReply, OnReply); |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 86 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 87 | IPC_END_MESSAGE_MAP() |
| 88 | return handled; |
| 89 | } |
| 90 | |
[email protected] | 67810501 | 2011-12-09 04:01:21 | [diff] [blame] | 91 | void IntentInjector::OnReply(webkit_glue::WebIntentReplyType reply_type, |
| 92 | const string16& data) { |
[email protected] | 325d034 | 2012-01-26 22:56:39 | [diff] [blame] | 93 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableWebIntents)) |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 94 | NOTREACHED(); |
| 95 | |
[email protected] | 0d9989d | 2011-12-21 20:26:00 | [diff] [blame] | 96 | if (intents_dispatcher_) { |
| 97 | intents_dispatcher_->SendReplyMessage(reply_type, data); |
[email protected] | 4f2c9c3 | 2011-10-21 03:02:51 | [diff] [blame] | 98 | } |
[email protected] | 1ef9313 | 2011-09-16 18:33:47 | [diff] [blame] | 99 | } |