• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 "ppapi/shared_impl/ppb_instance_shared.h"
6 
7 #include <string>
8 
9 #include "base/debug/trace_event.h"
10 #include "base/threading/platform_thread.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/c/ppb_input_event.h"
13 #include "ppapi/shared_impl/ppapi_globals.h"
14 #include "ppapi/shared_impl/ppb_image_data_shared.h"
15 #include "ppapi/shared_impl/var.h"
16 #include "ppapi/thunk/enter.h"
17 #include "ppapi/thunk/ppb_image_data_api.h"
18 
19 namespace ppapi {
20 
21 // static
22 const int PPB_Instance_Shared::kExtraCharsForTextInput = 100;
23 
~PPB_Instance_Shared()24 PPB_Instance_Shared::~PPB_Instance_Shared() {
25 }
26 
Log(PP_Instance instance,PP_LogLevel level,PP_Var value)27 void PPB_Instance_Shared::Log(PP_Instance instance,
28                               PP_LogLevel level,
29                               PP_Var value) {
30   LogWithSource(instance, level, PP_MakeUndefined(), value);
31 }
32 
LogWithSource(PP_Instance instance,PP_LogLevel level,PP_Var source,PP_Var value)33 void PPB_Instance_Shared::LogWithSource(PP_Instance instance,
34                                         PP_LogLevel level,
35                                         PP_Var source,
36                                         PP_Var value) {
37   // The source defaults to empty if it's not a string. The PpapiGlobals
38   // implementation will convert the empty string to the module name if
39   // possible.
40   std::string source_str;
41   if (source.type == PP_VARTYPE_STRING)
42     source_str = Var::PPVarToLogString(source);
43   std::string value_str = Var::PPVarToLogString(value);
44   PpapiGlobals::Get()->LogWithSource(instance, level, source_str, value_str);
45 }
46 
ValidateRequestInputEvents(bool is_filtering,uint32_t event_classes)47 int32_t PPB_Instance_Shared::ValidateRequestInputEvents(
48     bool is_filtering,
49     uint32_t event_classes) {
50   // See if any bits are set we don't know about.
51   if (event_classes &
52       ~static_cast<uint32_t>(PP_INPUTEVENT_CLASS_MOUSE |
53                              PP_INPUTEVENT_CLASS_KEYBOARD |
54                              PP_INPUTEVENT_CLASS_WHEEL |
55                              PP_INPUTEVENT_CLASS_TOUCH |
56                              PP_INPUTEVENT_CLASS_IME))
57     return PP_ERROR_NOTSUPPORTED;
58 
59   // Everything else is valid.
60   return PP_OK;
61 }
62 
ValidateSetCursorParams(PP_MouseCursor_Type type,PP_Resource image,const PP_Point * hot_spot)63 bool PPB_Instance_Shared::ValidateSetCursorParams(PP_MouseCursor_Type type,
64                                                   PP_Resource image,
65                                                   const PP_Point* hot_spot) {
66   if (static_cast<int>(type) < static_cast<int>(PP_MOUSECURSOR_TYPE_CUSTOM) ||
67       static_cast<int>(type) > static_cast<int>(PP_MOUSECURSOR_TYPE_GRABBING))
68     return false;  // Cursor type out of range.
69   if (type != PP_MOUSECURSOR_TYPE_CUSTOM) {
70     // The image must not be specified if the type isn't custom. However, we
71     // don't require that the hot spot be null since the C++ wrappers and proxy
72     // pass the point by reference and it will normally be specified.
73     return image == 0;
74   }
75 
76   if (!hot_spot)
77     return false;  // Hot spot must be specified for custom cursor.
78 
79   thunk::EnterResourceNoLock<thunk::PPB_ImageData_API> enter(image, true);
80   if (enter.failed())
81     return false;  // Invalid image resource.
82 
83   // Validate the image size. A giant cursor can arbitrarily overwrite parts
84   // of the screen resulting in potential spoofing attacks. So we force the
85   // cursor to be a reasonably-sized image.
86   PP_ImageDataDesc desc;
87   if (!PP_ToBool(enter.object()->Describe(&desc)))
88     return false;
89   if (desc.size.width > 32 || desc.size.height > 32)
90     return false;
91 
92   // Validate image format.
93   if (desc.format != PPB_ImageData_Shared::GetNativeImageDataFormat())
94     return false;
95 
96   // Validate the hot spot location.
97   if (hot_spot->x < 0 || hot_spot->x >= desc.size.width ||
98       hot_spot->y < 0 || hot_spot->y >= desc.size.height)
99     return false;
100   return true;
101 }
102 
103 }  // namespace ppapi
104