summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/offline/offline_load_service.cc
blob: a75fba66d6732fdf8131874c798982c26a239442 (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
// 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/browser/chromeos/offline/offline_load_service.h"

#include "base/ref_counted.h"
#include "base/singleton.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/common/notification_service.h"

namespace chromeos {

// A utility class that serves a singleton instance of OfflineLoadService.
// OfflineLoadSerivce itself cannot be a singleton as it implements
// RefCount interface.
class OfflineLoadServiceSingleton {
 public:
  chromeos::OfflineLoadService* offline_load_service() {
    return offline_load_service_.get();
  }

 private:
  friend struct DefaultSingletonTraits<OfflineLoadServiceSingleton>;
  OfflineLoadServiceSingleton()
      : offline_load_service_(new chromeos::OfflineLoadService()) {}
  virtual ~OfflineLoadServiceSingleton() {}

  scoped_refptr<chromeos::OfflineLoadService> offline_load_service_;

  DISALLOW_COPY_AND_ASSIGN(OfflineLoadServiceSingleton);
};

// static
OfflineLoadService* OfflineLoadService::Get() {
  return Singleton<OfflineLoadServiceSingleton>::get()->offline_load_service();
}

void OfflineLoadService::Observe(NotificationType type,
                                 const NotificationSource& source,
                                 const NotificationDetails& details) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  if (type.value == NotificationType::TAB_CLOSED) {
    registrar_.Remove(this, NotificationType::TAB_CLOSED, source);
    NavigationController* tab = Source<NavigationController>(source).ptr();
    BrowserThread::PostTask(
        BrowserThread::IO, FROM_HERE,
        NewRunnableMethod(this,
                          &OfflineLoadService::RemoveTabContents,
                          tab->tab_contents()));
  }
}

bool OfflineLoadService::ShouldProceed(int process_host_id,
                                       int render_view_id,
                                       const GURL& url) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  TabContents* tab_contents = tab_util::GetTabContentsByID(
      process_host_id, render_view_id);
  DCHECK(tab_contents);
  bool proceed = tabs_.find(tab_contents) != tabs_.end();
  DLOG(INFO) << "ShouldProceed:" << proceed << ", url=" << url.spec()
             << ", tab_contents=" << tab_contents;
  return proceed;
}

void OfflineLoadService::Proceeded(int process_host_id,
                                   int render_view_id,
                                   const GURL& url) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  TabContents* tab_contents = tab_util::GetTabContentsByID(
      process_host_id, render_view_id);
  DCHECK(tab_contents);
  if (tabs_.find(tab_contents) == tabs_.end()) {
    DLOG(INFO) << "Proceeded: url=" << url.spec()
               << ", tab_contents=" << tab_contents;
    tabs_.insert(tab_contents);
    BrowserThread::PostTask(
        BrowserThread::UI, FROM_HERE,
        NewRunnableMethod(this,
                          &OfflineLoadService::RegisterNotification,
                          &tab_contents->controller()));
  } else {
    DLOG(WARNING) << "Proceeded: ignoring duplicate";
  }
}

void OfflineLoadService::RemoveTabContents(TabContents* tab_contents) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  tabs_.erase(tabs_.find(tab_contents));
}

void OfflineLoadService::RegisterNotification(
    NavigationController* navigation_controller) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  registrar_.Add(this, NotificationType::TAB_CLOSED,
                   Source<NavigationController>(
                       navigation_controller));
}

}  // namespace chromeos