summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/blocked_plugin.cc
blob: a6ef499ff0963b84456cb0e04ffcf7837fdba478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2010 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 "chrome/renderer/blocked_plugin.h"

#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/file_path.h"
#include "base/string_piece.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/notification_service.h"
#include "chrome/renderer/render_view.h"
#include "grit/generated_resources.h"
#include "grit/renderer_resources.h"
#include "third_party/WebKit/WebKit/chromium/public/WebData.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h"
#include "third_party/WebKit/WebKit/chromium/public/WebURL.h"
#include "third_party/WebKit/WebKit/chromium/public/WebView.h"
#include "webkit/glue/plugins/webview_plugin.h"

using WebKit::WebCursorInfo;
using WebKit::WebFrame;
using WebKit::WebPlugin;
using WebKit::WebPluginContainer;
using WebKit::WebPluginParams;
using WebKit::WebURL;
using WebKit::WebView;

static const char* const kBlockedPluginDataURL = "chrome://blockedplugindata/";

BlockedPlugin::BlockedPlugin(RenderView* render_view,
                             WebFrame* frame,
                            const WebPluginParams& params)
    : render_view_(render_view),
      frame_(frame),
      plugin_params_(params) {
  plugin_ = new WebViewPlugin(this);

  WebView* web_view = plugin_->web_view();
  web_view->mainFrame()->setCanHaveScrollbars(false);

  int resource_id = IDR_BLOCKED_PLUGIN_HTML;
  const base::StringPiece template_html(
      ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id));

  DCHECK(!template_html.empty()) << "unable to load template. ID: "
                                 << resource_id;

  DictionaryValue localized_strings;
  localized_strings.SetStringFromUTF16("loadPlugin",
      l10n_util::GetStringUTF16(IDS_PLUGIN_LOAD));

  // "t" is the id of the templates root node.
  std::string htmlData = jstemplate_builder::GetTemplatesHtml(
      template_html, &localized_strings, "t");

  web_view->mainFrame()->loadHTMLString(htmlData,
                                        GURL(kBlockedPluginDataURL));

  registrar_.Add(this,
                 NotificationType::SHOULD_LOAD_PLUGINS,
                 NotificationService::AllSources());
}

void BlockedPlugin::BindWebFrame(WebFrame* frame) {
  BindToJavascript(frame, L"plugin");
  BindMethod("load", &BlockedPlugin::Load);
}

void BlockedPlugin::WillDestroyPlugin() {
  delete this;
}

void BlockedPlugin::Observe(NotificationType type,
                            const NotificationSource& source,
                            const NotificationDetails& details) {
  if (type == NotificationType::SHOULD_LOAD_PLUGINS) {
    LoadPlugin();
  } else {
    NOTREACHED();
  }
}

void BlockedPlugin::Load(const CppArgumentList& args, CppVariant* result) {
  LoadPlugin();
}

void BlockedPlugin::LoadPlugin() {
  CHECK(plugin_);
  WebPluginContainer* container = plugin_->container();
  WebPlugin* new_plugin =
      render_view_->CreatePluginInternal(frame_,
                                         plugin_params_,
                                         NULL,
                                         std::string());
  if (new_plugin && new_plugin->initialize(container)) {
    container->setPlugin(new_plugin);
    plugin_->ReplayReceivedData(new_plugin);
    container->invalidate();
    container->reportGeometry();
    plugin_->destroy();
  }
}