blob: 65daff0e010daef8cdd99b794d15e3d06b96d432 [file] [log] [blame]
[email protected]1ef93132011-09-16 18:33:471// 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]1ef93132011-09-16 18:33:4710#include "content/browser/renderer_host/render_view_host.h"
11#include "content/browser/tab_contents/tab_contents.h"
[email protected]6766b172011-11-21 18:29:3612#include "content/common/intents_messages.h"
[email protected]0d9989d2011-12-21 20:26:0013#include "content/public/browser/web_intents_dispatcher.h"
[email protected]c08950d22011-10-13 22:20:2914#include "content/public/common/content_switches.h"
[email protected]1ef93132011-09-16 18:33:4715#include "webkit/glue/web_intent_data.h"
[email protected]72d88832011-10-05 17:38:0716#include "webkit/glue/web_intent_reply_data.h"
[email protected]1ef93132011-09-16 18:33:4717
[email protected]26b5e322011-12-23 01:36:4718using content::WebContents;
19
[email protected]cb4282a2012-01-05 06:08:5420IntentInjector::IntentInjector(WebContents* web_contents)
21 : content::WebContentsObserver(web_contents),
[email protected]0d9989d2011-12-21 20:26:0022 intents_dispatcher_(NULL) {
[email protected]cb4282a2012-01-05 06:08:5423 DCHECK(web_contents);
[email protected]1ef93132011-09-16 18:33:4724}
25
26IntentInjector::~IntentInjector() {
27}
28
[email protected]26b5e322011-12-23 01:36:4729void IntentInjector::WebContentsDestroyed(content::WebContents* tab) {
[email protected]0d9989d2011-12-21 20:26:0030 if (intents_dispatcher_) {
31 intents_dispatcher_->SendReplyMessage(
32 webkit_glue::WEB_INTENT_SERVICE_TAB_CLOSED, string16());
[email protected]4f2c9c32011-10-21 03:02:5133 }
34
[email protected]1ef93132011-09-16 18:33:4735 delete this;
36}
37
[email protected]26b5e322011-12-23 01:36:4738void IntentInjector::SourceWebContentsDestroyed(WebContents* tab) {
[email protected]0d9989d2011-12-21 20:26:0039 intents_dispatcher_ = NULL;
[email protected]29a6c9732011-10-21 19:16:4240}
41
[email protected]0d9989d2011-12-21 20:26:0042void IntentInjector::SetIntent(
43 content::WebIntentsDispatcher* intents_dispatcher,
44 const webkit_glue::WebIntentData& intent) {
45 intents_dispatcher_ = intents_dispatcher;
[email protected]3ac1eef72011-09-28 20:46:0746 source_intent_.reset(new webkit_glue::WebIntentData(intent));
[email protected]1ef93132011-09-16 18:33:4747
48 SendIntent();
49}
50
51void IntentInjector::RenderViewCreated(RenderViewHost* host) {
52 SendIntent();
53}
54
[email protected]a6e16aec2011-11-11 18:53:0455void IntentInjector::DidNavigateMainFrame(
[email protected]1ef93132011-09-16 18:33:4756 const content::LoadCommittedDetails& details,
[email protected]6766b172011-11-21 18:29:3657 const content::FrameNavigateParams& params) {
[email protected]1ef93132011-09-16 18:33:4758 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]d8c660432011-12-22 20:51:2565// RenderViewHostInitialized to a WebContentsObserver latch? That still looks
[email protected]1ef93132011-09-16 18:33:4766// like it might be racy, though.
67void IntentInjector::SendIntent() {
68 if (source_intent_.get() == NULL ||
[email protected]325d0342012-01-26 22:56:3969 !CommandLine::ForCurrentProcess()->HasSwitch(
70 switches::kEnableWebIntents) ||
[email protected]768c5472011-12-26 19:06:1771 web_contents()->GetRenderViewHost() == NULL) {
[email protected]1ef93132011-09-16 18:33:4772 return;
73 }
74
75 // Send intent data through to renderer.
[email protected]768c5472011-12-26 19:06:1776 web_contents()->GetRenderViewHost()->Send(new IntentsMsg_SetWebIntentData(
77 web_contents()->GetRenderViewHost()->routing_id(),
[email protected]678105012011-12-09 04:01:2178 *(source_intent_.get())));
[email protected]1ef93132011-09-16 18:33:4779 source_intent_.reset(NULL);
80}
81
82bool IntentInjector::OnMessageReceived(const IPC::Message& message) {
83 bool handled = true;
84 IPC_BEGIN_MESSAGE_MAP(IntentInjector, message)
[email protected]678105012011-12-09 04:01:2185 IPC_MESSAGE_HANDLER(IntentsHostMsg_WebIntentReply, OnReply);
[email protected]1ef93132011-09-16 18:33:4786 IPC_MESSAGE_UNHANDLED(handled = false)
87 IPC_END_MESSAGE_MAP()
88 return handled;
89}
90
[email protected]678105012011-12-09 04:01:2191void IntentInjector::OnReply(webkit_glue::WebIntentReplyType reply_type,
92 const string16& data) {
[email protected]325d0342012-01-26 22:56:3993 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableWebIntents))
[email protected]1ef93132011-09-16 18:33:4794 NOTREACHED();
95
[email protected]0d9989d2011-12-21 20:26:0096 if (intents_dispatcher_) {
97 intents_dispatcher_->SendReplyMessage(reply_type, data);
[email protected]4f2c9c32011-10-21 03:02:5198 }
[email protected]1ef93132011-09-16 18:33:4799}