Home | History | Annotate | Download | only in inspector
      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 "config.h"
      6 #include "core/inspector/ConsoleMessageStorage.h"
      7 
      8 #include "core/frame/LocalDOMWindow.h"
      9 #include "core/inspector/ConsoleMessage.h"
     10 #include "core/inspector/InspectorConsoleInstrumentation.h"
     11 
     12 namespace blink {
     13 
     14 static const unsigned maxConsoleMessageCount = 1000;
     15 
     16 ConsoleMessageStorage::ConsoleMessageStorage(ExecutionContext* context)
     17     : m_expiredCount(0)
     18     , m_context(context)
     19     , m_frame(nullptr)
     20 {
     21 }
     22 
     23 ConsoleMessageStorage::ConsoleMessageStorage(LocalFrame* frame)
     24     : m_expiredCount(0)
     25     , m_context(nullptr)
     26     , m_frame(frame)
     27 {
     28 }
     29 
     30 void ConsoleMessageStorage::reportMessage(PassRefPtrWillBeRawPtr<ConsoleMessage> prpMessage)
     31 {
     32     RefPtrWillBeRawPtr<ConsoleMessage> message = prpMessage;
     33     message->collectCallStack();
     34 
     35     if (message->type() == ClearMessageType)
     36         clear();
     37 
     38     InspectorInstrumentation::addMessageToConsole(executionContext(), message.get());
     39 
     40     ASSERT(m_messages.size() <= maxConsoleMessageCount);
     41     if (m_messages.size() == maxConsoleMessageCount) {
     42         ++m_expiredCount;
     43         m_messages.removeFirst();
     44     }
     45     m_messages.append(message);
     46 }
     47 
     48 void ConsoleMessageStorage::clear()
     49 {
     50     InspectorInstrumentation::consoleMessagesCleared(executionContext());
     51     m_messages.clear();
     52     m_expiredCount = 0;
     53 }
     54 
     55 Vector<unsigned> ConsoleMessageStorage::argumentCounts() const
     56 {
     57     Vector<unsigned> result(m_messages.size());
     58     for (size_t i = 0; i < m_messages.size(); ++i)
     59         result[i] = m_messages[i]->argumentCount();
     60     return result;
     61 }
     62 
     63 void ConsoleMessageStorage::adoptWorkerMessagesAfterTermination(WorkerGlobalScopeProxy* workerGlobalScopeProxy)
     64 {
     65     for (size_t i = 0; i < m_messages.size(); ++i) {
     66         if (m_messages[i]->workerGlobalScopeProxy() == workerGlobalScopeProxy)
     67             m_messages[i]->setWorkerGlobalScopeProxy(nullptr);
     68     }
     69 }
     70 
     71 void ConsoleMessageStorage::frameWindowDiscarded(LocalDOMWindow* window)
     72 {
     73     for (size_t i = 0; i < m_messages.size(); ++i)
     74         m_messages[i]->frameWindowDiscarded(window);
     75 }
     76 
     77 size_t ConsoleMessageStorage::size() const
     78 {
     79     return m_messages.size();
     80 }
     81 
     82 ConsoleMessage* ConsoleMessageStorage::at(size_t index) const
     83 {
     84     return m_messages[index].get();
     85 }
     86 
     87 int ConsoleMessageStorage::expiredCount() const
     88 {
     89     return m_expiredCount;
     90 }
     91 
     92 ExecutionContext* ConsoleMessageStorage::executionContext() const
     93 {
     94     return m_frame ? m_frame->document() : m_context;
     95 }
     96 
     97 void ConsoleMessageStorage::trace(Visitor* visitor)
     98 {
     99     visitor->trace(m_messages);
    100     visitor->trace(m_context);
    101     visitor->trace(m_frame);
    102 }
    103 
    104 } // namespace blink
    105