Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / web_cache / renderer / web_cache_render_process_observer.cc
1 // Copyright 2014 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 "components/web_cache/renderer/web_cache_render_process_observer.h"
6
7 #include <limits>
8
9 #include "components/web_cache/common/web_cache_messages.h"
10 #include "third_party/WebKit/public/web/WebCache.h"
11
12 using blink::WebCache;
13
14 namespace web_cache {
15
16 namespace {
17 const size_t kUnitializedCacheCapacity = UINT_MAX;
18 }
19
20 WebCacheRenderProcessObserver::WebCacheRenderProcessObserver()
21   : clear_cache_pending_(false),
22     webkit_initialized_(false),
23     pending_cache_min_dead_capacity_(0),
24     pending_cache_max_dead_capacity_(0),
25     pending_cache_capacity_(kUnitializedCacheCapacity) {
26 }
27
28 WebCacheRenderProcessObserver::~WebCacheRenderProcessObserver() {
29 }
30
31 void WebCacheRenderProcessObserver::ExecutePendingClearCache() {
32   if (clear_cache_pending_ && webkit_initialized_) {
33     clear_cache_pending_ = false;
34     WebCache::clear();
35   }
36 }
37
38 bool WebCacheRenderProcessObserver::OnControlMessageReceived(
39     const IPC::Message& message) {
40   bool handled = true;
41   IPC_BEGIN_MESSAGE_MAP(WebCacheRenderProcessObserver, message)
42     IPC_MESSAGE_HANDLER(WebCacheMsg_SetCacheCapacities, OnSetCacheCapacities)
43     IPC_MESSAGE_HANDLER(WebCacheMsg_ClearCache, OnClearCache)
44     IPC_MESSAGE_UNHANDLED(handled = false)
45   IPC_END_MESSAGE_MAP()
46   return handled;
47 }
48
49 void WebCacheRenderProcessObserver::WebKitInitialized() {
50   webkit_initialized_ = true;
51   if (pending_cache_capacity_ != kUnitializedCacheCapacity) {
52     WebCache::setCapacities(pending_cache_min_dead_capacity_,
53                             pending_cache_max_dead_capacity_,
54                             pending_cache_capacity_);
55   }
56 }
57
58 void WebCacheRenderProcessObserver::OnRenderProcessShutdown() {
59   webkit_initialized_ = false;
60 }
61
62 void WebCacheRenderProcessObserver::OnSetCacheCapacities(
63     size_t min_dead_capacity,
64     size_t max_dead_capacity,
65     size_t capacity) {
66   if (!webkit_initialized_) {
67     pending_cache_min_dead_capacity_ = min_dead_capacity;
68     pending_cache_max_dead_capacity_ = max_dead_capacity;
69     pending_cache_capacity_ = capacity;
70     return;
71   }
72
73   WebCache::setCapacities(
74       min_dead_capacity, max_dead_capacity, capacity);
75 }
76
77 void WebCacheRenderProcessObserver::OnClearCache(bool on_navigation) {
78   if (on_navigation || !webkit_initialized_)
79     clear_cache_pending_ = true;
80   else
81     WebCache::clear();
82 }
83
84 }  // namespace web_cache