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.
5 #include "chrome/default_plugin/plugin_impl_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/file_util.h"
10 #include "base/path_service.h"
11 #include "base/string_util.h"
12 #include "chrome/common/chrome_plugin_messages.h"
13 #include "chrome/default_plugin/plugin_main.h"
14 #include "content/common/child_thread.h"
15 #include "googleurl/src/gurl.h"
16 #include "grit/default_plugin_resources.h"
17 #include "grit/webkit_strings.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/l10n/l10n_util_mac.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
23 #include "unicode/locid.h"
24 #include "webkit/glue/webkit_glue.h"
25 #include "webkit/plugins/npapi/default_plugin_shared.h"
27 // TODO(thakis): Most methods in this class are stubbed out and need to be
30 PluginInstallerImpl::PluginInstallerImpl(int16 mode)
35 PluginInstallerImpl::~PluginInstallerImpl() {
39 bool PluginInstallerImpl::Initialize(void* module_handle, NPP instance,
40 NPMIMEType mime_type, int16 argc,
41 char* argn[], char* argv[]) {
42 DVLOG(1) << __FUNCTION__ << " MIME Type : " << mime_type;
43 DCHECK(instance != NULL);
45 if (mime_type == NULL || strlen(mime_type) == 0) {
46 DLOG(WARNING) << __FUNCTION__ << " Invalid parameters passed in";
52 mime_type_ = mime_type;
54 command_ = [l10n_util::FixUpWindowsStyleLabel(l10n_util::GetStringUTF16(
55 IDS_DEFAULT_PLUGIN_NO_PLUGIN_AVAILABLE_MSG)) retain];
57 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
58 image_ = rb.GetNativeImageNamed(IDR_PLUGIN_ICON);
60 PluginInstallerBase::SetRoutingIds(argc, argn, argv);
64 bool PluginInstallerImpl::NPP_SetWindow(NPWindow* window_info) {
65 width_ = window_info->width;
66 height_ = window_info->height;
70 void PluginInstallerImpl::Shutdown() {
73 void PluginInstallerImpl::NewStream(NPStream* stream) {
74 plugin_install_stream_ = stream;
77 void PluginInstallerImpl::DestroyStream(NPStream* stream, NPError reason) {
78 if (stream == plugin_install_stream_)
79 plugin_install_stream_ = NULL;
82 bool PluginInstallerImpl::WriteReady(NPStream* stream) {
83 bool ready_to_accept_data = false;
84 return ready_to_accept_data;
87 int32 PluginInstallerImpl::Write(NPStream* stream, int32 offset,
88 int32 buffer_length, void* buffer) {
92 void PluginInstallerImpl::ClearDisplay() {
95 void PluginInstallerImpl::RefreshDisplay() {
98 bool PluginInstallerImpl::CreateToolTip() {
102 void PluginInstallerImpl::UpdateToolTip() {
105 void PluginInstallerImpl::DisplayAvailablePluginStatus() {
108 void PluginInstallerImpl::DisplayStatus(int message_resource_id) {
111 void PluginInstallerImpl::DisplayPluginDownloadFailedStatus() {
114 void PluginInstallerImpl::URLNotify(const char* url, NPReason reason) {
117 int16 PluginInstallerImpl::NPP_HandleEvent(void* event) {
118 NPCocoaEvent* npp_event = static_cast<NPCocoaEvent*>(event);
120 if (npp_event->type == NPCocoaEventDrawRect) {
121 CGContextRef context = npp_event->data.draw.context;
122 CGRect rect = CGRectMake(npp_event->data.draw.x,
123 npp_event->data.draw.y,
124 npp_event->data.draw.width,
125 npp_event->data.draw.height);
126 return OnDrawRect(context, rect);
131 std::wstring PluginInstallerImpl::ReplaceStringForPossibleEmptyReplacement(
132 int message_id_with_placeholders,
133 int messsage_id_without_placeholders,
134 const std::wstring& replacement_string) {
138 void PluginInstallerImpl::DownloadPlugin() {
141 void PluginInstallerImpl::DownloadCancelled() {
142 DisplayAvailablePluginStatus();
145 int16 PluginInstallerImpl::OnDrawRect(CGContextRef context, CGRect dirty_rect) {
146 gfx::ScopedNSGraphicsContextSaveGState scoped_state;
147 NSGraphicsContext* ns_context = [NSGraphicsContext
148 graphicsContextWithGraphicsPort:context flipped:YES];
149 [NSGraphicsContext setCurrentContext:ns_context];
152 NSColor* bg_color = [NSColor colorWithCalibratedRed:252 / 255.0
157 NSRectFill(NSRectFromCGRect(dirty_rect));
159 [[NSColor blackColor] set];
160 NSFrameRect(NSMakeRect(0, 0, width_, height_));
165 NSPoint point = NSMakePoint((width_ - [image_ size].width) / 2,
166 (height_ + [image_ size].height) / 2);
167 [image_ dissolveToPoint:point fraction:1.0];
171 NSShadow* shadow = [[[NSShadow alloc] init] autorelease];
172 [shadow setShadowColor:[NSColor colorWithDeviceWhite:1.0 alpha:0.5]];
173 [shadow setShadowOffset:NSMakeSize(0, -1)];
174 NSFont* font = [NSFont systemFontOfSize:
175 [NSFont systemFontSizeForControlSize:NSSmallControlSize]];
176 NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
177 font, NSFontAttributeName,
178 [NSColor blackColor], NSForegroundColorAttributeName,
179 shadow, NSShadowAttributeName,
182 NSSize text_size = [command_ sizeWithAttributes:attributes];
183 NSPoint label_point = NSMakePoint((width_ - text_size.width) / 2,
184 (height_ - text_size.height) / 2);
186 label_point.y += [image_ size].height / 2 + text_size.height / 2 + 10;
187 label_point = NSMakePoint(roundf(label_point.x), roundf(label_point.y));
188 [command_ drawAtPoint:label_point withAttributes:attributes];
194 void PluginInstallerImpl::ShowInstallDialog() {
197 void PluginInstallerImpl::NotifyPluginStatus(int status) {
198 ChildThread::current()->Send(
199 new ChromePluginProcessHostMsg_MissingPluginStatus(
201 renderer_process_id(),