Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(761)

Side by Side Diff: ppapi/proxy/flash_resource.cc

Issue 11516020: Refactor PPB_Flash GetLocalTimeZoneOffset to the new PPAPI resource model (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/proxy/flash_resource.h ('k') | ppapi/proxy/ppapi_messages.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/proxy/flash_resource.h" 5 #include "ppapi/proxy/flash_resource.h"
6 6
7 #include <cmath>
8
9 #include "base/containers/mru_cache.h"
10 #include "base/lazy_instance.h"
11 #include "base/time.h"
7 #include "ppapi/c/pp_errors.h" 12 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/private/ppb_flash.h" 13 #include "ppapi/c/private/ppb_flash.h"
9 #include "ppapi/proxy/plugin_globals.h" 14 #include "ppapi/proxy/plugin_globals.h"
10 #include "ppapi/proxy/ppapi_messages.h" 15 #include "ppapi/proxy/ppapi_messages.h"
16 #include "ppapi/shared_impl/time_conversion.h"
11 #include "ppapi/shared_impl/var.h" 17 #include "ppapi/shared_impl/var.h"
12 18
13 namespace ppapi { 19 namespace ppapi {
14 namespace proxy { 20 namespace proxy {
15 21
22 namespace {
23
24 struct LocalTimeZoneOffsetEntry {
25 base::TimeTicks expiration;
26 double offset;
27 };
28
29 class LocalTimeZoneOffsetCache
30 : public base::MRUCache<PP_Time, LocalTimeZoneOffsetEntry> {
31 public:
32 LocalTimeZoneOffsetCache()
33 : base::MRUCache<PP_Time, LocalTimeZoneOffsetEntry>(kCacheSize) {}
34 private:
35 static const size_t kCacheSize = 100;
36 };
37
38 base::LazyInstance<LocalTimeZoneOffsetCache>::Leaky
39 g_local_time_zone_offset_cache = LAZY_INSTANCE_INITIALIZER;
40
41 } // namespace
42
16 FlashResource::FlashResource(Connection connection, PP_Instance instance) 43 FlashResource::FlashResource(Connection connection, PP_Instance instance)
17 : PluginResource(connection, instance) { 44 : PluginResource(connection, instance) {
18 SendCreate(RENDERER, PpapiHostMsg_Flash_Create()); 45 SendCreate(RENDERER, PpapiHostMsg_Flash_Create());
19 SendCreate(BROWSER, PpapiHostMsg_Flash_Create()); 46 SendCreate(BROWSER, PpapiHostMsg_Flash_Create());
20 } 47 }
21 48
22 FlashResource::~FlashResource() { 49 FlashResource::~FlashResource() {
23 } 50 }
24 51
25 thunk::PPB_Flash_Functions_API* FlashResource::AsPPB_Flash_Functions_API() { 52 thunk::PPB_Flash_Functions_API* FlashResource::AsPPB_Flash_Functions_API() {
(...skipping 23 matching lines...) Expand all
49 StringVar* url_string_var(StringVar::FromPPVar(value)); 76 StringVar* url_string_var(StringVar::FromPPVar(value));
50 if (!url_string_var) 77 if (!url_string_var)
51 return PP_FALSE; 78 return PP_FALSE;
52 PluginGlobals::Get()->SetActiveURL(url_string_var->value()); 79 PluginGlobals::Get()->SetActiveURL(url_string_var->value());
53 return PP_TRUE; 80 return PP_TRUE;
54 } 81 }
55 } 82 }
56 return PP_FALSE; 83 return PP_FALSE;
57 } 84 }
58 85
86 double FlashResource::GetLocalTimeZoneOffset(PP_Instance instance,
87 PP_Time t) {
88 LocalTimeZoneOffsetCache& cache = g_local_time_zone_offset_cache.Get();
89
90 // Get the minimum PP_Time value that shares the same minute as |t|.
91 // Use cached offset if cache hasn't expired and |t| is in the same minute as
92 // the time for the cached offset (assume offsets change on minute
93 // boundaries).
94 PP_Time t_minute_base = floor(t / 60.0) * 60.0;
95 LocalTimeZoneOffsetCache::iterator iter = cache.Get(t_minute_base);
96 base::TimeTicks now = base::TimeTicks::Now();
97 if (iter != cache.end() && now < iter->second.expiration)
98 return iter->second.offset;
99
100 // Cache the local offset for ten seconds, since it's slow on XP and Linux.
101 // Note that TimeTicks does not continue counting across sleep/resume on all
102 // platforms. This may be acceptable for 10 seconds, but if in the future this
103 // is changed to one minute or more, then we should consider using base::Time.
104 const int64 kMaxCachedLocalOffsetAgeInSeconds = 10;
105 base::TimeDelta expiration_delta =
106 base::TimeDelta::FromSeconds(kMaxCachedLocalOffsetAgeInSeconds);
107
108 LocalTimeZoneOffsetEntry cache_entry;
109 cache_entry.expiration = now + expiration_delta;
110 cache_entry.offset = 0.0;
111
112 // We can't do the conversion here on Linux because the localtime calls
113 // require filesystem access prohibited by the sandbox.
114 // TODO(shess): Figure out why OSX needs the access, the sandbox warmup should
115 // handle it. http://crbug.com/149006
116 #if defined(OS_LINUX) || defined(OS_MACOSX)
117 int32_t result = SyncCall<PpapiPluginMsg_Flash_GetLocalTimeZoneOffsetReply>(
118 BROWSER,
119 PpapiHostMsg_Flash_GetLocalTimeZoneOffset(PPTimeToTime(t)),
120 &cache_entry.offset);
121 if (result != PP_OK)
122 cache_entry.offset = 0.0;
123 #else
124 cache_entry.offset = PPGetLocalTimeZoneOffset(PPTimeToTime(t));
125 #endif
126
127 cache.Put(t_minute_base, cache_entry);
128 return cache_entry.offset;
129 }
130
59 } // namespace proxy 131 } // namespace proxy
60 } // namespace ppapi 132 } // namespace ppapi
OLDNEW
« no previous file with comments | « ppapi/proxy/flash_resource.h ('k') | ppapi/proxy/ppapi_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698