summaryrefslogtreecommitdiffstats
path: root/chrome_frame/bind_context_info.cc
blob: bfdf89a20d73852e363f0879bf59c6e914b6adc9 (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
// Copyright (c) 2011 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_frame/bind_context_info.h"
#include "chrome_frame/utils.h"

// This is non const due to API expectations
static wchar_t* kBindContextInfo = L"_CHROMEFRAME_BIND_CONTEXT_INFO_";

// BindContextInfo member definitions.
BindContextInfo::BindContextInfo()
    : no_cache_(false),
      chrome_request_(false),
      is_switching_(false) {
}

BindContextInfo::~BindContextInfo() {
}

HRESULT BindContextInfo::Initialize(IBindCtx* bind_ctx) {
  DCHECK(bind_ctx);
  DCHECK(!ftm_);
  HRESULT hr = CoCreateFreeThreadedMarshaler(GetUnknown(), ftm_.Receive());
  DCHECK(ftm_);
  if (SUCCEEDED(hr)) {
    hr = bind_ctx->RegisterObjectParam(kBindContextInfo, GetUnknown());
  }

  DCHECK(SUCCEEDED(hr)) << "Failed to initialize BindContextInfo";
  return hr;
}

HRESULT BindContextInfo::FromBindContext(IBindCtx* bind_context,
                                         BindContextInfo** info) {
  DCHECK(info);
  if (!bind_context) {
    NOTREACHED();
    return E_POINTER;
  }

  base::win::ScopedComPtr<IUnknown> context;
  HRESULT hr = bind_context->GetObjectParam(kBindContextInfo,
                                            context.Receive());
  if (context) {
    base::win::ScopedComPtr<IBindContextInfoInternal> internal;
    hr = internal.QueryFrom(context);
    if (SUCCEEDED(hr)) {
      hr = internal->GetCppObject(reinterpret_cast<void**>(info));
      DCHECK_EQ(hr, S_OK);
      DLOG_IF(ERROR, *info != static_cast<BindContextInfo*>(internal.get()))
          << "marshalling took place!";
    }
  } else {
    DCHECK(FAILED(hr));
    CComObject<BindContextInfo>* bind_context_info = NULL;
    hr = CComObject<BindContextInfo>::CreateInstance(&bind_context_info);
    DCHECK(bind_context_info != NULL);
    if (bind_context_info) {
      bind_context_info->AddRef();
      hr = bind_context_info->Initialize(bind_context);
      if (FAILED(hr)) {
        bind_context_info->Release();
      } else {
        *info = bind_context_info;
      }
    }
  }

  return hr;
}

void BindContextInfo::SetToSwitch(IStream* cache) {
  is_switching_ = true;
  if  (!no_cache_) {
    cache_ = cache;
    RewindStream(cache_);
  }
}

std::wstring BindContextInfo::GetUrl() {
  if (has_prot_data()) {
    return prot_data_->url();
  }
  return std::wstring();
}